简介:
设计模式是开发人员在开发过程中针对某一类问题的解决方案。
面向对象设计原则:
1. 对接口编程而不是对实现编程
2. 优先使用对象组合而不是继承
设计模式类型:
共有23种设计模式,分为三大类:创建型模式、结构型模式、行为型模式
序号 | 描述 | 包括 |
---|---|---|
1 | 创建者模式:这些模式提供了一种在创建 对象时隐藏创建逻辑而不是直接new对象。 这样程序对判读针对某个实例需要创建哪 些对象更加灵活 |
- 工厂模式(Factory Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 建造者模式(Bulider Pattern) - 原型模式(Prototype Pattern) |
2 | 结构型模式:这些设计模式关注对象和类 的组合。继承的方式被用来组合接口和定 义组合对象获得新功能的方式。 |
- 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 过滤器模式(Filter、Criteria Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) |
3 | 行为型模式:关注对象之间的通信 |
- 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 空对象模式(Null Object Pattern) - 策略模式(Strategy Pattern) - 模板模式(Template Pattern) - 访问者模式(Visitor Pattern) |
设计模式的六大原则:
1. 开闭原则(Open Close Principle)
对扩展开发,对修改关闭。在程序扩展的时候,不能去修改原有代码,实现一个热拔插的效果。所以我们需要用接口和抽象类。
2.里氏代换原则(Liskov Substitution Principle)
任何基类出现的地方,子类一定可以出现。
3.依赖倒转原则(Dependence Inversion Principle)
开闭原则的基础,针对接口编程,依赖于抽象而不依赖于具体。
4.接口隔离原则(Interface Segregation Principle)
使用多个隔离的接口而不是单个接口,降低类之间的耦合度。
5.迪米特法则,又称最少知道原则(Demeter Principle)
一个类尽量少与其他类发生联系,使功能模块相互独立。
6.合成复用原则(Composite Reuse Principle)
尽量使用合成/聚合方式,而不是继承。