//手机套餐费用接口 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();
时间: 2024-10-11 13:45:18