# DesignPatternsDemo **Repository Path**: NauticalRoad/design-patterns-demo ## Basic Information - **Project Name**: DesignPatternsDemo - **Description**: 设计模式示例代码 - **Primary Language**: Java - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2021-03-25 - **Last Updated**: 2021-05-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # DesignPatternsDemo ### 介绍 设计模式示例代码 参考 https://www.bilibili.com/video/BV1G4411c7N4?p=38&spm_id_from=pageDriver --- ## 设计模式原则 #### 1.单一职责原则 (Single Responsibility Principle) 单一职责原则表示一个模块的组成元素之间的功能相关性。 从软件变化的角度来看,就一个类而言,应该仅有一个让它变化的原因;通俗地说,即一个类只负责一项职责。 #### 2.开放-关闭原则 (Open-Closed Principle) 开闭原则就是说对扩展开放,对修改关闭。在程序需要进行拓展的时候,不能去修改原有的代码,应实现一个热插拔的效果 #### 3.里氏替换原则 (Liskov Substitution Principle) 所有引用父类的地方必须能透明地使用其子类的对象。即尽量不要重写父类的方法,也尽量不要重载父类的方法。保证方法的实现是统一的 #### 4.依赖倒转原则 (Dependence Inversion Principle) 高层模块不应该依赖于底层模块 ;抽象不应该依赖于细节;细节可以依赖于抽象。核心思想是面向接口编程 #### 5.接口隔离原则 (Interface Segregation Principle) 一个类不应该依赖它不需要的接口;一个类对另一个类的依赖应该建立在最小的接口上。 #### 6.迪米特法则(Law Of Demeter) 一个对象应该对其他对象保持最少的了解,只与直接的朋友(出现在成员变量,方法的输入输出参数中的类)通信,核心:类间解耦,弱耦合,提高类的复用 #### 7.组合/聚合复用原则 (Composite/Aggregate Reuse Principle) 尽量使用对象组合,而不是继承来达到复用的目的 --- ## 设计模式的类型 根据设计模式的参考书 Design Patterns - Elements of Reusable Object-Oriented Software(中文译名:设计模式 - 可复用的面向对象软件元素) 中所提到的,总共有 23 种设计模式。 这些模式可以分为三大类: - 创建型模式(Creational Patterns) 这些设计模式提供了一种在创建对象的同时隐藏创建逻辑的方式,而不是使用 new 运算符直接实例化对象。这使得程序在判断针对某个给定实例需要创建哪些对象时更加灵活。 - 结构型模式(Structural Patterns) 这些设计模式关注类和对象的组合。继承的概念被用来组合接口和定义组合对象获得新功能的方式。 - 行为型模式(Behavioral Patterns) 这些设计模式特别关注对象之间的通信。 #### 1.创建型模式(Creational Patterns) - 工厂模式(Factory Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 建造者模式(Builder Pattern) - 原型模式(Prototype Pattern) #### 2.结构型模式(Structural Patterns) - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 过滤器模式(Filter、Criteria Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) #### 3.行为型模式(Behavioral Patterns) - 责任链模式(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)