OOD —— 管理类

139 阅读2分钟

步骤

题目后面都可以接上三个字:“管理员

  • NO implementation:
    • clarify requirement:问清楚哪些是必须的,哪些可以忽略
    • define class (core object):定义哪些class,以及有哪些具体的不同类型
      • 角度1:除了题目中问的object(管理员)外,被管理的objects有哪些
      • 角度2:这个管理系统中,input和output是什么
      • 角度3:常用「收据」的形式,来保存信息(ie:停车票/借书单……)
    • define field:每个class有哪些field
    • define method, how data flow works:class之间如何交互
      • 把自己想象成「管理员」,思考use case(一般从以下3个角度思考)
        • 预定
        • 服务
        • 结算
      • inheritance(interface / abstract class)为了好扩展
  • Implementation:
    • implement核心关键methods
  • *optimize with design pattern (锦上添花,optional)


停车场

image.png

class / field / method:

  • class:parking lot:本身作为一个系统,里面直接保存停车位等信息
    • field:
      • map<location, parkingSpot>
      • map<location, parkingTicket>:可以优化提取成一个单独的system
      • capcity of each type of parking spot:可以先hardcode,再优化成可扩展的
    • method:
      • Ticket park(String vehicle):停车 -> 根据车子的类型来分配车位并出票
        1. user give a vehicle type
        2. check if there is available parking spot for this type of vehicle
        3. (optional) *check if there is larger spot available if exact size spot is full
        4. add <location, parking spot> to our local map, and decrease capacity
        5. create a parking ticket and get it back to the user
      • Ticket exit(Ticket ticket):离开 -> 释放车位,根据车票计算收费 -> 返回车票作为收据
        1. read ticket and calculate fee(可能根据车子的类型有不同的收费标准,用strategy pattern)
        2. relase parking spot, increase capacity
        3. update ticket and return it back to the user as a receipt
  • class:parking spot(abstract class):给各种不同车型的停车位(output
    • field:
      • parking location (ie: A3, B4, ...)
  • class:parking ticket
    • field:
      • parking location
      • enter/exit timestamp
      • charge fee amount/ratio
  • *class:vehicle(input
  • *class:admin/user class
  • *class:payment class:信用卡/现金...