>  기사  >  백엔드 개발  >  装饰器模式,装饰模式_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으로 문의하세요.