iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 15
1
Software Development

你終究都要學設計模式的,那為什麼不一開始就學呢?系列 第 15

Day15. 範例:機器人廚師(命令模式)

本文同步更新於blog

情境:這是一間牛排館,會根據客戶的訂單出菜

<?php

namespace App\CommandPattern\WesternRestaurant;

class Program
{
    /**
     * @param array $order
     * @return array
     */
    public function makeOrder($order)
    {
        $result = [];
        if (in_array('Filet Mignon', $order)) {
            $result[] = '菲力牛排';
        }

        if (in_array('Sirloin Steak', $order)) {
            $result[] = '沙朗牛排';
        }

        return $result;
    }
}

隨著生日蒸蒸日上,
老闆已經沒辦法同時處理客人點餐與煎牛排了(這是兩種職責)。

決定引進我們販賣的機器人廚師
讓他能專心在記下客人的需求。


需求一:老闆決定到外場服務,透過指令指揮內場的機器人廚師煎牛排

  • 首先是機器人廚師的程式碼
<?php

namespace App\CommandPattern\WesternRestaurant\Receiver;

abstract class Chef
{
    /**
     * @return string
     */
    public function cookFiletMignon()
    {
        return '菲力牛排';
    }

    /**
     * @return string
     */
    public function cookSirloinSteak()
    {
        return '沙朗牛排';
    }
}
<?php

namespace App\CommandPattern\WesternRestaurant\Receiver;

use App\CommandPattern\WesternRestaurant\Receiver\Chef;

class RobotChefA extends Chef
{
}

<?php

namespace App\CommandPattern\WesternRestaurant\Receiver;

use App\CommandPattern\WesternRestaurant\Receiver\Chef;

class RobotChefB extends Chef
{
}

  • 再定義出命令介面
<?php

namespace App\CommandPattern\WesternRestaurant\Contracts;

interface Command
{
    public function execute();
}
  • 需要的指令:煎菲力牛排、煎沙朗牛排
<?php

namespace App\CommandPattern\WesternRestaurant;

use App\CommandPattern\WesternRestaurant\Contracts\Command;
use App\CommandPattern\WesternRestaurant\Receiver\Chef;

class CookFiletMignonCommand implements Command
{
    /**
     * @var Chef
     */
    protected $chef;

    public function __construct(Chef $chef)
    {
        $this->chef = $chef;
    }

    /**
     * @return string
     */
    public function execute()
    {
        return $this->chef->cookFiletMignon();
    }
}
<?php

namespace App\CommandPattern\WesternRestaurant;

use App\CommandPattern\WesternRestaurant\Contracts\Command;
use App\CommandPattern\WesternRestaurant\Receiver\Chef;

class CookSirloinSteakCommand implements Command
{
    /**
     * @param Chef
     */
    protected $chef;

    public function __construct(Chef $chef)
    {
        $this->chef = $chef;
    }

    /**
     * @return string
     */
    public function execute()
    {
        return $this->chef->cookSirloinSteak();
    }
}
  • 最後修改原本牛排館的程式
<?php

namespace App\CommandPattern\WesternRestaurant;

use App\CommandPattern\WesternRestaurant\Receiver\RobotChefA;
use App\CommandPattern\WesternRestaurant\Receiver\RobotChefB;

class Program
{
    /**
     * @param array $order
     * @return array
     */
    public function makeOrder($order)
    {
        $chefA = new RobotChefA();
        $chefB = new RobotChefB();
        $cookFiletMignonCommand = new cookFiletMignonCommand($chefA);
        $cookSirloinSteakCommand = new CookSirloinSteakCommand($chefB);

        $result = [];
        if (in_array('Filet Mignon', $order)) {
            $result[] = $cookFiletMignonCommand->execute();
        }

        if (in_array('Sirloin Steak', $order)) {
            $result[] = $cookSirloinSteakCommand->execute();
        }

        return $result;
    }
}

[角色對應關係]
調用者 (Invoker):老闆
接收者 (Receiver):機器人廚師
命令 (Command):煎牛排指令


[單一職責原則]
我們將點餐煎牛排視作兩種不同的職責。
透過命令來聯繫兩者。

[開放封閉原則]
當新增/修改需求時,不會動到所有程式碼。
(比如:外場老闆不會知道烹調方式的改變、機器人不會知道當前餐廳的優惠)

[依賴反轉原則]
調用者依賴抽象的命令介面
命令實作抽象的命令介面

此外命令模式還可以留下點餐紀錄,便於將來結帳、重做餐點呢。

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

ʕ •ᴥ•ʔ:使用命令模式的老闆,也隱含「人力」資源管理的味道。
(指定誰做什麼餐點)


上一篇
Day14. 命令模式
下一篇
Day16. 樣板方法模式
系列文
你終究都要學設計模式的,那為什麼不一開始就學呢?57
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言