PHP設計模式之工廠方法設計模式實例分析

來源:http://news.mkq.online/ 作者:牛站新聞

本文實例講述了PHP設計模式之工廠方法設計模式。分享給大家供大家參考,具體如下:

一、什麼是工廠方法模式

作爲一種創建型設計模式,工廠方法模式就是要創建“某種東西”。對於工廠方法,要創建的“東西”是一個產品,這個產品與創建它的類之間不存在綁定。實際上,爲了保持這種鬆耦合,客戶會通過一個工廠發出請求,再由工廠創建所請求的產品。利用工廠方法模式,請求者只發出請求,而不具體創建產品。

二、什麼時候使用工廠方法模式

如果實例化對象的子類可能改變,就要使用工廠方法模式。

三、一般工廠方法模式

使用一般工廠方法模式時,客戶只包含工廠的引用,一個工廠生產一種產品。增加一種產品的同時需要增加一個新工廠類和一個新產品類。
01
<?php
02
/**
03

  • 一般工廠方法設計模式
    04
    */
    05
    //工廠抽象類
    06
    abstract class Factory
    07
    {
    08
    protected abstract function produce();
    09
    public function startFactory()
    10
    {
    11
    $pro = $this->produce();
    12
    return $pro;
    13
    }
    14
    }
    15
    //文本工廠
    16
    class TextFactory extends Factory
    17
    {
    18
    protected function produce()
    19
    {
    20
    $textProduct = new TextProduct();
    21
    return $textProduct->getProperties();
    22
    }
    23
    }
    24
    //圖像工廠
    25
    class ImageFactory extends Factory
    26
    {
    27
    protected function produce()
    28
    {
    29
    $imageProduct = new ImageProduct();
    30
    return $imageProduct->getProperties();
    31
    }
    32
    }
    33
    //產品類接口
    34
    interface Product
    35
    {
    36
    public function getProperties();
    37
    }
    38
    //文本產品
    39
    class TextProduct implements Product
    40
    {
    41
    private $text;
    42
    function getProperties()
    43
    {
    44
    $this->text = "此處爲文本";
    45
    return $this->text;
    46
    }
    47
    }
    48
    //圖像產品
    49
    class ImageProduct implements Product
    50
    {
    51
    private $image;
    52
    function getProperties()
    53
    {
    54
    $this->image = "此處爲圖像";
    55
    return $this->image;
    56
    }
    57
    }
    58
    //客戶類
    59
    class Client
    60
    {
    61
    private $textFactory;
    62
    private $imageFactory;
    63
    public function __construct()
    64
    {
    65
    $this->textFactory = new TextFactory();
    66
    echo $this->textFactory->startFactory() . '<br />';
    67
    $this->imageFactory = new ImageFactory();
    68
    echo $this->imageFactory->startFactory() . '<br />';
    69
    }
    70
    }
    71
    $client = new Client();
    72
    /
    運行結果:
    73
    此處爲文本
    74
    此處爲圖像
    75
    */
    76
    ?>

四、參數化工廠方法模式

使用參數化工廠方法模式時,客戶包含工廠和產品的引用,發出請求時需要指定產品的種類,一個工廠生產多種產品。增加一種產品時只需要增加一個新產品類即可。
view source
print
?
01
<?php
02
/**
03

  • 參數化工廠方法設計模式
    04
    */
    05
    //工廠抽象類
    06
    abstract class Factory
    07
    {
    08
    protected abstract function produce(Product $product);
    09
    public function startFactory(Product $product)
    10
    {
    11
    $pro = $this->produce($product);
    12
    return $pro;
    13
    }
    14
    }
    15
    //工廠實現
    16
    class ConcreteFactory extends Factory
    17
    {
    18
    protected function produce(Product $product)
    19
    {
    20
    return $product->getProperties();
    21
    }
    22
    }
    23
    //產品類接口
    24
    interface Product
    25
    {
    26
    public function getProperties();
    27
    }
    28
    //文本產品
    29
    class TextProduct implements Product
    30
    {
    31
    private $text;
    32
    public function getProperties()
    33
    {
    34
    $this->text = "此處爲文本";
    35
    return $this->text;
    36
    }
    37
    }
    38
    //圖像產品
    39
    class ImageProduct implements Product
    40
    {
    41
    private $image;
    42
    public function getProperties()
    43
    {
    44
    $this->image = "此處爲圖像";
    45
    return $this->image;
    46
    }
    47
    }
    48
    //客戶類
    49
    class Client
    50
    {
    51
    private $factory;
    52
    private $textProduct;
    53
    private $imageProduct;
    54
    public function __construct()
    55
    {
    56
    $factory = new ConcreteFactory();
    57
    $textProduct = new TextProduct();
    58
    $imageProduct = new ImageProduct();
    59
    echo $factory->startFactory($textProduct) . '<br />';
    60
    echo $factory->startFactory($imageProduct) . '<br />';
    61
    }
    62
    }
    63
    $client = new Client();
    64
    /
    運行結果:
    65
    此處爲文本
    66
    此處爲圖像
    67
    */
    68
    ?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章