什么是设计模式?

设计模式是一套被 反复使用多数人知晓经过分类编目的代码设计经验的总结

为什么要用设计模式?

为了 可重用 代码,让代码更容易的被他人理解并保证代码的 可靠性

head.jpeg

谁提出了设计模式概念?

四人组( Gang of Four ),简称 GoF ,分别是 Erich Gamma, Richard Helm , Ralph Johnson John Vlissides 。但不是他们设计的,是他们在 95年的时候整理归纳 23 种最常用的设计模式并会变成一本书 Design Patterns: Elements of Reusable Object-Oriented Software 就是:《设计模式:可复用面向对象软件的基础》 。是经过时间的验证的,大家一定要学会。

虽然GoF设计模式只有23个,但是它们各具特色,每个模式都为某一个可重复的设计问题提供了一套解决方案。

根据他们的用途:设计模型可以分为三种:

  • 创建型(Creational)5种
  • 结构型(Structural)7种
  • 行为型(Behavioral)11种

创建型模式主要用于描述如何创建对象
结构型模式主要用于描述如何实现类或对象的组合
行为型模式主要用于描述类或对象怎样交互以及怎样分配职责

类型 模式名称 学习难度 使用频率
创建型模式
Creational Pattern
单例模式
Singleton Pattern
★☆☆☆☆ ★★★★☆
简单工厂模式
Simple Factory Pattern
★★☆☆☆ ★★★☆☆
工厂方法模式
Factory Method Pattern
★★☆☆☆ ★★★★★
抽象工厂模式
Abstract Factory Pattern
★★★★☆ ★★★★★
原型模式
Prototype Pattern
★★★☆☆ ★★★☆☆
建造者模式
Builder Pattern
★★★★☆ ★★☆☆☆
结构型模式
Structural Pattern
适配器模式
Adapter Pattern
★★☆☆☆ ★★★★☆
桥接模式
Bridge Pattern
★★★☆☆ ★★★☆☆
组合模式
Composite Pattern
★★★☆☆ ★★★★☆
装饰模式
Decorator Pattern
★★★☆☆ ★★★☆☆
外观模式
Façade Pattern
★☆☆☆☆ ★★★★★
享元模式
Flyweight Pattern
★★★★☆ ★☆☆☆☆
代理模式
Proxy Pattern
★★★☆☆ ★★★★☆
行为型模式
Behavioral Pattern
职责链模式
Chain of Responsibility Pattern
★★★☆☆ ★★☆☆☆
命令模式
Command Pattern
★★★☆☆ ★★★★☆
解释器模式
Interpreter Pattern
★★★★★ ★☆☆☆☆
迭代器模式
Iterator Pattern
★★★☆☆ ★★★★★
中介者模式
Mediator Pattern
★★★☆☆ ★★☆☆☆
备忘录模式
Memento Pattern
★★☆☆☆ ★★☆☆☆
观察者模式
Observer Pattern
★★★☆☆ ★★★★★
状态模式
State Pattern
★★★☆☆ ★★★☆☆
策略模式
Strategy Pattern
★☆☆☆☆ ★★★★☆
模板方法模式
Template Method Pattern
★★☆☆☆ ★★★☆☆
访问者模式
Visitor Pattern
★★★★☆ ★☆☆☆☆