《大话设计模式》命令模式-烤肉店点菜场景-PHP版代码

Standard

最近闲时在看早前公司里买的《大话设计模式》。
那本书上的代码是C#写的,下面是我用PHP改写的一些书中范例,初学者可以用其帮助强化下记忆。

命令模式
烧烤店点菜场景

<?php
/**
 * 命令模式 烤肉店点菜场景
 * @author 叶茂盛 anson.ye#gmail.com  http://yemaosheng.com
 */
interface Command {
    public function execute();
}
interface ListCommand extends Command {
    public function remove(Command $command);
    public function add(Command $command);
}
 /**
 * 命令集
 */
class MacroCommand implements ListCommand {
 
    private $_commandList;
 
    public function __construct() {
        $this->_commandList = array();
    }
 
    public function remove(Command $command) {
        $key = array_search($command, $this->_commandList);
        if ($key === FALSE) {
            return FALSE;
        }
        echo "去掉" . $command->name . "\n";
        unset($this->_commandList[$key]);
        return TRUE;
    }
 
    public function add(Command $command) {
        echo "点份" . $command->name . "\n";
        return array_push($this->_commandList, $command);
    }
 
    public function execute() {
        echo "确定下单\n";
        foreach ($this->_commandList as $command) {
            $command->execute();
        }
    }
}
 /**
 * 命令
 */
class BakeMutton implements Command {
 
    private $_barbecuer;
    public $name;
 
    public function __construct(Barbecuer $barbecuer) {
        $this->_barbecuer = $barbecuer;
        $this->name = __CLASS__;
    }
    public function execute() {
        $this->_barbecuer->bakeMutton();
    }
}
class BakeChicken implements Command {
 
    private $_barbecuer;
    public $name;
 
    public function __construct(Barbecuer $barbecuer) {
        $this->_barbecuer = $barbecuer;
        $this->name = __CLASS__;
    }
    public function execute() {
        $this->_barbecuer->bakeChicken();
    }
}
class BakeChickenWing implements Command {
 
    private $_barbecuer;
    public $name;
 
    public function __construct(Barbecuer $barbecuer) {
        $this->_barbecuer = $barbecuer;
        $this->name = __CLASS__;
    }
    public function execute() {
        $this->_barbecuer->bakeChickenWing();
    }
}
 /**
 * 烧烤厨师
 */
class Barbecuer {
 
    private $_name;
 
    public function __construct($name) {
        $this->_name = $name;
    }
 
    public function bakeMutton() {
        echo $this->_name . "开始烤羊肉.\n";
    }
    public function bakeChicken() {
        echo $this->_name . "开始烤鸡肉.\n";
    }
    public function bakeChickenWing() {
        echo $this->_name . "开始烤鸡翅膀.\n";
    }
}
 /**
 * 服务员
 */
class Waiter {
 
    private $_command;
 
    public function __construct(Command $command) {
        $this->_command = $command;
    }
 
    public function action() {
        $this->_command->execute();
    }
}
 /**
 * Main
 */
class Main {
    public static function run() {
        $barbecuer = new Barbecuer('烧烤厨师');
 
        $bakeMutton = new BakeMutton($barbecuer);
        $bakeChicken = new BakeChicken($barbecuer);
        $bakeChickenWing = new BakeChickenWing($barbecuer);
 
        $macroCommand = new MacroCommand();
        $macroCommand->add($bakeMutton);
        $macroCommand->add($bakeChicken);
        $macroCommand->add($bakeChickenWing);
 
        $waiter = new Waiter($macroCommand);
 
        $macroCommand->remove($bakeMutton);
 
        $waiter = new Waiter($macroCommand);
        $waiter->action();
    }
}
 
Main::run();
?>
//php command.php 
//点份BakeMutton
//点份BakeChicken
//点份BakeChickenWing
//去掉BakeMutton
//确定下单
//烧烤厨师开始烤鸡肉.
//烧烤厨师开始烤鸡翅膀.

转载请注明出处 http://yemaosheng.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.