iT邦幫忙

第 12 屆 iThome 鐵人賽

1

本文同步更新於blog

情境:三隻小豬想要蓋房子

  • 首先是牆壁,可以選擇不同的建材
<?php

namespace App\PrototypePattern\Building;

class Wall
{
    /**
     * @var string
     */
    public $material;

    public function __construct(string $material)
    {
        $this->material = $material;
    }
}

  • 接著是房子
<?php

namespace App\PrototypePattern\Building;

use App\PrototypePattern\Building\Wall;

class Building
{
    /**
     * @var string
     */
    public $name;

    /**
     * @var Wall
     */
    public $wall;

    /**
     * @param Wall $wall
     * @param string $name
     */
    public function __construct(Wall $wall, string $name = 'unnamed')
    {
        $this->wall = $wall;
        $this->name = $name;
    }
}

  • 接著豬大哥用稻草 (straw),蓋了間稻草屋
<?php

namespace App\PrototypePattern\Building;

use App\PrototypePattern\Building\Building;
use App\PrototypePattern\Building\Wall;

class Program
{
    /**
     * @return array
     */
    public function run()
    {
        //firstBuilding
        $strawWall = new Wall('straw');
        $firstBuilding = new Building($strawWall, 'oldestPigHouse');

        dump($firstBuilding->name); // oldestPigHouse
        dump($firstBuilding->wall->material); // straw
    }
}

完成後,豬大哥很得意自己對房子的設計,
詢問弟弟們要不要直接拷貝一間。


需求一:拷貝豬大哥的稻草屋

兩個弟弟想了想,拷貝大哥的房子,好像是個省事的方法,
但二弟想用木材 (wood),三弟想用磚塊 (bricks) 來蓋房子。

  • 決定拷貝稻草屋的弟弟們
<?php

namespace App\PrototypePattern\Building;

use App\PrototypePattern\Building\Building;
use App\PrototypePattern\Building\Wall;

class Program
{
    /**
     * @return array
     */
    public function run()
    {
        //firstBuilding
        $strawWall = new Wall('straw');
        $firstBuilding = new Building($strawWall, 'oldestPigHouse');

        //secondBuilding
        $secondBuilding = clone $firstBuilding;
        $secondBuilding->name = 'middlePigHouse';
        $secondBuilding->wall->material = 'wood';

        //thirdBuilding
        $thirdBuilding = clone $firstBuilding;
        $thirdBuilding->name = 'youngestPigHouse';
        $thirdBuilding->wall->material = 'bricks';

        dump($firstBuilding->name); // oldestPigHouse
        dump($firstBuilding->wall->material); // bricks

        dump($secondBuilding->name); // middlePigHouse
        dump($secondBuilding->wall->material); // bricks

        dump($thirdBuilding->name); // youngestPigHouse
        dump($thirdBuilding->wall->material); // bricks

        return [
            'firstBuilding' => $firstBuilding,
            'secondBuilding' => $secondBuilding,
            'thirdBuilding' => $thirdBuilding
        ];
    }
}

這時發生了件很尷尬的事情,
當豬二哥選用木材當建材時,大哥的稻草屋就變成木頭屋了...
而豬小弟選用磚塊當建材時,兩個哥哥們的房子就變成磚頭屋了...

導演表示,這樣故事沒辦法進行下去,請我們修改一下。


需求二:讓弟弟們對建材的修改,不會影響到哥哥

  • 修改Building中的clone()方法,讓拷貝時能重新創建牆壁
<?php

namespace App\PrototypePattern\Building;

use App\PrototypePattern\Building\Wall;

class Building
{
    /**
     * @var string
     */
    public $name;

    /**
     * @var Wall
     */
    public $wall;

    /**
     * @param Wall $wall
     * @param string $name
     */
    public function __construct(Wall $wall, string $name = 'unnamed')
    {
        $this->wall = $wall;
        $this->name = $name;
    }

    public function __clone()
    {
        $this->wall = clone $this->wall;
    }
}
  • 不用修改原本的程式碼
<?php

namespace App\PrototypePattern\Building;

use App\PrototypePattern\Building\Building;
use App\PrototypePattern\Building\Wall;

class Program
{
    /**
     * @return array
     */
    public function run()
    {
        //firstBuilding
        $strawWall = new Wall('straw');
        $firstBuilding = new Building($strawWall, 'oldestPigHouse');

        //secondBuilding
        $secondBuilding = clone $firstBuilding;
        $secondBuilding->name = 'middlePigHouse';
        $secondBuilding->wall->material = 'wood';

        //thirdBuilding
        $thirdBuilding = clone $firstBuilding;
        $thirdBuilding->name = 'youngestPigHouse';
        $thirdBuilding->wall->material = 'bricks';

        dump($firstBuilding->name); // oldestPigHouse
        dump($firstBuilding->wall->material); // straw

        dump($secondBuilding->name); // middlePigHouse
        dump($secondBuilding->wall->material); // wood

        dump($thirdBuilding->name); // youngestPigHouse
        dump($thirdBuilding->wall->material); // bricks

        return [
            'firstBuilding' => $firstBuilding,
            'secondBuilding' => $secondBuilding,
            'thirdBuilding' => $thirdBuilding
        ];
    }
}

後來弟弟們改用其他建材時,就不會影響到原本哥哥的房子了。

這就是淺複製 (Shallow Copy)深複製 (Deep Copy) 的不同。

後來三隻小豬跑去開建設公司,又是另一個故事了。


最後附上類別圖:
https://ithelp.ithome.com.tw/upload/images/20201031/20111630Uabzi4dLRG.png
(註:若不熟悉 UML 類別圖,可參考UML類別圖說明。)

ʕ •ᴥ•ʔ:一開始clone()方法能直接使用,是因為PHP的魔術方法__clone()唷!


上一篇
Day37. 原型模式
下一篇
Day39. 建造者模式
系列文
你終究都要學設計模式的,那為什麼不一開始就學呢?57
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言