zoukankan      html  css  js  c++  java
  • 装饰器模式

     1 <?php
     2 //装饰器模式-在不改变原有类的结构上,对类的功能那个作补充
     3 
     4 //武器基类
     5 abstract class Weapon{
     6     abstract public function descriptions();
     7     abstract public function cost();
     8 }
     9 
    10 //剑类
    11 class Glave extends Weapon{
    12     public function descriptions(){
    13         return 'Glave';
    14     }
    15     
    16     public function cost(){
    17         return "100";
    18     }
    19 }
    20 
    21 //匕首类
    22 class Knife extends Weapon{
    23     public function descriptions(){
    24         return __CLASS__;
    25     }
    26     public function cost(){
    27         return "80";
    28     }
    29 }
    30 
    31 //斧类
    32 class Axe extends Weapon{
    33     public function descriptions(){
    34         return  __CLASS__;
    35     }
    36     public function cost(){
    37         return "200";
    38     }
    39 }
    40 
    41 //属性类
    42 class Property extends Weapon{
    43     protected $_weapon = null;
    44     protected $_price = 0;
    45     protected $_descriptions = '';
    46     public function __construct(Weapon $weapon){
    47         $this->_weapon = $weapon;
    48     }
    49     public function cost(){
    50         return     $this->_weapon->cost() + $this->_price;
    51     }
    52     
    53     public function descriptions(){
    54         return $this->_weapon->descriptions().$this->_descriptions;
    55     }
    56 }
    57 
    58 //力量属性
    59 class Strength extends Property{
    60     protected $_price = 30;
    61     protected $_descriptions = '+ Strength';
    62 }
    63 
    64 //敏捷属性
    65 class Agility extends Property{
    66     protected $_price = 50;
    67     protected $_descriptions = '+ Agility';
    68 }
    69 
    70 //智力属性
    71 class Intellect extends Property{
    72     protected $_price = 20;
    73     protected $_descriptions = '+ Intellect';
    74 }
    75 
    76 $weapon = new Agility(new Strength(new Strength(new Glave())));
    77 echo $weapon->cost();
    78 echo $weapon->descriptions();
  • 相关阅读:
    重新理解:ASP.NET 异步编程
    EF DbContext.Configuration.ProxyCreationEnabled 什么鬼?
    爱的预感
    ASP.NET MVC Ajax.ActionLink 简单用法
    DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践(1)
    EntityFramework 外键值映射
    ASP.NET Web API 异常日志记录
    ASP.NET MVC Application_Error 无效不执行
    JavaScript sync and async(同步和异步)
    软件的模块化开发
  • 原文地址:https://www.cnblogs.com/gide/p/4440044.html
Copyright © 2011-2022 走看看