装饰模式(I/O流)

      装饰模式:装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案,提供比继承更多的灵活性。动态给一个对象增加功能,这些功能可以再动态的撤消。增加由一些基本功能的排列组合而产生的非常大量的功能。

       Decorator模式,它通过给对象添加装饰来动态的添加新的功能。如下是Decorator模式的UML图:

<STRONG><A href=

Java I/O 包中的Decorator模式介绍(图一)" />


  Component为组件和装饰的公共父类,它定义了子类必须实现的方法。

  ConcreteComponent是一个具体的组件类,可以通过给它添加装饰来增加新的功能。

  Decorator是所有装饰的公共父类,它定义了所有装饰必须实现的方法,同时,它还保存了一个对于Component的引用,以便将用户的请求转发给Component,并可能在转发请求前后执行一些附加的动作。

  ConcreteDecoratorA和ConcreteDecoratorB是具体的装饰,可以使用它们来装饰具体的Component。

JAVA I/O流就是使用装饰模式这种情景设计的

原文:http://www.ltesting.net/ceshi/ruanjianceshikaifajishu/rjcskfyy/java/2007/0622/61692.html

装饰模式(Decorator)原理:
装饰模式又名包装(Wrapper)模式。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。装饰模式以对客户透明的方式动态的给一个对象附加上更多的责任。换言之,客户端并不会觉得对象在装饰前和装饰后有什么不同。装饰模式可以在不创造更多子类的情况下,装饰模式把客户端的调用委派到被装饰类。装饰模式的关键在于这种扩展完全是透明的。装饰模式是在不必改变原类文件和使用继承的情况下,动态的扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象

装饰模式的角色:
--抽象构件角色(Component):给出一个抽象接口,以规范准备接收附加责任的对象。相当于I/O流的抽象类OutputStream/InputStream

--具体构件角色(Concrete Component):定义一个将要接收附加责任的类。
相当于I/O流的节点流FileOutputStream/FileInputStream

--装饰角色(Decorator):持有一个构件(Component)对象的引用,并定义一个与抽象构件接口一致的接口。相当于I/O流的FilterOutputStream/FilterInputStream

--具体装饰角色(Concrete Decotator):负责给构件对象“贴上”附加的责任。相当于I/O流的FilterOutputStream/FilterInputStream的子类(缓冲流:BufferedInputStream/BufferedOutputStream;数据流:DataOutputStream/DataInputStream)


下面使用装饰模式设计:
定义Component这个接口类似于I/O流的抽象类OutputStream
Java代码   收藏代码
  1. /* 
  2.  * 定义抽象构件角色(Component):给出一个抽象接口, 
  3.  * 以规范准备接收附加责任的对象 ,定义这个接口类似于I/O流的抽象类OutputStream/InputStream 
  4.  */  
  5. package com.decorator;  
  6.   
  7. public interface Component {  
  8.   
  9.     public void doSomething();  
  10. }  

定义ConcreteComponent这个类相当于I/O流的节点流FileOutputStream/FileInputStream
Java代码   收藏代码
  1. /* 
  2.  * 具体构件角色(Concrete Component):定义一个将要接收附加责任的类。 
  3.  */  
  4. package com.decorator;  
  5.   
  6. public class ConcreteComponent implements Component {  
  7.   
  8.     @Override  
  9.     public void doSomething() {  
  10.           
  11.         System.out.println("功能A");  
  12.     }  
  13.   
  14. }  


定义这个Decorator类相当于I/O流的过滤流(FilterOutputStream/FilterInputStream)
Java代码   收藏代码
  1. /* 
  2.  * 装饰角色(Decorator):持有一个构件(Component)对象的引用, 
  3.  * 并定义一个与抽象构件接口一致的接口。 
  4.  */  
  5. package com.decorator;  
  6.   
  7. public class Decorator implements Component {  
  8.   
  9.     private Component component;  //持有接口的引用;  
  10.       
  11.     public Decorator(Component component) {  
  12.         this.component = component;  
  13.     }  
  14.       
  15.     @Override  
  16.     public void doSomething() {  
  17.           
  18.         component.doSomething();  
  19.     }  
  20.   
  21. }  


定义下面两个类ConcteteDecorator,ConcteteDecorator2都继承了父类Decorator,相当于I/O流的(缓冲流:BufferedInputStream/BufferedOutputStream;数据流:DataOutputStream/DataInputStream)继承了过滤流FilterOutputStream/FilterInputStream
Java代码   收藏代码
  1. /* 
  2.  * 具体构件角色(Concrete Component):定义一个将要接收附加责任的类。 
  3.  */  
  4. package com.decorator;  
  5.   
  6. public class Decorator implements Component {  
  7.   
  8.     private Component component;  //持有接口的引用;  
  9.       
  10.     public Decorator(Component component) {  
  11.         this.component = component;  
  12.     }  
  13.       
  14.     @Override  
  15.     public void doSomething() {  
  16.           
  17.         component.doSomething();  
  18.     }  
  19.   
  20. }  
  21.   
  22. /* 
  23.  * 具体构件角色(Concrete Component):定义一个将要接收附加责任的类。 
  24.  */  
  25. package com.decorator;  
  26.   
  27. public class ConcteteDecorator2 extends Decorator {  
  28.   
  29.     public ConcteteDecorator2(Component component) {  
  30.         super(component);  
  31.     }  
  32.   
  33.     @Override  
  34.     public void doSomething() {  
  35.         // TODO Auto-generated method stub  
  36.         super.doSomething();  
  37.         this.doAnotherThing();  
  38.     }  
  39.       
  40.     private void doAnotherThing() {  
  41.         System.out.println("功能C");  
  42.     }  
  43. }  


结果测试
Java代码   收藏代码
  1. /* 
  2.  * 测试结果 
  3.  */  
  4. package com.decorator;  
  5.   
  6. public class Client {  
  7.   
  8.     public static void main(String[] args) {  
  9.           
  10.         //节点流  
  11.         Component component = new ConcreteComponent();  
  12.           
  13.         //过滤流  
  14.         Component component2 = new ConcteteDecorator(component);  
  15.           
  16.         //过滤流  
  17.         Component component3 = new ConcteteDecorator2(component2);  
  18.           
  19.         component3.doSomething();  
  20.           
  21.         /* 
  22.          * 以面的几行代码整合起来等同于下面这段精简代码,和I/O流的  DataOutputStream dos = new DataOutputStream(new BufferedOutputStream( 
  23.                 new FileOutputStream("data.txt")))相同 
  24.          */  
  25. //      Component component5 = new ConcteteDecorator2(new ConcteteDecorator(new ConcreteComponent()));  
  26. //      component5.doSomething();  
  27.     }  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值