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

WBOY
Freigeben: 2016-07-13 09:57:23
Original
772 Leute haben es durchsucht

装饰器模式,装饰模式

装饰器模式

<?<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();
Nach dem Login kopieren

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/983244.htmlTechArticle装饰器模式,装饰模式 装饰器模式 ? php // 装饰器模式-在不改变原有类的结构上,对类的功能那个作补充//武器基类 abstract class Weapon{ abst...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage