天天看點

23種設計模式【Java】全案例解析

具體解析

建立型

  • 單例模式
  • 簡單工廠
  • 工廠方法
  • 抽象工廠
  • 建造者模式
  • 原型模式

結構型

  • 外觀模式
  • 擴充卡模式
  • 橋接模式
  • 組合模式
  • 裝飾模式
  • 享元模式
  • 代理模式

行為型

  • 模闆方法
  • 責任鍊
  • 狀态模式
  • 政策模式
  • 指令模式
  • 觀察者模式
  • 備忘錄模式
  • 中介者模式
  • 疊代器模式
  • 解釋器模式
  • 通路者模式

7個原則

  1. 單一職責原則【SINGLE RESPONSIBILITY PRINCIPLE】: 一個類負責一項職責。
  2. 裡氏替換原則【LISKOV SUBSTITUTION PRINCIPLE】: 繼承與派生的規則。
  3. 依賴倒置原則【DEPENDENCE INVERSION PRINCIPLE】: 高層子產品不應該依賴低層子產品,二者都應該依賴其抽象;抽象不應該依賴細節;細節應該依賴抽象。即針對接口程式設計,不要針對實作程式設計。
  4. 接口隔離原則【INTERFACE SEGREGATION PRINCIPLE】: 建立單一接口,不要建立龐大臃腫的接口,盡量細化接口,接口中的方法盡量少。
  5. 迪米特法則【LOW OF DEMETER】: 低耦合,高内聚。
  6. 開閉原則【OPEN CLOSE PRINCIPLE】: 一個軟體實體如類、子產品和函數應該對擴充開放,對修改關閉。
  7. 組合/聚合複用原則【Composition/Aggregation Reuse Principle(CARP) 】: 盡量使用組合和聚合少使用繼承的關系來達到複用的原則。

24大設計模式

簡單工廠一般不提,是以就是 23 種。

概覽圖

23種設計模式【Java】全案例解析

一覽表

類型 模式名稱 學習難度 使用頻率
建立型模式 Creational Pattern 單例模式 Singleton Pattern ★☆☆☆☆ ★★★★☆
建立型模式 Creational Pattern 簡單工廠模式 Simple Factory Pattern ★★☆☆☆ ★★★☆☆
建立型模式 Creational Pattern 工廠方法模式 Factory Method Pattern ★★☆☆☆ ★★★★★
建立型模式 Creational Pattern 抽象工廠模式 Abstract Factory Pattern ★★★★☆ ★★★★★
建立型模式 Creational Pattern 原型模式 Prototype Pattern ★★★☆☆ ★★★☆☆
建立型模式 Creational Pattern 建造者模式 Builder Pattern ★★★★☆ ★★☆☆☆
結構型模式 Structural Pattern 擴充卡模式 Adapter Pattern ★★☆☆☆ ★★★★☆
結構型模式 Structural Pattern 橋接模式 Bridge Pattern ★★★☆☆ ★★★☆☆
結構型模式 Structural Pattern 組合模式 Composite Pattern ★★★☆☆ ★★★★☆
結構型模式 Structural Pattern 裝飾模式 Decorator Pattern ★★★☆☆ ★★★☆☆
結構型模式 Structural Pattern 外觀模式 Façade Pattern ★☆☆☆☆ ★★★★★
結構型模式 Structural Pattern 享元模式 Flyweight Pattern ★★★★☆ ★☆☆☆☆
結構型模式 Structural Pattern 代理模式 Proxy Pattern ★★★☆☆ ★★★★☆
行為型模式 Behavioral Pattern 職責鍊模式 Chain of Responsibility Pattern ★★★☆☆ ★★☆☆☆
行為型模式 Behavioral Pattern 指令模式 Command Pattern ★★★☆☆ ★★★★☆
行為型模式 Behavioral Pattern 解釋器模式 Interpreter Pattern ★★★★★ ★☆☆☆☆
行為型模式 Behavioral Pattern 疊代器模式 Iterator Pattern ★★★☆☆ ★★★★★
行為型模式 Behavioral Pattern 中介者模式 Mediator Pattern ★★★☆☆ ★★☆☆☆
行為型模式 Behavioral Pattern 備忘錄模式 Memento Pattern ★★☆☆☆ ★★☆☆☆
行為型模式 Behavioral Pattern 觀察者模式 Observer Pattern ★★★☆☆ ★★★★★
行為型模式 Behavioral Pattern 狀态模式 State Pattern ★★★☆☆ ★★★☆☆
行為型模式 Behavioral Pattern 政策模式 Strategy Pattern ★☆☆☆☆ ★★★★☆
行為型模式 Behavioral Pattern 模闆方法模式 Template Method Pattern ★★☆☆☆ ★★★☆☆
行為型模式 Behavioral Pattern 通路者模式 Visitor Pattern ★★★★☆ ★☆☆☆☆

關聯關系

23種設計模式【Java】全案例解析

知識體系圖

23種設計模式【Java】全案例解析

參考

Java設計模式

圖說設計模式

Java設計模式