首页  >  文章  >  后端开发  >  装饰器模式,装饰模式_PHP教程

装饰器模式,装饰模式_PHP教程

WBOY
WBOY原创
2016-07-13 09:57:23764浏览

装饰器模式,装饰模式

装饰器模式

<?<span>php
</span><span>//</span><span>装饰器模式-在不改变原有类的结构上,对类的功能那个作补充

//武器基类</span>
<span>abstract</span> <span>class</span><span> Weapon{
    </span><span>abstract</span> <span>public</span> <span>function</span><span> descriptions();
    </span><span>abstract</span> <span>public</span> <span>function</span><span> cost();
}

</span><span>//</span><span>剑类</span>
<span>class</span> Glave <span>extends</span><span> Weapon{
    </span><span>public</span> <span>function</span><span> descriptions(){
        </span><span>return</span> 'Glave'<span>;
    }
    
    </span><span>public</span> <span>function</span><span> cost(){
        </span><span>return</span> "100"<span>;
    }
}

</span><span>//</span><span>匕首类</span>
<span>class</span> Knife <span>extends</span><span> Weapon{
    </span><span>public</span> <span>function</span><span> descriptions(){
        </span><span>return</span> <span>__CLASS__</span><span>;
    }
    </span><span>public</span> <span>function</span><span> cost(){
        </span><span>return</span> "80"<span>;
    }
}

</span><span>//</span><span>斧类</span>
<span>class</span> Axe <span>extends</span><span> Weapon{
    </span><span>public</span> <span>function</span><span> descriptions(){
        </span><span>return</span>  <span>__CLASS__</span><span>;
    }
    </span><span>public</span> <span>function</span><span> cost(){
        </span><span>return</span> "200"<span>;
    }
}

</span><span>//</span><span>属性类</span>
<span>class</span> Property <span>extends</span><span> Weapon{
    </span><span>protected</span> <span>$_weapon</span> = <span>null</span><span>;
    </span><span>protected</span> <span>$_price</span> = 0<span>;
    </span><span>protected</span> <span>$_descriptions</span> = ''<span>;
    </span><span>public</span> <span>function</span> __construct(Weapon <span>$weapon</span><span>){
        </span><span>$this</span>->_weapon = <span>$weapon</span><span>;
    }
    </span><span>public</span> <span>function</span><span> cost(){
        </span><span>return</span>     <span>$this</span>->_weapon->cost() + <span>$this</span>-><span>_price;
    }
    
    </span><span>public</span> <span>function</span><span> descriptions(){
        </span><span>return</span> <span>$this</span>->_weapon->descriptions().<span>$this</span>-><span>_descriptions;
    }
}

</span><span>//</span><span>力量属性</span>
<span>class</span> Strength <span>extends</span><span> Property{
    </span><span>protected</span> <span>$_price</span> = 30<span>;
    </span><span>protected</span> <span>$_descriptions</span> = '+ Strength'<span>;
}

</span><span>//</span><span>敏捷属性</span>
<span>class</span> Agility <span>extends</span><span> Property{
    </span><span>protected</span> <span>$_price</span> = 50<span>;
    </span><span>protected</span> <span>$_descriptions</span> = '+ Agility'<span>;
}

</span><span>//</span><span>智力属性</span>
<span>class</span> Intellect <span>extends</span><span> Property{
    </span><span>protected</span> <span>$_price</span> = 20<span>;
    </span><span>protected</span> <span>$_descriptions</span> = '+ Intellect'<span>;
}

</span><span>$weapon</span> = <span>new</span> Agility(<span>new</span> Strength(<span>new</span> Strength(<span>new</span><span> Glave())));
</span><span>echo</span> <span>$weapon</span>-><span>cost();
</span><span>echo</span> <span>$weapon</span>->descriptions();

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/983244.htmlTechArticle装饰器模式,装饰模式 装饰器模式 ? php // 装饰器模式-在不改变原有类的结构上,对类的功能那个作补充//武器基类 abstract class Weapon{ abst...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn