zoukankan      html  css  js  c++  java
  • php设计模式--装饰器模式

    包装对象 扩展实例。

    interface IComponent
    {
        function Display();
    }
    
    class Person implements IComponent
    {
        private $name;
        function __construct($name){
            $this->name = $name;
        }
        function Display(){
            echo "装扮的:{$this->name}<br/>";
        }
    }
    
    
    class clothes implements IComponent
    {
        protected $component;
        function Decorate(IComponent $component){
            $this->component = $component;
        }
    
        public function Display(){
            if (!empty($this->component)) {
                $this->component->Display();
            }
        }
    }
    
    class xie extends clothes
    {
        function Display(){
            echo "回力";
            parent::Display();
        }
    }
    
    class yundong extends clothes 
    {
        function Display(){
            echo "耐克";
            parent::Display();
        }
    }
    
    class txue extends clothes 
    {
        function Display(){
            echo "阿迪";
            parent::Display();
        }
    }
    
    class waitao extends clothes 
    {
        function Display(){
            echo "李宁";
            parent::Display();
        }
    }
    
    //$ym = new Person("姚明");
    $md = new Person("麦迪");
    
    //$xie = new xie();
    //$waitao = new waitao();
    
    //$xie->Decorate($ym);
    //$waitao->Decorate($xie);
    //$waitao->Display();
    //echo "<hr/>";
    
    $yd = new yundong();
    $tx = new txue();
    
    $yd->Decorate($md);
    $tx->Decorate($yd);
    $tx->Display();
    die;
  • 相关阅读:
    《构建之法》心得体会
    简单工厂模式加减乘除器
    个人简介
    单元测试和代码覆盖率工具的使用
    Bookstore系统缺陷报告
    《构建之法读后感》
    3137102432_ 施少兵_ lab3
    3137102432_施少兵_实验2
    个人简介
    第六次作业:购物系统缺陷
  • 原文地址:https://www.cnblogs.com/songyanan/p/12067341.html
Copyright © 2011-2022 走看看