外观模式/ 门面模式(Facade Pattern)

来源:互联网 发布:资源均衡优化的步骤 编辑:程序博客网 时间:2024/04/29 16:03

百度词条的解释

http://baike.baidu.com/view/2739662.htm

<?php//正面模式(外观模式)//外观类(厨师)class Cook {private $foods = array();//烹饪public function cooking(Customer $customer) {$menuList = $customer->getOrder();foreach($menuList as $value) {switch($value){case 'pig':$menu = new Pig();break;case 'vegetable':$menu = new Vegetable();break;case 'fish':$menu = new Fish;break;}$cookingFood = $menu->cooking();array_push($this->foods,$cookingFood);}}public function getFoods(){return $this->foods;}}//顾客类class Customer {private $menu = array();public function order($food) {array_push($this->menu,$food);return $this;}public function getOrder() {return $this->menu;}}class Pig {public function cooking() {return 'ripe-pig';}}class Vegetable {public function cooking() {return 'ripe-vegetable';}}class Fish {public function cooking() {return 'ripe-fish';}}//顾客类实例$custom = new Customer();$custom->order('fish')->order('pig')->order('vegetable');//厨师类实例(外观类实例)$cook = new Cook();$cook->cooking($custom);var_dump($cook->getFoods());?>

原创粉丝点击