六大设计原则

March 22, 2016

设计模式与设计原则

六大设计原则

单一职责原则 Single Responsibility Principle

应该有且仅有一个原因引起类的变更。There should never be more than one reason for a class to change.

开闭原则 Open Closed Principle

Software entities like classes,modules and functions should be open for extension but closed for modifications.
一个软件实体如类,模块和函数应该对外扩展开放,对修改关闭。

里氏替换原则 Liskov Substitution Principle

If for each object o1 of type S these is an object o2 of type T such that for all programs P defined in terms of T , the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T .

如果对每一个类型为S 的对象 o1,都有类型为 T 的对象 o2,使得以 T 定义的所有程序 P 在所有对象 o1 都代换成 o2 时,程序P的行为没有发生变化,那么类型S是类型T的子类型。

Functions that use pointer or references to base classes must be able to use objects of derived classes without knowing it.

所有引用基类的地方必须能透明地使用其子类的对象。

迪米特法则 Law of Demeter

也叫 Least Knowledge Principle 最少知识原则
一个对象应该对其他对象有最少的了解。

接口隔离原则 Interface Segregation Principle

Clients should not be forced to depend upon interfaces that they don't use.
客户端不应该依赖它不需要的接口

The dependency of one class to another one should depend on the smallest possible interface .
类间的依赖关系应该建立在最少的接口上。

依赖倒置原则 Dependence Inversion Principle

High level modules should not depend upon low level modules.Both should depend upon abstractions.Abstractions should not depend upon details.Details should depend upon abstractions.

  • 高层模块不应该依赖底层模块,两者都应该依赖其抽象
  • 抽象不应该依赖细节
  • 细节应该依赖抽象

--- EOF ---

添加新评论