建造者模式实例

本文通过实例介绍了建造模式在PHP中的应用,解决厨师做菜时配料不准确的问题。通过抽象类定义做菜的标准步骤,并通过具体建造者类实现不同菜品的制作。

<?php
/**
 * 建造模式
 * 功能:由于厨师经常做菜不是少放了油就是没盐,顾客很不满意,
 *      为了提高服务质量,厨师们都按照以下程序做菜
 */

/**
 * 具体产品:
 * 做菜,最初就这一个类,厨师们乱放油盐
 */
class Cooking
{
	//菜的所有配料
	private $vegetables = array();
	
	//为这道菜添加配料
	public function add( $item )
	{
		if( is_array( $item ) )
		{
			$this->vegetables = array_merge( $this->vegetables , $item );
		}
		else 
		{
			$this->vegetables[] = $item;
		}
	}
	
	//显示这道菜用了些什么原料
	public function show()
	{
		print_r( $this->vegetables );
	}
}



/**
 * 在抽象类中,把该放的东西定死,做任何菜都得要执行这个。我看他们还会忘记放盐了不
 */
abstract class CookingBuilder
{
	//放油
	abstract function setOil();
	//放盐
	abstract function setSalt();
	//放其它佐料
	abstract function setOthers();
	//放菜
	abstract function setVegetables();
	
	abstract function getResult();
}

/**
 * 一个具体的建造者,实现CookingBuilder接口,做土豆丝,
 */
class PotatoBuilder extends CookingBuilder 
{
	private $oil = '菜油';
	private $salt = '盐一勺';
	private $vegetables = '土豆丝';
	private $others = array( '青椒' , '蒜' );
	
	private $vegetablesObj;
	
	public function __construct()
	{
		$this->vegetablesObj = new Cooking();
	}
	//放油
	public  function setOil()
	{
		$this->vegetablesObj->add( $this->oil );
	}
	//放盐
	public  function setSalt()
	{
		$this->vegetablesObj->add( $this->salt );
	}
	//放菜
	public  function setVegetables()
	{
		$this->vegetablesObj->add( $this->vegetables );
	}
	//放其它佐料
	public  function setOthers()
	{
		$this->vegetablesObj->add( $this->others );
	}
	
	public function getResult()
	{
		return $this->vegetablesObj;
	}

}


/**
 * 另一个具体的建造者,实现CookingBuilder接口,做西红柿炒蛋,
 */
class TomatoBuilder extends CookingBuilder 
{
	private $oil = '花生油';
	private $salt = '盐半勺';
	private $vegetables = '西红柿';
	private $others = array( '白糖' , '鸡蛋' );
	
	private $vegetablesObj;
	
	public function __construct()
	{
		$this->vegetablesObj = new Cooking();
	}
	//放油
	public  function setOil()
	{
		$this->vegetablesObj->add( $this->oil );
	}
	//放盐
	public  function setSalt()
	{
		$this->vegetablesObj->add( $this->salt );
	}
	//放菜
	public  function setVegetables()
	{
		$this->vegetablesObj->add( $this->vegetables );
	}
	//放其它佐料
	public  function setOthers()
	{
		$this->vegetablesObj->add( $this->others );
	}
	
	public function getResult()
	{
		return $this->vegetablesObj;
	}

}


/**
 * 指挥者:在这里使用CookingBuilder
 */
class Director
{
	private $directer;
	public function init( $builder )
	{
		$this->directer = $builder;
		$this->cooking();
	}
	
	public function cooking()
	{
		$this->directer->setOil();
		$this->directer->setSalt();
		$this->directer->setVegetables();
		$this->directer->setOthers();
	}
}


$director = new Director();
$potatoBuilder = new PotatoBuilder();
$tomatoBuilder = new TomatoBuilder();


$director->init( $potatoBuilder );
$potato = $potatoBuilder->getResult();
$potato->show();

$director->init( $tomatoBuilder );
$tomato = $tomatoBuilder->getResult();
$tomato->show();


?>

转载于:https://www.cnblogs.com/lchb/articles/1941163.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值