装饰器设计模式

装饰器设计模式(来源于php设计模式 看书笔记)

何时使用装饰器
  • 如果想为现有对象增加新功能 而不想影响其它功能
关于包装器
  • 下面代码显示了如何将一个整数包装在一个对象中,以及如何获取这个整数
  • 包装器表示用来减少不兼容性的策略
class primitiveWrap{ 
    private $wrapMe; 
    public function __construct($wrapMe) { 
        $this->wrapMe; 
    }

    public function showWrap() {
            return $this-wrapMe;
        }

}
$myPrim = 521;
$wrappedUp = new PrimitiveWrap($myPrim);
echo $wrappedUp->showWrap();
php 中内置的包装器
  • file_get_contents 将一个文件包装在内置的包装器中。
设计模式包装器
  • 下面的代码展示了Client 如何将组件对象($component) 包装在装饰器(Maintenance)中; $component = new Maintenance($component);
包装多个组件的装饰器
开发人员的约会服务
  • 它为软件开发人员建立一个约会服务,两个组件分别是Male和Female,可以分别为这两个组件装饰不同的约会地点
  • 每个组件都有一个名字和指定的年龄,具体装饰会有不同的状态
  • 组件接口
    • 包括3个属性和5个方法
    • $date 属性用来标识这是一个“约会”,而不是普通的日期对象
    • $ageGroup 是该组件所属的组
    • $feature 是由某个具体装饰提供的特性
//IComponent 接口 IComponent.php 
//所有方法都是抽象的,属性是受保护的 
    abstract class IComponent { 
        protected $date; 
        protected $ageGroup;
        protected $feature; 
        abstract public function setAge($ageNow); 
        abstract public function getAge(); 
        abstract public function setFeature(); 
        abstract public function getFeature(); 
    } 
    // Male.php 
    // 两个具体组件实现为Male(男性) 和Female(女性).它们分别有一个构造函数,将$date值(ID)设置为”Male“ 或 ”Female“. 另外获取方法/设置方法 设置年龄和其他可能增加的装饰 
    // 可以使用 $setfeature() 方法为这两个组件增加其他特性作为装饰。可以把$setfeature()看作是一个组件增强器,而不只是一个组件设置方法 
    class Male extends IComponent { 
        function __construct() { 
            $this->date = "Male"; 
            $this->setFeature("Dude programmer features:"); 
        } 
        public function setAge($ageNow) { 
            $this->ageGroup = $ageNow; 
        } public function getAge() { 
            return $this->ageGroup; 
        } 
        public function setFeature($fea) { 
            $this->feature = $fea; 
        } 
        public function getFeature() { 
            return $this->feature; 
        }
    } 
    //Female.php 
    class Female extends IComponent { 
        function __construct() { 
            $this->date = "Female"; 
            $this->setFeature("Grrrl programmer features:"); 
        } 
        public function setAge($ageNow) { 
            $this->ageGroup = $ageNow; 
        } 
        public function getAge() { 
            return $this->ageGroup; 
        } 
        public function setFeature($fea) { 
            $this->feature = $fea; 
        } 
        public function getFeature() { 
            return $this->feature; 
        }
    } 
    // 包含组件方法的装饰器 
    //Decorator.php Decorator(装饰者) 
    abstract class Decorator extends IComponent { 
        public function setAge($ageNoe) { 
            $this->ageGroup=$this->ageGroup; 
        } 
        public function getAge() { 
            return $this->ageGroup; 
        } 
    } 
    // 以下是三个具体的装饰器 ProgramLang.php program (程序) 
    class ProgramLang extends Decorator { 
        private $languageNow; 
        private $language = array( "php" => "PHP", "cs" => "C#", "js" => "JavaScript", ); 
        public function __construct(IComponent $dateNow) { 
            $this->date = $dateNow;   
        } 
        public function setFeature($lan) { 
            $this->languageNow = $this->language[$lan]; 
        } 
        public function getFeature() { 
            $output = $this->date->getFeature(); // 调用了组件里面的方法 
            $fmat = "<br />&nbsp;&nbsp;"; 
            $output .= "$fmat preferred programming language:"; 
            $output .= $this->languageNow; 
            return $output; 
        }
    }

    // client 
    function __autoload($class_name) {
        include $class_name.'.php';
    }

    class Client{
        private $hotDate;
        public function __contruct() {
            $this->hotDate = new Female();
            $this->hotDate->setAge("Age group 4");
            echo $this->hotDate->getAge();
            $this->hotDate->getFeature();
            echo $this->hotDate->getFeature();
        }
        private function wrapComponent(IComponent $component) {
            $component = new ProgramLang($component);
            $component->setFeature("php");
            $component = new Hardware($component); // 跟 ProgramLang 类类似
            $component->setFeature("lin");
            $component = new Food($component); // 跟 ProgramLang 类类似
            $component->setFeature("veg");
            retrun $component;

        }
    }
    $worker = new Client();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值