> php教程 > php手册 > 본문

装饰器模式,装饰模式

WBOY
풀어 주다: 2016-06-13 09:07:14
원래의
784명이 탐색했습니다.

装饰器模式,装饰模式

装饰器模式

<?<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();
로그인 후 복사

 

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!