本文共 1224 字,大约阅读时间需要 4 分钟。
name=$name; } public function getName(){ return $this->name; }}//代理接口。针对接口编程,符合了依赖倒转原则。abstract class GiveGift{ abstract public function giveDolls(); abstract public function giveFlowers(); abstract public function giveChocolate();}//追求者类class Pursuit extends GiveGift{ private $mm; public function __construct(SchoolGirl $mm){ $this->mm=$mm; } public function giveDolls(){ echo $this->mm->getName().'送你洋娃娃'; } public function giveFlowers(){ echo $this->mm->getName().'送你鲜花'; } public function giveChocolate(){ echo $this->mm->getName().'送你巧克力'; }}//代理者,它与追求者的功能(方法)一样,只不过起到了一个//代理的作用,实际的东西(功能)依然是追求者的。代理者//送的东西依然是追求者的。class Proxy extends GiveGift{ private $gg; public function __construct(SchoolGirl $mm){ $this->gg=new Pursuit($mm); } public function giveDolls(){ $this->gg->giveDolls(); } public function giveFlowers(){ $this->gg->giveFlowers(); } public function giveChocolate(){ $this->gg->giveChocolate(); }}//客户端$proxy1=new Proxy(new SchoolGirl('韩梅梅'));$proxy1->giveDolls();echo '';$proxy1->giveFlowers();echo '';$proxy1->giveChocolate();
转载于:https://blog.51cto.com/noican/1614782