设计模式-抽象工厂
作者:edwin
日期:2019-08-15 20:12:50
所属分类:后端 - php

这种模式提供了一个接口来创建一系列相关或相互依赖对象而无需指定它们具体的类

Client(客户端)
This class uses only interfaces declared by AbstractFactory and AbstractProduct classes.
(这个类只使用AbstractFactory和AbstractProduct类声明的接口。)

ProductB1(产品B1)
This class (a) defines a product object to be created by the corresponding concrete factory, and (b) implements the AbstractProduct interface.
(这类(a)定义了一个由相应的具体工厂创建的产品对象,和(b)实现了AbstractProduct接口。)

ProductB2(产品B2)
This class (a) defines a product object to be created by the corresponding concrete factory, and (b) implements the AbstractProduct interface.
(这类(a)定义了一个由相应的具体工厂创建的产品对象,和(b)实现了AbstractProduct接口。)

AbstractProductB(抽像产品B)
This class declares an interface for a type of product object.
(这类声明一个产品对象类型的接口。)

ProductA1(产品A1)
This class (a) defines a product object to be created by the corresponding concrete factory, and (b) implements the AbstractProduct interface.
(这类(a)定义了一个由相应的具体工厂创建的产品对象,和(b)实现了AbstractProduct接口。)

ProductA2(产品A2)
This class (a) defines a product object to be created by the corresponding concrete factory, and (b) implements the AbstractProduct interface.
(这类(a)定义了一个由相应的具体工厂创建的产品对象,和(b)实现了AbstractProduct接口。)

AbstractProductA(抽像产品A)
This class declares an interface for a type of product object.
(这类声明一个产品对象类型的接口。)

ConcreateFactory2(具体工厂2)
This class implements the operations to create concrete product objects.
(这个类实现了操作来创建具体产品对象。)

ConcreateFactory1(具体工厂1)
This class implements the operations to create concrete product objects.
(这个类实现了操作来创建具体产品对象。)

AbstractFactory(抽像工厂)
This class declares an interface for operations that create abstract product objects.
(这类声明一个接口用于创建抽象产品对象的操作。)

代码实现

<?php

class Client {
 
     /**
     * Main program.
     */
    public static function main() {
        self::run(new ConcreteFactory1());
        self::run(new ConcreteFactory2());
    }
 
    /**
     * 调用工厂实例生成产品,输出产品名
     * @param   $factory    AbstractFactory     工厂实例
     */
    public static function run(AbstractFactory $factory) {
        $productA = $factory->createProductA();
        $productB = $factory->createProductB();
        echo $productA->getName(), '';
        echo $productB->getName(), '';
    }
 
}


class ProductB1 implements AbstractProductB {
    private $_name;
 
    public function __construct() {
        $this->_name = 'product B1';
    }
 
    public function getName() {
        return $this->_name;
    }
}


class ProductB2 implements AbstractProductB {
    private $_name;
 
    public function __construct() {
        $this->_name = 'product B2';
    }
 
    public function getName() {
        return $this->_name;
    }
}

interface AbstractProductB {
    public function getName();
}

class ProductA1 implements AbstractProductA {
    private $_name;
 
    public function __construct() {
        $this->_name = 'product A1';
    }
 
    public function getName() {
        return $this->_name;
    }
}


class ProductA2 implements AbstractProductA {
    private $_name;
 
    public function __construct() {
        $this->_name = 'product A2';
    }
 
    public function getName() {
        return $this->_name;
    }
}

interface AbstractProductA {
 
    /**
     * 取得产品名
     */
    public function getName();
}

class ConcreteFactory1 implements AbstractFactory{
 
    public function createProductA() {
        return new ProductA1();
    }
 
    public function createProductB() {
        return new ProductB1();
    }
}

 

class ConcreteFactory2 implements AbstractFactory{
 
    public function createProductA() {
        return new ProductA2();
    }
 
    public function createProductB() {
        return new ProductB2();
    }
}

interface AbstractFactory {
    /**
     * 创建等级结构为A的产品的工厂方法
     */
    public function createProductA();
 
     /**
     * 创建等级结构为B的产品的工厂方法
     */
    public function createProductB();
 
}

评论

全部评论 / 0