装饰器模式之java IO流(FilterOutputStream对用户透明及它的作用)

例如现在有这样一个需求:读取一个文件里面的内容,将文件里的内容所有的小写字母都转换成大写的。这个时候我们就可以通过去扩展IO流去做。

package com.unis.io;  
2.  
3.import java.io.BufferedReader;  
4.import java.io.File;  
5.import java.io.FileReader;  
6.import java.io.FilterReader;  
7.import java.io.IOException;  
8.import java.io.Reader;  
9.  
10.public class UppCaseReader extends FilterReader {  
11.  
12.    public UppCaseReader(Reader in) {  
13.        super(in);  
14.    }  
15.  
16.    @Override  
17.    public int read(char[] cbuf, int off, int len) throws IOException {  
18.        int result = super.read(cbuf, off, len);   
19.        for(int i=off;i<len;i++){  
20.            if(cbuf[i]>='a'&&cbuf[i]<='z'){  
21.                cbuf[i] -= 32;  
22.            }  
23.        }  
24.        return result;  
25.    }  
26.  
27.//  @Override  
28.//  public int read() throws IOException {  
29.//      int result = super.read();  
30.//      if(result>='a'&&result<='z'){  
31.//          result-=32;  
32.//      }  
33.//      return result;  
34.//  }  
35.      
36.    public static void main(String[] args) throws IOException {  
37.        Reader reader = new BufferedReader(new UppCaseReader(new FileReader(new File("src/com/unis/io/UppCaseReader.java"))));  
38.        int i =0;  
39.        while((i=reader.read())!=-1){  
40.            System.out.print((char)i);  
41.        }  
42.    }  
43.}  

 Reader reader = new BufferedReader(new UppCaseReader(new FileReader(new File("src/com/unis/io/UppCaseReader.java"))));  

是典型的装饰器模式的运用语句。每封装一次,就添加一个新的功能。



Java装饰模式 
装饰模式:给一个类添加一些额外的职责,并且在添加这些额外的职责时不会控制该类的执行逻辑。 装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。

装饰模式以对客户端透明的方式动态的给一个对象附加上更多的责任。换言之客户端并不会觉的对象在装饰前和装饰后有什么区别。

实例化 装饰者子类的时候,是调用构建者子类来实例化,不会调用装饰者类,装饰者类对客户端来说是透明的,只是对功能的扩张。
UML类图: 

装饰模式与类继承的区别
1: 装饰模式是一种动态行为,对已经存在类进行随意组合,而类的继承是一种静态的行为,一个类定义成什么样的,该类的对象便具有什么样的功能,无法动态的改变。
2: 装饰模式扩展的是对象的功能,不需要增加类的数量,而类继承扩展是类的功能,在继承的关系中,如果我们想增加一个对象的功能,我们只能通过继承关系,在子类中增加两个方法。
3: 装饰模式是在不改变原类文件和使用继承的情况下,动态的扩展一个对象的功能,它是通过创建一个包装对象,也就是装饰来包裹真是的对象。

组成部分: 
抽象构件:原始的功能接口-----相当于i/o流里面InputStream/OutputStream和Reader/Writer
具体构件:具体的原始功能类----实现接口的类---------------相当于i/o里面的FileOutputStream和FileInputStream。
装饰角色:持有具体构件类的对象,以便执行原有功能------接口的实现类,类里调用接口的对象 --------------相当于i/o里面的FilterOutputStream和FilterInputStream
具体装饰:具体扩展的功能在这里--------------------------------相当于i/o流里面的BufferedOutputStream和BufferedInputStream以及DataOutputStream和DataInputSrtream。


[java]  view plain  copy
  1. public interface IComponent {  
  2.      void Operation();  
  3. }  

[java]  view plain  copy
  1. public class Component implements IComponent{  
  2.   
  3.     public void Operation()  
  4.     {  
  5.         System.out.println("Component Operation");  
  6.     }  
  7. }  

