PHP – 设计模式概述

一、概述

1、前言

设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的。

2、设计模式的提出者

在 1994 年,由 Erich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides 四人合著出版了一本名为 Design Patterns - Elements of Reusable Object-Oriented Software(中文译名:设计模式 - 可复用的面向对象软件元素) 的书,该书首次提到了软件开发中设计模式的概念。

四位作者合称 GOF(四人帮,全拼 Gang of Four)。

3、设计模式的基本设计原则

(1)对接口编程而不是对实现编程。

(2)优先使用对象组合而不是继承。

 

二、设计模式的类型

1、创建型模式

创建型设计模式用于处理对象的实例化

(1)简单工厂模式(Simple Factory)

(2)工厂模式(Factory Pattern)

(3)抽象工厂模式(Abstract Factory Pattern)

(4)静态工厂模式(Static Factory)

(5)单例模式(Singleton Pattern)

(6)原型模式(Prototype Pattern)

(7)多例模式(Multiton Pattern)

(8)对象池模式(Pool Pattern)

(9)建造者模式(Builder Pattern)

2、结构型模式

这些设计模式关注类和对象的组合。继承的概念被用来组合接口定义组合对象获得新功能的方式。

(1)适配器模式(Adapter Pattern)

(2)桥接模式(Bridge Pattern)

(3)组合模式(Composite Pattern)

(4)装饰器模式(Decorator Pattern)

(5)外观模式(Facade Pattern)

(6)代理模式(Proxy Pattern)

(7)数据映射模式(Data Mapper)

(8)依赖注入模式(Dependency Injection)

(9)流接口模式(Fluent Interface)

(10)注册模式(Registry Pattern)

3、行为型模式

该设计模式特别关注对象之间的通信。

(1)观察者模式(Observer Pattern)

(2)职责链模式(Chain of Responsibility Pattern)

(3)命令行模式(Command Pattern)

(4)迭代器模式(Iterator Pattern)

(5)策略模式(Strategy Pattern)

(4)状态模式(State Pattern)

(5)模板方法模式(Template Pattern)

(6)解释器模式(Interpreter Pattern)

(8)访问者模式(Visitor Pattern)

(9)中介者模式(Mediator Pattern)

(10)备忘录模式(Memento Pattern)