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.装饰对象可以在转发这些请求以前或以后增加一些附加功能。这样就确保了在运行
    时,不用修改给定对象的结构就可以在外部增加附加的功能。在面向对象的设计中,通常是
    通过继承来实现对给定类的功能扩展。

  • 相关阅读:
    Javascript的私有变量和方法、共有变量和方法以及特权方法、构造器、静态共有属性和静态共有方法
    Spring4整合Hibernate4出现的错误的解决
    Javascript类的创建
    Kettle学习总结(一)
    Kettle Excel导入数据到数据库
    Python 爬虫 2 (转)
    Js仿腾讯微博效果
    飘雪效果
    列表获取对应图片
    飞入购物车
  • 原文地址:https://www.cnblogs.com/jsmiao/p/4575596.html
Copyright © 2011-2022 走看看