Design pattern -- Decorator.

  装饰模式,可以用一个装修实例来说明。

 

decorator
<?php
/**
*
* @author lake
* @package Decorator
*
*/
interface House
{
public function decorate();
}
class MyHouse implements House
{
public function decorate()
{
echo "我是lake,我要装修我的房子".__CLASS__."<br/>";
}
}
abstract class Decorator implements House
{
public function __construct( House $house )
{
$this->house = $house;
}

public function decorate()
{

}
}
class worker1 extends Decorator
{

public function decorate( )
{
echo $this->house->decorate()."我是装修工'".__CLASS__."',我负责给您的房子刷墙纸<br/>";
}
}

class worker2 extends Decorator
{
public function decorate( )
{
echo $this->house->decorate()."我是装修工'".__CLASS__."',我负责给您的房子铺上地板<br/>";
}
}

class worker3 extends Decorator
{
public function decorate( )
{
echo $this->house->decorate()."我是装修工'".__CLASS__."',我负责给您的房子装上吊灯<br/>";
}
}

$o = new worker3(new worker2(new worker1(new MyHouse())));

echo $o->decorate();
?>

 

  
posted @ 2010-01-06 22:57  一缕青烟  阅读(176)  评论(0编辑  收藏  举报