Home > php教程 > php手册 > body text

装饰器模式,装饰模式

WBOY
Release: 2016-06-13 09:07:14
Original
784 people have browsed it

装饰器模式,装饰模式

装饰器模式

<?<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();
Copy after login

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!