Decorator模式中遭遇继承与聚合

2016-01-29 12:58 5 1 收藏

Decorator模式中遭遇继承与聚合,Decorator模式中遭遇继承与聚合

【 tulaoshi.com - Java 】

  一:背景:Decorator

  *Decorator 常被翻译成"装饰",我觉得翻译成"油漆工"更形象点,油漆工(decorator)是用来刷油漆的,那么被刷油漆的对象我们称decoratee.这两种实体在Decorator 模式中是必须的。

  *Decorator 定义:

  动态给一个对象添加一些额外的职责,就象在墙上刷油漆.使用Decorator 模式相比用生成子类方式达到功能的扩充显得更为灵活。

  *为什么使用Decorator?

  我们通常可以使用继承来实现功能的拓展,如果这些需要拓展的功能的种类很繁多,那么势必生成很多子类,增加系统的复杂性,同时,使用继承实现功能拓展,我们必须可预见这些拓展功能,这些功能是编译时就确定了,是静态的。

  使用Decorator 的理由是:这些功能需要由用户动态决定加入的方式和时机.Decorator 提供了"即插即用"的方法,在运行期间决定何时增加何种功能。

  *对于该模式,初步归纳为

  1.基本功能为接口

  2.Decorator参数为接口本身也为接口以便为下一个Decorator的参数

  3.基本功能类实现接口 并作为Decorator构造函数的参数,以便在此基础上添加新功能

  4.额外功能由Decorator中的数据结构处理
  
  二:问题

  这是一段Decorator设计模式的实现例子如下:

  基本功能:Counter类
  需要添加的功能

  1:上限控制

  2:下限控制

  import java.io.*;
  class Counter{
  private int value;
  public Counter(int v){
  System.out.println("init me here in The Counter with value!");
  value=v;
  }

  public Counter(Counter cc){
  System.out.println("init me here in The Counter with class!");
  value=cc.value;
  }

  public int read_value(){
  System.out.println("read me here The value is:"+value);
  System.out.println("read me here in The Counter!");

  return value;
  }

  public void increment(){
  System.out.println("increment me here in The Counter !");
  value++;
  }

  public void decrement(){
  System.out.println("decrement me here in The Counter !");
  value--;
  }
  }

  class Decorator extends Counter
  {
  Counter counter;
  public Decorator(Counter c)
  {
  super(c);
  System.out.println("init me here with class Decorator!");
  } 
  }

  class UpperLimit extends Decorator//上限控制
  {
  public UpperLimit(Counter c)
  {
  super(c);
  counter=c;
  System.out.println("init me here with class UpperLimit!");
  }
  public void increment()
  {
  if(counter.read_value()20)
  {
   System.out.println("Too High");
  }
  else
  {
   System.out.println("increment me here with class UpperLimit!");
   counter.increment();
  }
  }
  /*public void decrement()
  {
  counter.decrement();
  }
  public int read_value()
  {
  return counter.read_value();
  }*/

  }

  class LowerLimit extends Decorator//下限控制
  {
  public LowerLimit(Counter c)
  {
  super(c);
  counter=c;
  System.out.println("init me here in The Counter with class LowerLimit!");
  }
  public void decrement()
  {
  System.out.println("Class value :"+read_value());
  System.out.println("Dec value :"+counter.read_value());
  if(counter.read_value()<=0)
  {
   System.out.println(counter.read_value());
   System.out.println("Too Low");
  }
  else
  {
   System.out.println("decrement me here in The Counter with class LowerLimit!");
   counter.decrement();
  }
  }
  /*public void increment()
  {
  counter.increment();
  }
  public int read_value()
  {
  return counter.read_value();
  }*/
  }

  class CounterFactory
  {
  public static Counter crea

来源:https://www.tulaoshi.com/n/20160129/1488351.html

延伸阅读
标签: Web开发
一.类的定义: 1.混合的构造函数/原型:  程序代码 function Parent(name) {     //实例属性     this.name = name; } //实例方法 Parent.prototype.hello = function () {     alert("parent!"); } //类属性 Parent.PI = 3.14159; //类方法 Parent.say = function () {   &...
标签: Web开发
1、关于javascript的apply和call函数 prototype.js中用了大量的apply和call函数,不注意会造成理解偏差。 官方解释:应用某一对象的一个方法,用另一个对象替换当前对象。 apply与call的区别是第二个参数不同。apply是  数组或者arguments 对象。而call是逗号隔开的任何类型。 apply,call方法最让人混淆的地方也是apply,c...
标签: word
Word 2007中插入模式的启用与禁用 Word 2007中新增了一种称为插入模式的功能,可以允许用户使用Insert键控制改写模式。当插入模式启用时,用户可以使用Insert键切换插入或改写状态。如果处于改写状态,则用户可以将光标所在处的文本直接改写为其他文本。 在使用此功能之前,必须首先要将其启用。下面介绍启用Word 2007插入模式的具体...
标签: Web开发
代码是随手写的,只提供思路。 这个原理很简单,看代码就懂,不多说了。 (function (){ var h = 0; handle = function (){return h++}; var f = function (){}; extend = function (a, b){     f.prototype = a;     var ret = new f;     if (typeof b == 'function') { &nbs...
作者:JavaResearch 大多数人认为,接口的意义在于顶替多重继承。众所周知Java没有c++那样多重继承的机制,但是却能够实现多个接口。其实这样做是很牵强的,接口和继承是完全不同的东西,接口没有能力代替多重继承,也没有这个义务。接口的作用,一言以蔽之,就是标志类的类别(type of class)。把不同类型的类归于不同的接口,可以更好的管...

经验教程

276

收藏

23
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部