Home >Backend Development >PHP Tutorial >16 PHP design patterns introduced

16 PHP design patterns introduced

小云云
小云云Original
2018-03-22 10:50:565801browse

This article mainly shares with you an introduction to 16 PHP design patterns. The article involves some UML class diagrams. For a better understanding, you can read the UML class diagrams first. Hope it helps everyone.

1. Factory pattern

Factory pattern can be divided into three types of patterns: simple factory pattern, factory method pattern, abstract factory pattern;
1. Simple factory pattern

Also known as the Static Factory Method pattern, it belongs to the class creation pattern. In the simple factory pattern, instances of different classes can be returned according to different parameters. The simple factory pattern specifically defines a class to be responsible for creating instances of other classes. The created instances usually have a common parent class.

Role:

Factory类:负责创建具体产品的实例
Product类:抽象产品类,定义产品子类的公共接口
ConcreteProduct 类:具体产品类,实现Product父类的接口功能,也可添加自定义的功能

UML class diagram:

Sample code:

<?php //简单工厂模式class Cat{
  function __construct()
  {
      echo "I am Cat class <br>";
  }
}class Dog{
  function __construct()
  {
      echo "I am Dog class <br>";
  }
}class Factory{
  public static function CreateAnimal($name){
      if ($name == &#39;cat&#39;) {          return new Cat();
      } elseif ($name == &#39;dog&#39;) {          return new Dog();
      }
  }
}$cat = Factory::CreateAnimal(&#39;cat&#39;);$dog = Factory::CreateAnimal(&#39;dog&#39;);

The biggest advantage of the simple factory pattern is to implement objects The creation of the object is separated from the use of the object, and the creation of the object is handed over to a specialized factory class. However, its biggest disadvantage is that the factory class is not flexible enough. Adding new specific products requires modifying the judgment logic code of the factory class, and when there are many products, The factory method code will be very complex.
2. Factory method pattern

In this pattern, by defining an abstract core factory class and defining an interface for creating product objects, the work of creating specific product instances is delayed to its factory subclasses. The advantage of this is that the core class only focuses on the interface definition of the factory class, and specific product instances are left to the specific factory subclass to create. When the system needs to add a new product, there is no need to modify the existing system code. You only need to add a specific product class and its corresponding factory subclass. This makes the system more scalable and conforms to the opening and closing principle of object-oriented programming;

Role:

Product:抽象产品类
ConcreteProduct:具体产品类
Factory:抽象工厂类
ConcreteFactory:具体工厂类

UML class diagram:
16 PHP design patterns introduced
Write picture description here
Sample code:

<?php interface Animal{
  public function run();
  public function say();}class Cat implements Animal{
  public function run(){
      echo "I ran slowly <br>";
  }  public function say(){
      echo "I am Cat class <br>";
  }
}class Dog implements Animal{
  public function run(){
      echo "I&#39;m running fast <br>";
  }  public function say(){
      echo "I am Dog class <br>";
  }
}abstract class Factory{
  abstract static function createAnimal();}class CatFactory extends Factory{
  public static function createAnimal()
  {
      return new Cat();
  }
}class DogFactory extends Factory{
  public static function createAnimal()
  {
      return new Dog();
  }
}$cat = CatFactory::createAnimal();$cat->say();$cat->run();$dog = DogFactory::createAnimal();$dog->say();$dog->run();

Factory method pattern is simple Further abstraction and promotion of the factory pattern. Due to the use of object-oriented polymorphism, the factory method pattern maintains the advantages of the simple factory pattern and overcomes its disadvantages. In the factory method pattern, the core factory class is no longer responsible for the creation of all products, but leaves the specific creation work to subclasses. This core class is only responsible for giving the interface that a specific factory must implement, and is not responsible for the details of how the product class is instantiated. This allows the factory method pattern to allow the system to introduce new products without modifying the factory role.
3. Abstract Factory Pattern

Provides an interface for creating a series of related or interdependent objects without specifying their specific classes. Abstract Factory Pattern, also known as Kit Pattern, is an object creation pattern.

This pattern is a further extension of the factory method pattern. In the factory method pattern, a specific factory is responsible for producing a specific type of product, that is, a one-to-one relationship. However, if a specific factory is required to produce multiple product objects, then the abstract factory pattern needs to be used.

In order to facilitate the understanding of this pattern, two concepts are introduced here:

产品等级结构:产品等级结构即产品的继承结构,如一个抽象类是电视机,其子类有海尔电视机、海信电视机、TCL电视机,则抽象电视机与具体品牌的电视机之间构成了一个产品等级结构,抽象电视机是父类,而具体品牌的电视机是其子类。
产品族 :在抽象工厂模式中,产品族是指由同一个工厂生产的,位于不同产品等级结构中的一组产品,如海尔电器工厂生产的海尔电视机、海尔电冰箱,海尔电视机位于电视机产品等级结构中,海尔电冰箱位于电冰箱产品等级结构中。

Role:

抽象工厂(AbstractFactory):担任这个角色的是抽象工厂模式的核心,是与应用系统的商业逻辑无关的。
具体工厂(Factory):这个角色直接在客户端的调用下创建产品的实例,这个角色含有选择合适的产品对象的逻辑,而这个逻辑是与应用系统商业逻辑紧密相关的。
抽象产品(AbstractProduct):担任这个角色的类是抽象工厂模式所创建的对象的父类,或它们共同拥有的接口
具体产品(Product):抽象工厂模式所创建的任何产品对象都是一个具体的产品类的实例。

UML class diagram:
16 PHP design patterns introduced
16 PHP design patterns introduced
Sample code:

<?php interface TV{
  public function open();
  public function use();}class HaierTv implements TV{
  public function open()
  {
      echo "Open Haier TV <br>";
  }  public function use()
  {
      echo "I&#39;m watching TV <br>";
  }
}interface PC{
  public function work();
  public function play();}class LenovoPc implements PC{
  public function work()
  {
      echo "I&#39;m working on a Lenovo computer <br>";
  }  public function play()
  {
      echo "Lenovo computers can be used to play games <br>";
  }
}abstract class Factory{
  abstract public static function createPc();
  abstract public static function createTv();}class ProductFactory extends Factory{
  public static function createTV()
  {
      return new HaierTv();
  }  public static function createPc()
  {
      return new LenovoPc();
  }
}$newTv = ProductFactory::createTV();$newTv->open();$newTv->use();$newPc = ProductFactory::createPc();$newPc->work();$newPc->play();

2. Builder pattern

Also known as: generator pattern, it is an object construction pattern. It can abstract the construction process of complex objects (abstract category), so that different implementation methods of this abstract process can construct objects with different performances (attributes).

Builder pattern is to create a complex object step by step. It allows users to build complex objects only by specifying their type and content. Users do not need to know the specific internal construction details. For example, a car is composed of wheels, engines and other parts. For ordinary people, we only use a complete car. At this time, we need to add a constructor to help us assemble these components in order. Become a complete car.

Role:

Builder:抽象构造者类,为创建一个Product对象的各个部件指定抽象接口。
ConcreteBuilder:具体构造者类,实现Builder的接口以构造和装配该产品的各个部件。定义并明确它所创建的表示。提供一个检索产品的接口
Director:指挥者,构造一个使用Builder接口的对象。
Product:表示被构造的复杂对象。ConcreateBuilder创建该产品的内部表示并定义它的装配过程。
包含定义组成部件的类,包括将这些部件装配成最终产品的接口。

UML class diagram:
16 PHP design patterns introduced

Sample code:

<?php /**
* chouxiang builer
*/abstract class Builder{
  protected $car;  abstract public function buildPartA();
  abstract public function buildPartB();
  abstract public function buildPartC();
  abstract public function getResult();}class CarBuilder extends Builder{
  function __construct()
  {
      $this->car = new Car();
  }  public function buildPartA(){
      $this->car->setPartA(&#39;发动机&#39;);
  }  public function buildPartB(){
      $this->car->setPartB(&#39;轮子&#39;);
  }  public function buildPartC(){
      $this->car->setPartC(&#39;其他零件&#39;);
  }  public function getResult(){
      return $this->car;
  }
}class Car{
  protected $partA;  protected $partB;  protected $partC;  public function setPartA($str){
      $this->partA = $str;
  }  public function setPartB($str){
      $this->partB = $str;
  }  public function setPartC($str){
      $this->partC = $str;
  }  public function show()
  {
      echo "这辆车由:".$this->partA.&#39;,&#39;.$this->partB.&#39;,和&#39;.$this->partC.&#39;组成&#39;;
  }
}class Director{
  public $myBuilder;  public function startBuild()
  {
      $this->myBuilder->buildPartA();      $this->myBuilder->buildPartB();      $this->myBuilder->buildPartC();      return $this->myBuilder->getResult();
  }  public function setBuilder(Builder $builder)
  {
      $this->myBuilder = $builder;
  }
}$carBuilder = new CarBuilder();$director = new Director();$director->setBuilder($carBuilder);$newCar = $director->startBuild();$newCar->show();

3. Singleton pattern

单例模式,也叫单子模式,是一种常用的软件设计模式。在应用这个模式时,单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为。

实现单例模式的思路是:一个类能返回对象一个引用(永远是同一个)和一个获得该实例的方法(必须是静态方法,通常使用getInstance这个名称);当我们调用这个方法时,如果类持有的引用不为空就返回这个引用,如果类保持的引用为空就创建该类的实例并将实例的引用赋予该类保持的引用;同时我们还将该类的构造函数定义为私有方法,这样其他处的代码就无法通过调用该类的构造函数来实例化该类的对象,只有通过该类提供的静态方法来得到该类的唯一实例。

---维基百科

The key points of the singleton pattern are: a class can only have one instance; it must create its own instance; it must provide this instance to the entire system by itself. Singleton pattern is an object creation pattern.

Role:

Singleton:单例类

UML Class Diagram:
16 PHP design patterns introduced

Sample Code:

<?php class Singleton{
  private static $instance;  //私有构造方法,禁止使用new创建对象
  private function __construct(){}  public static function getInstance(){
      if (!isset(self::$instance)) {          self::$instance = new self;
      }      return self::$instance;
  }  //将克隆方法设为私有,禁止克隆对象
  private function __clone(){}  public function say()
  {
      echo "这是用单例模式创建对象实例 <br>";
  }  public function operation()
  {
      echo "这里可以添加其他方法和操作 <br>";
  }
}// $shiyanlou = new Singleton();$shiyanlou = Singleton::getInstance();$shiyanlou->say();$shiyanlou->operation();$newShiyanlou = Singleton::getInstance();
var_dump($shiyanlou === $newShiyanlou);

上述的五个模式均属于创建型模式,关于结构型模式,
更多请看实验楼教程【16个PHP设计模式详解】

文中涉及一些 UML 类图,为了更好地理解,可以先阅读 UML 类图。

一、工厂模式

工厂模式具体可分为三类模式:简单工厂模式,工厂方法模式,抽象工厂模式;
1.简单工厂模式

又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式。在简单工厂模式中,可以根据参数的不同返回不同类的实例。简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。

角色:

Factory类:负责创建具体产品的实例
Product类:抽象产品类,定义产品子类的公共接口
ConcreteProduct 类:具体产品类,实现Product父类的接口功能,也可添加自定义的功能

UML类图:

示例代码:

<?php //简单工厂模式class Cat{
  function __construct()
  {
      echo "I am Cat class <br>";
  }
}class Dog{
  function __construct()
  {
      echo "I am Dog class <br>";
  }
}class Factory{
  public static function CreateAnimal($name){
      if ($name == &#39;cat&#39;) {          return new Cat();
      } elseif ($name == &#39;dog&#39;) {          return new Dog();
      }
  }
}$cat = Factory::CreateAnimal(&#39;cat&#39;);$dog = Factory::CreateAnimal(&#39;dog&#39;);

简单工厂模式最大的优点在于实现对象的创建和对象的使用分离,将对象的创建交给专门的工厂类负责,但是其最大的缺点在于工厂类不够灵活,增加新的具体产品需要修改工厂类的判断逻辑代码,而且产品较多时,工厂方法代码将会非常复杂。
2.工厂方法模式

此模式中,通过定义一个抽象的核心工厂类,并定义创建产品对象的接口,创建具体产品实例的工作延迟到其工厂子类去完成。这样做的好处是核心类只关注工厂类的接口定义,而具体的产品实例交给具体的工厂子类去创建。当系统需要新增一个产品是,无需修改现有系统代码,只需要添加一个具体产品类和其对应的工厂子类,是系统的扩展性变得很好,符合面向对象编程的开闭原则;

角色:

Product:抽象产品类
ConcreteProduct:具体产品类
Factory:抽象工厂类
ConcreteFactory:具体工厂类

UML类图:
16 PHP design patterns introduced
16 PHP design patterns introduced
示例代码:

<?php interface Animal{
  public function run();
  public function say();}class Cat implements Animal{
  public function run(){
      echo "I ran slowly <br>";
  }  public function say(){
      echo "I am Cat class <br>";
  }
}class Dog implements Animal{
  public function run(){
      echo "I&#39;m running fast <br>";
  }  public function say(){
      echo "I am Dog class <br>";
  }
}abstract class Factory{
  abstract static function createAnimal();}class CatFactory extends Factory{
  public static function createAnimal()
  {
      return new Cat();
  }
}class DogFactory extends Factory{
  public static function createAnimal()
  {
      return new Dog();
  }
}$cat = CatFactory::createAnimal();$cat->say();$cat->run();$dog = DogFactory::createAnimal();$dog->say();$dog->run();

工厂方法模式是简单工厂模式的进一步抽象和推广。由于使用了面向对象的多态性,工厂方法模式保持了简单工厂模式的优点,而且克服了它的缺点。在工厂方法模式中,核心的工厂类不再负责所有产品的创建,而是将具体创建工作交给子类去做。这个核心类仅仅负责给出具体工厂必须实现的接口,而不负责产品类被实例化这种细节,这使得工厂方法模式可以允许系统在不修改工厂角色的情况下引进新产品。
3.抽象工厂模式

提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。抽象工厂模式又称为Kit模式,属于对象创建型模式。

此模式是对工厂方法模式的进一步扩展。在工厂方法模式中,一个具体的工厂负责生产一类具体的产品,即一对一的关系,但是,如果需要一个具体的工厂生产多种产品对象,那么就需要用到抽象工厂模式了。

为了便于理解此模式,这里介绍两个概念:

产品等级结构:产品等级结构即产品的继承结构,如一个抽象类是电视机,其子类有海尔电视机、海信电视机、TCL电视机,则抽象电视机与具体品牌的电视机之间构成了一个产品等级结构,抽象电视机是父类,而具体品牌的电视机是其子类。
产品族 :在抽象工厂模式中,产品族是指由同一个工厂生产的,位于不同产品等级结构中的一组产品,如海尔电器工厂生产的海尔电视机、海尔电冰箱,海尔电视机位于电视机产品等级结构中,海尔电冰箱位于电冰箱产品等级结构中。

角色:

抽象工厂(AbstractFactory):担任这个角色的是抽象工厂模式的核心,是与应用系统的商业逻辑无关的。
具体工厂(Factory):这个角色直接在客户端的调用下创建产品的实例,这个角色含有选择合适的产品对象的逻辑,而这个逻辑是与应用系统商业逻辑紧密相关的。
抽象产品(AbstractProduct):担任这个角色的类是抽象工厂模式所创建的对象的父类,或它们共同拥有的接口
具体产品(Product):抽象工厂模式所创建的任何产品对象都是一个具体的产品类的实例。

UML类图:
16 PHP design patterns introduced
16 PHP design patterns introduced
示例代码:

<?php interface TV{
  public function open();
  public function use();}class HaierTv implements TV{
  public function open()
  {
      echo "Open Haier TV <br>";
  }  public function use()
  {
      echo "I&#39;m watching TV <br>";
  }
}interface PC{
  public function work();
  public function play();}class LenovoPc implements PC{
  public function work()
  {
      echo "I&#39;m working on a Lenovo computer <br>";
  }  public function play()
  {
      echo "Lenovo computers can be used to play games <br>";
  }
}abstract class Factory{
  abstract public static function createPc();
  abstract public static function createTv();}class ProductFactory extends Factory{
  public static function createTV()
  {
      return new HaierTv();
  }  public static function createPc()
  {
      return new LenovoPc();
  }
}$newTv = ProductFactory::createTV();$newTv->open();$newTv->use();$newPc = ProductFactory::createPc();$newPc->work();$newPc->play();

二、建造者模式

又名:生成器模式,是一种对象构建模式。它可以将复杂对象的建造过程抽象出来(抽象类别),使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象。

建造者模式是一步一步创建一个复杂的对象,它允许用户只通过指定复杂对象的类型和内容就可以构建它们,用户不需要知道内部的具体构建细节。例如,一辆汽车由轮子,发动机以及其他零件组成,对于普通人而言,我们使用的只是一辆完整的车,这时,我们需要加入一个构造者,让他帮我们把这些组件按序组装成为一辆完整的车。

角色:

Builder:抽象构造者类,为创建一个Product对象的各个部件指定抽象接口。
ConcreteBuilder:具体构造者类,实现Builder的接口以构造和装配该产品的各个部件。定义并明确它所创建的表示。提供一个检索产品的接口
Director:指挥者,构造一个使用Builder接口的对象。
Product:表示被构造的复杂对象。ConcreateBuilder创建该产品的内部表示并定义它的装配过程。
包含定义组成部件的类,包括将这些部件装配成最终产品的接口。

UML类图:
16 PHP design patterns introduced

示例代码:

<?php /**
* chouxiang builer
*/abstract class Builder{
  protected $car;  abstract public function buildPartA();
  abstract public function buildPartB();
  abstract public function buildPartC();
  abstract public function getResult();}class CarBuilder extends Builder{
  function __construct()
  {
      $this->car = new Car();
  }  public function buildPartA(){
      $this->car->setPartA(&#39;发动机&#39;);
  }  public function buildPartB(){
      $this->car->setPartB(&#39;轮子&#39;);
  }  public function buildPartC(){
      $this->car->setPartC(&#39;其他零件&#39;);
  }  public function getResult(){
      return $this->car;
  }
}class Car{
  protected $partA;  protected $partB;  protected $partC;  public function setPartA($str){
      $this->partA = $str;
  }  public function setPartB($str){
      $this->partB = $str;
  }  public function setPartC($str){
      $this->partC = $str;
  }  public function show()
  {
      echo "这辆车由:".$this->partA.&#39;,&#39;.$this->partB.&#39;,和&#39;.$this->partC.&#39;组成&#39;;
  }
}class Director{
  public $myBuilder;  public function startBuild()
  {
      $this->myBuilder->buildPartA();      $this->myBuilder->buildPartB();      $this->myBuilder->buildPartC();      return $this->myBuilder->getResult();
  }  public function setBuilder(Builder $builder)
  {
      $this->myBuilder = $builder;
  }
}$carBuilder = new CarBuilder();$director = new Director();$director->setBuilder($carBuilder);$newCar = $director->startBuild();$newCar->show();

三、单例模式

单例模式,也叫单子模式,是一种常用的软件设计模式。在应用这个模式时,单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为。

实现单例模式的思路是:一个类能返回对象一个引用(永远是同一个)和一个获得该实例的方法(必须是静态方法,通常使用getInstance这个名称);当我们调用这个方法时,如果类持有的引用不为空就返回这个引用,如果类保持的引用为空就创建该类的实例并将实例的引用赋予该类保持的引用;同时我们还将该类的构造函数定义为私有方法,这样其他处的代码就无法通过调用该类的构造函数来实例化该类的对象,只有通过该类提供的静态方法来得到该类的唯一实例。

---维基百科

单例模式的要点有:某个类只能有一个实例;它必须自行创建本身的实例;它必须自行向整个系统提供这个实例。单例模式是一种对象创建型模式。

角色:

Singleton:单例类

UML 类图:
16 PHP design patterns introduced

示例代码:

<?php class Singleton{
  private static $instance;  //私有构造方法,禁止使用new创建对象
  private function __construct(){}  public static function getInstance(){
      if (!isset(self::$instance)) {          self::$instance = new self;
      }      return self::$instance;
  }  //将克隆方法设为私有,禁止克隆对象
  private function __clone(){}  public function say()
  {
      echo "这是用单例模式创建对象实例 <br>";
  }  public function operation()
  {
      echo "这里可以添加其他方法和操作 <br>";
  }
}// $shiyanlou = new Singleton();$shiyanlou = Singleton::getInstance();$shiyanlou->say();$shiyanlou->operation();$newShiyanlou = Singleton::getInstance();
var_dump($shiyanlou === $newShiyanlou);

上述的五个模式均属于创建型模式,关于结构型模式。
相关推荐:

5种js设计模式

关于php设计模式-适配器的方法详解

JS设计模式之原型模式详解

The above is the detailed content of 16 PHP design patterns introduced. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn