面向对象设计的原则指南 – 概要篇
2007-12-10 22:06Update
我们在进行面向对象设计(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 modification.
软件实体(模块,类,方法等)应该对扩展开放,对修改关闭。
The Liskov Substitution Principle (LSP) - OO设计的里氏替换原则Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.
所有引用基类的地方必须能透明地使用其子类的对象。
The Dependency Inversion Principle (DIP) - OO设计的依赖倒置原则A. High level modules should not depend upon low level modules. Both should depend upon abstractions.
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.
不能强迫用户去依赖那些他们不使用的接口。
包设计原则
包的内部关系方面(聚合性)的原则
The Release Reuse Equivalency Principle (REP) - OO设计的重用发布等价原则The granule of reuse is the granule of release.
重用粒度等价于发布粒度。
The Common Reuse Principle (CRP) - OO设计的全部重用原则The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.
包的所有类被一起重用。如果你重用了其中的一个类,就重用全部。
The Common Closure Principle (CCP) - OO设计的共同封闭原则The classes in a package should be closed together against the same kinds of changes. a change that affects a package affects all the classes in that package.
一个包中所有的类应该对同一种类型的变化关闭。一个变化影响一个包,便影响了包中所有的类。
包之间的关系方面(耦合性)的原则
The Acyclic Dependencies Principle (ADP) - OO设计的无环依赖原则The dependency structure between packages must be a directed acyclic graph (DAG). That is, there must be no cycles in the dependency structure.
包之间的依赖结构必须是一个直接的无环图形(DAG)。也就是说,在依赖结构中不允许出现环(循环依赖)。
The Stable Dependencies Principle (SDP) - OO设计的稳定依赖原则The dependencies between packages in a design should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable that it is.
一个设计中的包之间的依赖应该朝着稳定的方向进行。一个包只应该依赖那些比自己更稳定的包。
The Stable Abstractions Principle (SAP) - OO设计的稳定抽象等价原则Packages that are maximally stable should be maximally abstract. Instable packages should be concrete. The abstraction of a package should be in proportion to its stability.
最稳定的包应该是最抽象的包。不稳定的包应该是具体的包。包的抽象程度跟它的稳定性成正比。
参考资料
The Single Responsibility Principle

The Open Closed Principle

The Liskov Substitution Principle

The Dependency Inversion Principle

The Interface Segregation Principle

Granularity
by Robert C. Martin。Principles and Patterns
by Robert C. Martin。Stability
by Robert C. Martin。
- Relative Articles
- GRASP设计模式 - 概要篇 - (2007-12-16 19:59)
- GRASP Protected Variations Pattern - GRASP之变化预防模式 - (2007-12-13 20:32)
- GRASP Pure Fabrication Pattern - GRASP之纯虚构模式 - (2007-12-13 20:27)
- GRASP Polymorphism Pattern - GRASP之多态性模式 - (2007-12-13 20:24)
- GRASP Indirection Pattern - GRASP之间接性模式 - (2007-12-12 22:36)
- GRASP Controller Pattern - GRASP之控制器模式 - (2007-12-12 22:34)
- GRASP Low Coupling Pattern - GRASP之低耦合模式 - (2007-12-12 22:32)
- GRASP High Cohesion Pattern - GRASP之高内聚模式 - (2007-12-12 22:30)
- GRASP Creator Pattern - GRASP之创建者模式 - (2007-12-12 22:28)
- GRASP Information Expert Pattern - Grasp之信息专家模式 - (2007-12-11 21:57)