面向对象设计的原则指南
–
概要篇
我们在进行面向对象设计
(
OOD
)
时应该怎样进行,
遵循什么原则呢?我们或许听说过设计
模式,
那是针对特定的问题提出的特定的解决方法。
面向对象的设计从提出到现在经过很多
人的经验和实践,也总结出了很多原则。
在设计开发中,
如果能有意识地向这些原则靠拢,
对我们的系统设计与开发会有很大的帮助,
也是构筑具有稳定性,扩展性的系统的一个保障:
-
是否遵守了那些基本原则
-
如果违反了基本原则,是否存在合适的理由
这些被大师们总结出来的
基本原则
包括了:
1
,类的设计原则
2
,包的设计原则
2.1
包的内部关系方面(聚合性)的原则
2.2
包之间的关系方面(耦合性)的原则
类设计原则
The Single Responsibility Principle (SRP) - OO
设计的单一职责原则
There should never be more than one reason for a class to change.
永远不要让一个类存在多个改变的理由。
The Open-Closed Principle (OCP) -
面向对象软件设计的开闭原则
Software entities (classes, modules, function, etc.) should be open for extension, but closed for m
odification.
软件实体(模块,类,方法等)应该对扩展开放,对修改关闭。
The Liskov Substitution Principle (LSP) - OO
设计的里氏替换原则
Functions that use pointers or references to base classes must be able to use objects of derived cl
asses without knowing it.
所有引用基类的地方必须能透明地使用其子类的对象。
The Dependency Inversion Principle (DIP) - OO
设计的依赖倒置原则
A. High level modules should not depend upon low level modules. Both should depend upon abst
ractions.
B. Abstractions should not depend upon details. Details should depend upon abstractions.
A.
高层模块不应该依赖于低层模块,二者都应该依赖于抽象
B.
抽象不应该依赖于细节,细节应该依赖于抽象
The Interface Segregation Principle (ISP) - OO
设计的接口分隔原则
Clients should not be forced to depend upon interfaces that they do not use.
不能强迫用户去依赖那些他们不使用的接口。