c++设计模式:设计原则(C + + design pattern: design principles)

c++设计八大原则(降低改变带来的代码修改)

一.依赖倒置原则(DIP)

1.高层模块(稳定)不应该依赖于低层模块(变化),二者应该依赖于抽象(更稳定)
<高层模块 包括 低层模块所依赖的抽象,而不是低层模块的本身,而其本身也依赖于自身抽象模块>

2.抽象(稳定)不应该依赖于实现细节(变化),实现细节应该依赖于抽象(稳定)
<抽象本身不应该依赖于低层的细节变化,否则会消除抽象的稳定性 (抽象类不应该调用子类函数),应该将低层变化的细节依赖于抽象的稳定中去>

二.开放封闭原则(OCP)

1.对扩展开放,对更改封闭
2.类模块应该是可扩展的,但不可修改
<类模块可以进行拓展,但不要进行修改(添加远远好于在原来基础上修改>

三.单一职责原则(SRP)

1.一个类应该仅有一个引起它变化的原因
2.变化的方向隐含着类的责任
<一个类责任要单一,不应该拥有过多的责任,使类本身被四处拉扯>

四.Liskov替换原则(LSP)

1.子类必须能够替换它们的基类(IS-A)
2.继承表达类型抽象
<考虑一个类继承于某个父类的时候,一定要满足IS-A的关系,不可随意继承>

五.接口隔离原则(ISP)

1.不应该强迫客户程序依赖它们不用的方法
2.接口应该小而完备
<对接口应该严格管理对外的权限,做到小而完备>

六.优先使用对象组合,而不是类继承

1.类继承通常为“白箱复用”,对象组合通常为“黑箱复用”
2.继承在某种程度上破坏了封装性。子类父类耦合度高
3.而对象组合则只要求被组合的对象具有良好定义的接口,耦合度低
<不要过度依赖于继承,有时候对象组合也许才是更好的选择>

七.封装变化点

1.使用封装来创建对象之间的分界层,让设计者可以在分界层的一侧进行修改,而不会对另外一侧产生不良的影响,从而实现分界层次间的松耦合
<理解变化点和分界层,进一步松耦合>

八.针对接口编程,而不是针对实现编程

1.不将变量类型声明为某个特定的具体类,而是声明为某个接口。·客户程序无需获知对象的具体类型,只需要知道对象所具有的接口
2.减少系统中各部分的依赖关系,从而实现“高内聚、松耦合”的类型设计方案。
<接口标准化!多用接口不直接使用内部数据>

————————

Eight principles of C + + Design (reduce code modification caused by change)

1、 Dependency Inversion Principle (DIP)

1. High level modules (stable) should not depend on low-level modules (change), but both should rely on < strong > abstraction < / strong > (more stable)
< The high-level module includes the abstraction that the low-level module depends on, not the low-level module itself, and it also depends on its own abstract module & gt;

2. Abstraction (stability) should not depend on implementation details (change), and implementation details should depend on abstraction (stability)
< The abstraction itself should not depend on low-level detail changes, otherwise it will eliminate the stability of the abstraction (abstract classes should not call subclass functions), and the details of low-level changes should be dependent on the stability of the abstraction & gt;

2、 Open closed principle (OCP)

1. It is open to expansion and closed to change
2. Class modules should be extensible, but not modifiable
< Class modules can be expanded but not modified (adding is far better than modifying on the original basis & gt;

3、 Single responsibility principle (SRP)

1. A class should have only one reason for its change
2. The direction of change implies the class’s < strong > responsibility < / strong >
< A class should have a single responsibility and should not have too many responsibilities, so that the class itself is pulled around & gt;

四.Liskov替换原则(LSP)

1. Subclasses must be able to replace their base class (is-a)
2. Inheritance expression type abstraction
≪ when considering that a class inherits from a parent class, it must meet the is-a relationship and cannot inherit at will & gt;

5、 Interface isolation principle (ISP)

1. Clients should not be forced to rely on methods they do not use
2. The interface should be small and complete
< The external permissions of the interface should be strictly managed to be small and complete & gt;

6、 Object composition takes precedence over class inheritance

1. Class inheritance is usually “white box reuse”, and object combination is usually “black box reuse”
2. Inheritance destroys encapsulation to some extent. Child and parent classes are highly coupled < / strong >
3. Object combination only requires that the combined objects have well-defined interfaces, < strong > low coupling < / strong >
< Don’t rely too much on inheritance. Sometimes object composition may be a better choice & gt;

7、 Package change point

1. Use encapsulation to create boundary layers between objects, so that designers can modify one side of the boundary layer without adverse impact on the other side, so as to realize loose coupling between boundary layers
< Understand the change point and boundary layer and further loose coupling & gt;

8、 Programming for interfaces, not implementations

1. The variable type is not declared as a specific concrete class, but as an interface· The client program does not need to know the specific type of the object, but only the interface of the object
2. Reduce the dependency of each part of the system, so as to realize the type design scheme of “high cohesion and loose coupling”.
< Interface standardization! Multi purpose interface does not directly use internal data & gt;