Decorator模式
生活随笔
收集整理的這篇文章主要介紹了
Decorator模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Decorator(裝飾器)模式能夠像標準的繼承一樣為類添加新的功能。
不同于標準繼承機制的是,如果對象進行了實例化,Decorator模式能夠在運行時動態地為對象添加新的功能。
<?php abstract class AbstractCar{public abstract function getPrice();public abstract function getManufacturer(); } class Car extends AbstractCar{private $price=200000;private $manufacturer='bmw';public function getPrice(){return $this->price;}public function getManufacturer(){return $this->manufacturer;} }class CarDecorator extends AbstractCar{private $target;public function __construct(Car $target){$this->target=$target;}public function getPrice(){return $this->target->getPrice();}public function getManufacturer(){return $this->target->getManufacturer();} }class NavigationSystem extends CarDecorator{public function getPrice(){return parent::getPrice()+1000;} }$car=new Car(); $car=new NavigationSystem($car);echo $car->getPrice();?>?
轉載于:https://www.cnblogs.com/HKUI/p/4357969.html
總結
以上是生活随笔為你收集整理的Decorator模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MD5加密详解
- 下一篇: input file实现批量上传