OOD设计原则概述:
RobertCecilMartin, BertrandMeyer, BarbaraLiskov等提出了面向对象的11个设计原则,被面向对象设计师们视为OOD的圣经.可以从下面看到他们的原文:
http://c2.com/cgi/wiki?PrinciplesOfObjectOrientedDesign
类设计原则(Class Design Principles):
- 单一职责原则(SRP) : 一个类应该有且只有一个改变的理由。 (SRP) The Single Responsibility Principle: A class should have only one reason to change.
- 开放封闭原则(OCP) :软件实体(类,模块,函数等)应该是可以扩展的,但是不能修改。 (OCP) The Open-Closed Principle: Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
- Liskov替换原则(LSP):子类必须适用于基类,即子类一定能替换基类,反之则不然. (LSP) The Liskov Substitution Principle:Subtypes must be substitutable for their base types.
- 依赖倒置原则(DIP):抽象不应该依赖于细节,而细节应该依赖于抽象。 (DIP) The Dependency Inversion Principle :Abstractions should not depend upon details. Details should depend upon abstractions.
- 接口隔离原则(ISP):客户只要关注它们所需的接口,而不应该强迫客户依赖于它们不用的方法。 (ISP) The Interface Segregation Principle:Clients should not be forced to depend upon methods that they do not use. Interfaces belong to clients, not to hierarchies.
包内聚性原则(Principles of Package Cohesion):
- 重用发布等价原则(REP):重用的粒度就是发布的粒度。 (REP) The Reuse/Release Equivalency Principle:The granule of reuse is the granule of release.
- 共同封闭原则(CCP):包中的所有类对于同一类性质的变化应该是共同封闭的。 (CCP) The Common Closure Principle:he classes in a package should be closed together against the same kinds of changes. A change that affects a closed package affects all the classes in that package and no other packages.
- 共同重用原则(CRP):一个包中的所有类应该是共同重用的。
-
(CRP) The Common Reuse Principle:The classes in a package are reused together. If you reuse one of the classes in the package, you reuse them all.
包耦合性原则(Principles of Package Coupling):
- 无环依赖原则(ADP):在包的依赖关系图中不允许存在环。 (ADP) The Acyclic Dependencies Principle:Allow no cycles in the package dependency graph.
- 稳定依赖原则(SDP):朝着稳定的方向进行依赖。 (SDP) The Stable Dependencies Principle:Depend in the direction of stability.
- 稳定抽象原则(SAP):包的抽象程度应该和其稳定程度一致 (SAP) The Stable Abstraction Principle:A package should be as abstract as it is stable.