zoukankan      html  css  js  c++  java
  • php设计模式之:装饰者模式

    Beverage.php

    <?php
    namespace 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

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

    CubitaCoffee.php

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

    CoffeeDecorator.php

    <?php
    
    namespace 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

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

    SugarDecorator.php

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

    最终调用:

    $coffee = new DesignPatternBlueMountainCoffee();//生成一杯蓝山咖啡
    $beverage = new DesignPatternMilkDecorator($coffee);//给蓝山咖啡加一份牛奶
    $beverage = new DesignPatternMilkDecorator($beverage);//给蓝山咖啡再加一份牛奶
    $beverage = new DesignPatternSugarDecorator($beverage);//给蓝山咖啡加一份糖
    $beverage = new DesignPatternSugarDecorator($beverage);//给蓝山咖啡再加一份糖
    $beverage = new DesignPatternSugarDecorator($beverage);//给蓝山咖啡再加一份糖
    var_dump($beverage->getCost());
    

    程序运行结果:

    float(26.6)
    
  • 相关阅读:
    【Go】windows下搭建go语言编译环境
    【java回调】同步/异步回调机制的原理和使用方法
    【tomcat】tomcat远程调试
    【tomcat】获取访问者真实IP
    【深度学习学习记录】之一:开篇闲扯一些话
    【java】线程安全的整型类AtomicInteger
    【OpenStack】源码级深入了解删除虚拟机操作
    【Maven】maven的常用命令以及搭建maven私人仓库
    素 数 (第三届省赛)
    房间安排(第三届省赛)
  • 原文地址:https://www.cnblogs.com/foolishnoob/p/9261219.html
Copyright © 2011-2022 走看看