[java]  view plain  copy
  1. public abstract class Decorator implements IComponent {  
  2.   
  3.     protected IComponent realComponnet;  
  4.   
  5.     public Decorator(IComponent component) {  
  6.         realComponnet = component;  
  7.     }  
  8.   
  9.     @Override  
  10.     public void Operation() {  
  11.         // TODO Auto-generated method stub  
  12.         if (realComponnet != null) {  
  13.             realComponnet.Operation();  
  14.         }  
  15.   
  16.     }  
  17.   
  18. }  

[java]  view plain  copy
  1. public class AuthDecorator extends Decorator {  
  2.   
  3.     /** 
  4.      * @param component 
  5.      */  
  6.     public AuthDecorator(IComponent component) {  
  7.         super(component);  
  8.         // TODO Auto-generated constructor stub  
  9.     }  
  10.   
  11.     @Override  
  12.     public void Operation() {  
  13.         // TODO Auto-generated method stub  
  14.         System.out.println("Befor Operation Authorization");  
  15.         super.Operation();  
  16.         System.out.println("After Operation Authorization");  
  17.     }  
  18.   
  19. }  

[java]  view plain  copy
  1. public class LogDecorator extends Decorator {  
  2.   
  3.     /** 
  4.      * @param component 
  5.      */  
  6.     public LogDecorator(IComponent component) {  
  7.         super(component);  
  8.         // TODO Auto-generated constructor stub  
  9.     }  
  10.   
  11.     @Override  
  12.     public void Operation() {  
  13.         // TODO Auto-generated method stub  
  14.         System.out.println("Operation Logging");  
  15.         super.Operation();  
  16.         System.out.println("Operation Log Finished");  
  17.     }  
  18.   
  19. }  

Java IO中

DataoutputStream out=new DataoutputStream(new FileoutputStream());

这就是 装饰者模式,DataoutputStream是装饰者子类,FileoutputStream是实现接口的子类。

这里不会调用到装饰者类--FilteroutputStream,只是作为继承的另一种方案,对客户端来说是透明的,是为了功能的扩张.



 这就是为什么平时学java时,不会接触到FilteroutputStream这个类的原因

 

输出结果: 
晚上 
开车 

其实装饰者就是给对象穿马甲 多穿一层就多加一点功能


转载于:http://www.cnblogs.com/ChrisRIM/archive/2012/08/21/2648372.html



  InputStream :字节序列输入类鼻祖  ----------------------  Component
  public abstract class  InputStream  implements Closeable
 最基本的读取字节的抽象方法,供子类扩展。
  public abstract  int  read() throws  IOException  ;
  FileInputStream  读取文件中的字节流类 继承  InputStream  ------------  ConcreteComponent
  public class  FileInputStream  extends  InputStream
 构造函数
  public  FileInputStream  (String name) throws  FileNotFoundException
 本地方法,与操作系统低层交互的具体读入方法
  public  native  int  read() throws  IOException  ; ( 基于本地方法的底层实现  ,java 调用非  java 程序实现 )

  FilterInputStream  过滤流类,起装饰器作用,用于对输入装配各种功能  ------------  Decorator
  public class  FilterInputStream  extends  InputStream
 用于记录被装饰者,也就是需要装配新功能的  InputStream 对象
  protected volatile  InputStream  in;
 构造装饰器   in 用来设置需要被包装  InputStream 对象
  protected  FilterInputStream  (  InputStream  in) {  this.in  = in; // 设置需要被包装  InputStream 对象  }
 读入字节
  public  int  read() throws  IOException  { return  in.read  (); }
  BufferedInputStream  使输入流具有缓冲功能,是一种可以装配缓冲功能的装饰器,继承 FilterInputStream  ---------------------------  ConcreteDecorator
  public class  BufferedInputStream  extends  FilterInputStream
  // 构造器       in 就是被装配缓冲功能的  InputStream

public BufferedInputStream ( InputStream in) { this(in, defaultBufferSize );


转载于:http://mengzhenbin.iteye.com/blog/1395613


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值