首页 > 代码库 > php设计模式--装饰模式

php设计模式--装饰模式

//手机套餐费用接口abstract class IPhonePackpageFee{      protected abstract function SpendFee();      public $Fee;}
//基础消费class  MonthFee extends IPhonePackpageFee{    public function __construct($fee)    {        $this->Fee=$fee;    }    public function SpendFee ()    {        echo("基础套餐费用:".$this->Fee);        echo ("<hr/>");    }}

装饰基类:

//装饰类class DecoratorService extends IPhonePackpageFee{       private  $exFee;       function __construct(IPhonePackpageFee $exfee)    {        $this->exFee=$exfee;    }    public  function SpendFee()    {        $this->exFee->SpendFee();    }}
class  MobileFlow extends  DecoratorService{    function __construct(DecoratorService $exService,$fee)    {        parent::__construct($exService);        $this->Fee=$fee;    }    public function SpendFee()    {        $this->SpendFlow();        parent::SpendFee();    }    private function SpendFlow()    {        echo ("流量费用:".$this->Fee);        echo ("<br/>");    }}

调用:

echo("php装饰模式:");        echo ("<hr/>");        $monthfee=new MonthFee(30);        $decorator=new DecoratorService($monthfee);        $flow=new MobileFlow($decorator,12);        $flow->SpendFee();

 

php设计模式--装饰模式