Decorator

Decorator常被翻译成"装饰",我觉得翻译成"油漆工"更形象点,油漆工(decorator)是用来刷油漆的,那么被刷油漆的对象我们称decoratee.这两种实体在Decorator模式中是必须的.
  
  Decorator定义:
  动态给一个对象添加一些额外的职责,就象在墙上刷油漆.使用Decorator模式相比用生成子类方式达到功能的扩充显得更为灵活.
  
  为什么使用Decorator?
  我们通常可以使用继承来实现功能的拓展,如果这些需要拓展的功能的种类很繁多,那么势必生成很多子类,增加系统的复杂性,同时,使用继承实现功能拓展,我们必须可预见这些拓展功能,这些功能是编译时就确定了,是静态的.
  
  使用Decorator的理由是:这些功能需要由用户动态决定加入的方式和时机.Decorator提供了"即插即用"的方法,在运行期间决定何时增加何种功能.
  
  如何使用?
  举Adapter中的打桩示例,在Adapter中有两种类:方形桩 圆形桩,Adapter模式展示如何综合使用这两个类,在Decorator模式中,我们是要在打桩时增加一些额外功能,比如,挖坑 在桩上钉木板等,不关心如何使用两个不相关的类.
  
  我们先建立一个接口:
  
  public interface Work
  {
    public void insert();
  
  }
  
  接口Work有一个具体实现:插入方形桩或圆形桩,这两个区别对Decorator是无所谓.我们以插入方形桩为例:
  
  public class SquarePeg implements Work{
    public void insert(){
      System.out.println("方形桩插入");
    }
  }
  
  现在有一个应用:需要在桩打入前,挖坑,在打入后,在桩上钉木板,这些额外的功能是动态,可能随意增加调整修改,比如,可能又需要在打桩之后钉架子(只是比喻).
  
  那么我们使用Decorator模式,这里方形桩SquarePeg是decoratee(被刷油漆者),我们需要在decoratee上刷些"油漆",这些油漆就是那些额外的功能.
  
  public class Decorator implements Work{
  
    private Work work;
    //额外增加的功能被打包在这个List中
    private ArrayList others = new ArrayList();
  
    //在构造器中使用组合new方式,引入Work对象;
    public Decorator(Work work)
    {
      this.work=work;
    
      others.add("挖坑");
  
      others.add("钉木板");
    }
  
    public void insert(){
  
      newMethod();
    }
  
    //在新方法中,我们在insert之前增加其他方法,这里次序先后是用户灵活指定的   
    public void newMethod()
    {
      otherMethod();
      work.insert();
  
    }
  
    public void otherMethod()
    {
      ListIterator listIterator = others.listIterator();
      while (listIterator.hasNext())
      {
        System.out.println(((String)(listIterator.next())) + " 正在进行");
      }
  
    }
  }
  
  在上例中,我们把挖坑和钉木板都排在了打桩insert前面,这里只是举例说明额外功能次序可以任意安排.
  
  好了,Decorator模式出来了,我们看如何调用:
  
  Work squarePeg = new SquarePeg();
  Work decorator = new Decorator(squarePeg);
  decorator.insert();
  
  Decorator模式至此完成.
  
  如果你细心,会发现,上面调用类似我们读取文件时的调用:
  
  FileReader fr = new FileReader(filename);
  BufferedReader br = new BufferedReader(fr);
  
  实际上Java 的I/O API就是使用Decorator实现的,I/O变种很多,如果都采取继承方法,将会产生很多子类,显然相当繁琐.
  
  Jive中的Decorator实现
  在论坛系统中,有些特别的字是不能出现在论坛中如"打倒XXX",我们需要过滤这些"反动"的字体.不让他们出现或者高亮度显示.
  
  在IBM Java专栏中专门谈Jive的文章中,有谈及Jive中ForumMessageFilter.java使用了Decorator模式,其实,该程序并没有真正使用Decorator,而是提示说:针对特别论坛可以设计额外增加的过滤功能,那么就可以重组ForumMessageFilter作为Decorator模式了. 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
面有两个帐号: 帐号:1 PIN:42 帐号:2 PIN:1234 原文是如下: To run the simulation above, you need to do the following: 1.Click on the "ON" button (lower right-hand corner) to turn the ATM on. 2.Enter the number of $20 bills you want to have be in the cash dispenser at the start of the simulation when you are prompted to do so, and press RETURN 3.Perform any number of sessions, as follows: 1.Click on the "Click to insert card" button to simulate inserting a card 2.Type in the card number when you are prompted to do so (see below), and press RETURN 3.Enter the PIN associated with the card (see below). Although you can use your regular keyboard, it's more fun to click on the keys on the simulated ATM keyboard as displayed. 4.Perform any number of transactions, using your mouse to click the keys on the simulated ATM keyboard. Note that the machine will simulate ejecting your card when you indicate you do not wish to perform any more transactions (unless, of course, your card is retained due to too many invalid PINs). 4.Turn off the ATM by clicking on the "OFF" button (same position as the "ON" button". Note that you cannot turn the ATM off while in the middle of a customer session. 5.The entire simulation may be repeated as many times as you want, by turning the machine ON again. For demonstration purposes, this program uses a very simple simulation of the bank, with hardwired card numbers and PIN's, etc. The following are the available cards: Card Number PIN Available accounts 1 42 Checking account #1, Savings account #2 2 1234 Checking account #1, Money market account #3 (Note that both cards link to the same checking account) All the features of the bank work - both current and available balances (initially the same) are maintained for each account and a $300 daily withdrawal limit per card is enforced.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值