六大设计原则 (1)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/efine_dxq/article/details/72904071

1、单一职责原则

There should never be more than one reason for a class change.

2、里氏替换原则

  • 第一种定义

    If for each object o1 of type S there 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 substitued for o2 then S is a subtype of T.
    如果对于每一个类型为S的对象o1,都有类型为T的对象o2,使得以T定义的所有程序P在所有的对象o1都代换成o2时,程序P的行为没有发生变化,那么类型S是类型T的子类型。

  • 第二种定义

    Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.
    所有引用基类的地方必须能透明地使用其子类的对象。

3、依赖倒置原则

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.
高层模块儿不应该依赖底层模块,两者都应该依赖其抽象。
抽象不应该依赖细节,细节应该依赖抽象。
(面向接口编程)

4、接口隔离原则

客户端不应该依赖它所不需要的接口。
类间的依赖关系应该建立在最小的接口上。

5、迪米特法则

一个对象应该对其他对象有最少的了解。

6、开闭原则

一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。

猜你喜欢

转载自blog.csdn.net/efine_dxq/article/details/72904071