C++设计模式

一 分类
1 创建型
工厂方法模式(Factory Method)https://blog.csdn.net/xufeng0991/article/details/50267737
抽象工厂模式(Abstract Factory)https://blog.csdn.net/xufeng0991/article/details/50267763
创建者模式(Builder)https://blog.csdn.net/xufeng0991/article/details/50267803
原型模式(Prototype)https://blog.csdn.net/xufeng0991/article/details/50267833
单例模式(Singleton)https://blog.csdn.net/xufeng0991/article/details/50267853

2 结构型
外观模式(Facade)https://blog.csdn.net/xufeng0991/article/details/50324283
适配器模式(Adapter)https://blog.csdn.net/xufeng0991/article/details/50324689
代理模式(Proxy)https://blog.csdn.net/xufeng0991/article/details/50324843
装饰模式(Decorator)https://blog.csdn.net/xufeng0991/article/details/50324983
桥接模式(Bridge)https://blog.csdn.net/xufeng0991/article/details/50325071
组合模式(Composite)https://blog.csdn.net/xufeng0991/article/details/50325123
享元模式(Flyweight)https://blog.csdn.net/xufeng0991/article/details/50325311

3 行为型模式
模板方法模式(Template Method)https://blog.csdn.net/xufeng0991/article/details/52177327
观察者模式(Observer)https://blog.csdn.net/xufeng0991/article/details/52177351
状态模式(State)https://blog.csdn.net/xufeng0991/article/details/52177374
策略模式(Strategy)https://blog.csdn.net/xufeng0991/article/details/52177388
职责链模式(Chain of Responsibility)https://blog.csdn.net/xufeng0991/article/details/52177390
命令模式(Command)https://blog.csdn.net/xufeng0991/article/details/52177396
访问者模式(Visitor)https://blog.csdn.net/xufeng0991/article/details/52177407
中介者模式(Mediator)https://blog.csdn.net/xufeng0991/article/details/52177416
备忘录模式(Memento)https://blog.csdn.net/xufeng0991/article/details/52177421
迭代器模式(Iterator)https://blog.csdn.net/xufeng0991/article/details/52177439
解释器模式(Interpreter)https://blog.csdn.net/xufeng0991/article/details/52177439
二 原则
总原则:开闭原则(Open Close Principle)
开闭原则就是说对扩展开放,对修改关闭。在程序需要进行拓展的时候,不能去修改原有的代码,而是要扩展原有代码,实现一个热插拔的效果。所以一句话概括就是:为了使程序的扩展性好,易于维护和升级。想要达到这样的效果,我们需要使用接口和抽象类等。

1 单一职责原则
不要存在多于一个导致类变更的原因,也就是说每个类应该实现单一的职责,如若不然,就应该把类拆分。

2 里氏替换原则
里氏代换原则(Liskov Substitution Principle LSP)面向对象设计的基本原则之一。里氏代换原则中说,任何基类可以出现的地方,子类一定可以出现。LSP是继承复用的基石,只有当衍生类可以替换掉基类,软件单位的功能不受到影响时,基类才能真正被复用,而衍生类也能够在基类的基础上增加新的行为。里氏代换原则是对“开-闭”原则的补充。实现“开-闭”原则的关键步骤就是抽象化。而基类与子类的继承关系就是抽象化的具体实现,所以里氏代换原则是对实现抽象化的具体步骤的规范。—— From Baidu 百科
历史替换原则中,子类对父类的方法尽量不要重写和重载。因为父类代表了定义好的结构,通过这个规范的接口与外界交互,子类不应该随便破坏它。

3 依赖倒转原则
这个是开闭原则的基础,具体内容:面向接口编程,依赖于抽象而不依赖于具体。写代码时用到具体类时,不与具体类交互,而与具体类的上层接口交互。

4 接口隔离原则
这个原则的意思是:每个接口中不存在子类用不到却必须实现的方法,如果不然,就要将接口拆分。使用多个隔离的接口,比使用单个接口(多个接口方法集合到一个的接口)要好。

5 迪米特法则(最少知道原则)
就是说:一个类对自己依赖的类知道的越少越好。也就是说无论被依赖的类多么复杂,都应该将逻辑封装在方法的内部,通过public方法提供给外部。这样当被依赖的类变化时,才能最小的影响该类。
最少知道原则的另一个表达方式是:只与直接的朋友通信。类之间只要有耦合关系,就叫朋友关系。耦合分为依赖、关联、聚合、组合等。我们称出现为成员变量、方法参数、方法返回值中的类为直接朋友。局部变量、临时变量则不是直接的朋友。我们要求陌生的类不要作为局部变量出现在类中。

6 合成复用原则
原则是尽量首先使用合成/聚合的方式,而不是使用继承。
————————————————
版权声明:本文为CSDN博主「丿寒风」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xufeng0991/java/article/details/50267675

posted @ 2020-07-28 14:22  丶仰望星空  阅读(143)  评论(0编辑  收藏  举报