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

    区域类:

      1.平原类(经验值2)

        1.1干净的平原类(经验值加2)

        1.2污染的平原类(经验值减4)

        1.3干净又污染的平原类

      2.高原类(经验值3)

        2.1干净的高原类(经验值加2)

        2.2污染的高原类(经验值减4)

        2.3干净又污染的高原类

    装饰器模式类图:

    <?php
    abstract class Tile{
    	abstract public function exprience();
    }
    class Plains extends Tile{
    	protected $_expricece = 2;
    	public function exprience(){
    		return $this->_expricece;
    	}
    }
    class Highland extends Tile{
    	private $_expricece = 3;
    	public function exprience(){
    		return $this->_expricece;
    	}
    }
    abstract class Decorator extends Tile{
    	protected $_tile;
    	public function __construct(Tile $_tile){
    		$this->_tile = $_tile;
    	}
    }
    class Clean extends Decorator{
    	public function exprience(){
    		return $this->_tile->exprience() + 2;
    	}
    }
    class Polluted extends Decorator{
    	public function exprience(){
    		return $this->_tile->exprience() - 4;
    	}
    }
    
    
    $_plains = new Plains();
    echo "普通平原的经验值:".$_plains->exprience();
    
    echo "<br/>";
    
    $_cleanplains = new Clean(new Plains());
    echo '干净平原的经验值:'.$_cleanplains->exprience();
    
    echo "<br/>";
    
    $_cleanhighland = new Clean(new Highland());
    echo '干净高原的经验值:'.$_cleanhighland->exprience();
    
    echo "<br/>";
    
    $_cleanpollutedplains = new Clean(new Polluted(new Plains()));
    echo "即干净又污染的平原的经验值:".$_cleanpollutedplains->exprience();
    ?>
    

      

    装饰模式的特点:
    1.装饰对象和真实对象有相同的接口。这样客户端对象就可以以和真实对象相同的方式
    和装饰对象交互。
    . 2.装饰对象包含一个真实对象的索引(reference)
    . 3.装饰对象接受所有的来自客户端的请求。它把这些请求转发给真实的对象。
    . 4.装饰对象可以在转发这些请求以前或以后增加一些附加功能。这样就确保了在运行
    时,不用修改给定对象的结构就可以在外部增加附加的功能。在面向对象的设计中,通常是
    通过继承来实现对给定类的功能扩展。

  • 相关阅读:
    APP版本更新通知流程测试要点
    Android+appium +python 点击坐标tap方法的封装
    appium 元素定位find_element_by_android_uiautomator方法使用
    Android 应用加固(乐固)操作说明
    查询APP Store已发布过的版本记录
    appium 报错:AttributeError:"NoneType' object has no attribute 'XXX'
    appium 运行报错:...... Attempt to re-install io.appium.settings without first uninstalling解决方案
    Charles模拟网络请求页面的网络超时测试
    利用漏洞中验证码绕过的小技巧
    C中的volatile用法
  • 原文地址:https://www.cnblogs.com/jsmiao/p/4575596.html
Copyright © 2011-2022 走看看