Home  >  Article  >  Backend Development  >  Comparison of PHP simple factory pattern, factory method pattern and abstract factory pattern

Comparison of PHP simple factory pattern, factory method pattern and abstract factory pattern

高洛峰
高洛峰Original
2017-03-18 10:49:331406browse

PHP Factory PatternConcept: Factory pattern is a class that has certain methods that create objects for you. You can use a factory class to create objects without using new directly. This way, if you want to change the type of object created, you only need to change the factory. All code using this factory is automatically changed.
According to different levels of abstraction, PHP factory patterns are divided into: simple factory pattern, factory method pattern and abstract factory pattern

Simple factory pattern:

/**
 *简单工厂模式与工厂方法模式比较。
 *简单工厂又叫静态工厂方法模式,这样理解可以确定,简单工厂模式是通过一个静态方法创建对象的。 
 */
interface  people {
    function  jiehun();
}
class man implements people{
    function jiehun() {
        echo &#39;送玫瑰,送戒指!<br>&#39;;
    }
}
 
class women implements people {
    function jiehun() {
        echo &#39;穿婚纱!<br>&#39;;
    }
}
 
class SimpleFactoty {
    // 简单工厂里的静态方法
    static function createMan() {
        return new     man;
    }
    static function createWomen() {
        return new     women;
    }
    
}
 
$man = SimpleFactoty::createMan();
$man->jiehun();
$man = SimpleFactoty::createWomen();
$man->jiehun();

Factory method pattern:

<?php
/*
 *工厂方法模式:
 *定义一个创建对象的接口,让子类决定哪个类实例化。 他可以解决简单工厂模式中的封闭开放原则问题。<www.phpddt.com整理>
 */
interface  people {
    function  jiehun();
}
class man implements people{
    function jiehun() {
        echo &#39;送玫瑰,送戒指!<br>&#39;;
    }
}
 
class women implements people {
    function jiehun() {
        echo &#39;穿婚纱!<br>&#39;;
    }
}
 
interface  createMan {  // 注意了,这里是简单工厂本质区别所在,将对象的创建抽象成一个接口。
    function create();
 
}
class FactoryMan implements createMan{
    function create() {
        return  new man;
    }
}
class FactoryWomen implements createMan {
    function create() {
        return new women;
    }
}
 
class  Client {
    // 简单工厂里的静态方法
    function test() {
        $Factory =  new  FactoryMan;
        $man = $Factory->create();
        $man->jiehun();
        
        $Factory =  new  FactoryWomen;
        $man = $Factory->create();
        $man->jiehun();
    }
}
 
$f = new Client;
$f->test();

Abstract factory pattern:

<?php
/*
抽象工厂:提供一个创建一系列相关或相互依赖对象的接口。 
注意:这里和工厂方法的区别是:一系列,而工厂方法则是一个。
那么,我们是否就可以想到在接口create里再增加创建“一系列”对象的方法呢?
*/
interface  people {
    function  jiehun();
}
class Oman implements people{
    function jiehun() {
        echo &#39;美女,我送你玫瑰和戒指!<br>&#39;;
    }
}
class Iman implements people{
    function jiehun() {
        echo &#39;我偷偷喜欢你<br>&#39;;
    }
}
 
class Owomen implements people {
    function jiehun() {
        echo &#39;我要穿婚纱!<br>&#39;;
    }
}
 
class Iwomen implements people {
    function jiehun() {
        echo &#39;我好害羞哦!!<br>&#39;;
    }
}
 
interface  createMan {  // 注意了,这里是本质区别所在,将对象的创建抽象成一个接口。
    function createOpen(); //分为 内敛的和外向的
    function createIntro(); //内向
 
}
class FactoryMan implements createMan{
    function createOpen() {
        return  new  Oman;
    }
    function createIntro() {
        return  new Iman;
    }
}
class FactoryWomen implements createMan {
    function createOpen() {
        return  new  Owomen;
    }
    function createIntro() {
        return  new Iwomen;
    }
}
 
class  Client {
    // 简单工厂里的静态方法
    function test() {
        $Factory =  new  FactoryMan;
        $man = $Factory->createOpen();
        $man->jiehun();
        
        $man = $Factory->createIntro();
        $man->jiehun();
        
        
        $Factory =  new  FactoryWomen;
        $man = $Factory->createOpen();
        $man->jiehun();
        
        $man = $Factory->createIntro();
        $man->jiehun();
        
    }
}
 
$f = new Client;
$f->test();

Difference:

Simple factory mode: used to produce any product in the same hierarchical structure. There is nothing you can do about adding new products

Factory mode: used to produce fixed products in the same hierarchical structure. (Supports adding any product)
Abstract factory: used to produce all products of different product families. (There is nothing you can do about adding new products; adding product families is supported)

The above three factory methods have different levels of support in the two directions of hierarchical structure and product family. So consider which method should be used according to the situation

Scope of application:

Simple factory pattern:

The factory class is responsible for creating fewer objects. The customer only knows the parameters passed into the factory class and does not care about how to create the object.

Factory Method Pattern:

When a class does not know the class of the object it must create or when a class wants a subclass to specify the object it creates, when the class delegates the responsibility of creating the object to multiple helper subclasses You can use the factory method pattern when you have one of these and you want to localize the information about which helper subclass is the delegate.

Abstract Factory Pattern:

A system should not depend on the details of how product class instances are created, composed and expressed. This is important for all forms of factory patterns. This system has more than one product family, and the system only consumes one of the product families. Products belonging to the same product family are used together, and this constraint must be reflected in the system design. The system provides a product class library, and all products appear with the same interface, so that the client does not depend on the implementation.

Whether it is a simple factory pattern, a factory pattern or an abstract factory pattern, they essentially extract the immutable parts and leave the variable parts as interfaces to achieve maximum reuse. Which design pattern is more suitable depends on specific business needs.

For more articles on the comparison of PHP simple factory pattern, factory method pattern and abstract factory pattern, please pay attention to the PHP Chinese website!

Related articles:

Detailed explanation of the specific code to implement the abstract factory pattern in Java

Abstract Factory Pattern of JAVA design pattern

PHP object-oriented development - Abstract Factory Pattern

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