OOD-ELEVATOR CASE
• S – Single responsibility principle
• O – Open close principle
• L – Liskov substitution principle
• I – Interface segregation principle
• D – Dependency inversion principle
Single responsibility principle 单一责任原则
一个类应该有且只有一个去改变他的理由,这意味着一个类应该只有一项工作。
Open close principle 开放封闭原则
对象或实体应该对扩展开放,对修改封闭 (Open to extension, close to modification)。
Liskov substitution principle 里氏替换原则
任何一个子类或派生类应该可以替换它们的基类或父类
Interface segregation principle 接口分离原则
不应该强迫一个类实现它用不上的接口
Dependency inversion principle 依赖反转原则
抽象不应该依赖于具体实现,具体实现应该依赖于抽象
Clarify
通过和面试官交流,去除题目中的歧义,确定答题范围
Core objects
确定题目所涉及的类,以及类之间的映射关系
• Access modifier
- package
- public
- private
- protected
Elevator
- List buttons
Request
ElevatorSystem
- List elevators
ElevatorButton
Cases
确定题目中所需要实现的场景和功能
• Elevator
- Take external request
- Take internal request
- Open gate
- Close gate
- Check weight
Classes
通过类图的方式,具体填充题目中涉及的类
Class diagram (类图)
- Class Name
- Attributes
- Functions
遍历你所列出的use cases
- 对于每一个use case,更加详细的述这个use case在做什么事情 (例如:take external request -> ElevatorSystem takes an external request, and decide to push this request to an appropriate elevator)
- 针对这个述,在已有的Core objects里填充进所需要的信息
Correctness 说人话:检查自己的设计,是否满足关键点
ELEVATOR
What if I want to apply different ways to handle external requests during different time of a day?
Solution 1: if - else
Solution 2: Strategy design pattern
• 如何判断一个Internal request 是否为Valid?
Solution:
If elevator going up
requested level lower than current level
invalid
If elevator going down
requested level higher than current level
invalid
• 如何知道一个函数,是否成功完成任务?
- Use exceptions
Use case: check weight
An elevator checks its current weight and compare with limit to see if overweight
• Use case: Close gate
An elevator
checks if overweight;
close the door;
then check stops corresponds to current status; if no stops left, check the reserve direction stops; change status to reserve direction or idle.
• Use case: Open gate
An elevator reaches the destination level, open gate
• Use case: Take internal request
An elevator takes an internal request, determine if it’s valid, inserts in its stop list.
• Use case: Take external request
An elevator takes an external request, inserts in its stop list.
Use case: Handle request
ElevatorSystem takes an external request, and decide to push this
request to an appropriate elevator