PHP design pattern: Decorator mode

Source: Internet
Author: User

beverage.php

<?phpnamespace DesignPattern;abstract class Beverage{    /**     * @var string     */    protected $name = ‘‘;    /**     * @return string     */    public function getName(): string    {        return $this->name;    }    /**     * @param string $name     * @return Beverage     */    public function setName(string $name): Beverage    {        $this->name = $name;        return $this;    }    /**     * @return float     */    public abstract function getCost(): float;    /**     * Beverage constructor.     */    public function __construct()    {    }}

bluemountaincoffee.php

<?phpnamespace DesignPattern;/** * 蓝山咖啡 */class BlueMountainCoffee extends Beverage{    /**     * @return float     */    public function getCost(): float    {        return 18.0;    }}

cubitacoffee.php

<?phpnamespace DesignPattern;/** * 琥爵咖啡 */class CubitaCoffee extends Beverage{    /**     * @return float     */    public function getCost(): float    {        return 24.0;    }}

coffeedecorator.php

<?phpnamespace DesignPattern;abstract class CoffeeDecorator extends Beverage{    /**     * @var CoffeeDecorator     */    protected $coffeeDecorator;    /**     * CoffeeDecorator constructor.     * @param Beverage $coffeeDecorator     */    public function __construct(Beverage $coffeeDecorator)//特别需要注意这个地方,装饰者模式必须允许对装饰后的对象再次装饰    {        parent::__construct();        $this->coffeeDecorator = $coffeeDecorator;    }}

milkdecorator.php

<?phpnamespace DesignPattern;class MilkDecorator extends CoffeeDecorator{    /**     * @return float     */    public function getCost(): float    {        return $this->coffeeDecorator->getCost() + 2.5;    }}

sugardecorator.php

<?phpnamespace DesignPattern;class SugarDecorator extends CoffeeDecorator{    /**     * @return float     */    public function getCost(): float    {        return $this->coffeeDecorator->getCost() + 1.2;    }}

Final Call:

$coffee = new \DesignPattern\BlueMountainCoffee();//生成一杯蓝山咖啡$beverage = new \DesignPattern\MilkDecorator($coffee);//给蓝山咖啡加一份牛奶$beverage = new \DesignPattern\MilkDecorator($beverage);//给蓝山咖啡再加一份牛奶$beverage = new \DesignPattern\SugarDecorator($beverage);//给蓝山咖啡加一份糖$beverage = new \DesignPattern\SugarDecorator($beverage);//给蓝山咖啡再加一份糖$beverage = new \DesignPattern\SugarDecorator($beverage);//给蓝山咖啡再加一份糖var_dump($beverage->getCost());

Program Run Result:

float(26.6)

PHP design pattern: Decorator mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.