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

百度词条的解释

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());
?>

你可能感兴趣的:(外观模式/ 门面模式(Facade Pattern))