io流中的装饰模式对理解io流的重要性

 为了说明 io流中的装饰者模式对理解io流的重要性,我想先简要介绍以下io的装饰模式。

 

  装饰(decorator)你也可以翻译成修饰。比如:一个会精通化学数学的物理学家。在这个"物理学家"前面有两个修饰语分别是化学和数学。这两个修饰语就相当于把物理学家这个人让他又有了两项本领(功能)也就是会数学和化学。同样的装饰模式也就是不断给concrete Component增加修饰语,以扩展它自身的功能。

  装饰模式简介:

  Decorator Pattern的四大角色:
1.Component (抽象构件 一般是一个接口)【在io中就是Inputstream Outputstream Reader Writer】
2.ConcreteComponent(具体构件,要实现Component接口)【在io流中也就是Component的接口的子类减去Decoratoar本身及其子类】
3.Decorator(抽象装饰,一般是一个具体类,因为他有抽象的功能在里面,所有叫抽象装饰,要实现Component接口)[在io中就是FilterInputStream FilterOutputStream FilterReader ]不包括FilterWriter因为这个抽象类,没有任何子类。
4.ConcreteDecorator(具体装饰) 【在io中也就是上述所述Decorator的子类】
编程步骤:
1.写一个接口:Component
2.写一个类ConcreteComponent实现Component
3.写一个类Decorator实现Component
4.写一个ConcretDecorator实现Component
5.在实际中这个ConcreteDecorator一般最少写三个即Decorator1  Decorator2   Decorator3  他们之间是继承关系。
代码如下:
1.Component :

public interface Component {
void operation();
}
2.ConcreteComponent:
ublic class ConcreteComponent implements Component {
 
@Override
public void operation() {
// TODO Auto-generated method stub
 
}
 
}
3.Decorator:
 
public class Decorator implements Component {
public  Component component;
 
public Decorator(Component component) {
this.component = component;
}
public Decorator() {
// TODO Auto-generated constructor stub
}
@Override
public void operation() {
// TODO Auto-generated method stub
System.out.println("I can do everything");
}
 
}
4.ConcreteDecorator :Fish
public class Fish extends Decorator {
public Fish(){
 
}
public Fish(Component component){
this.component=component;
}
@Override
public void operation() {
// TODO Auto-generated method stub
super.operation();
System.out.println("fish can swim in the water ");
}
 
}
5.ConcreteDecorator : Bird
public class Bird extends Fish {
public Bird(){
 
}
public Bird(Component component){
this.component=component;
}
@Override
public void operation() {
// TODO Auto-generated method stub
super.operation();
System.out.println("virtue people like bird ,the can overview all the landscape ");
 
}
 
}
6.ConcreteDecorator Snipe
public class Snipe extends Bird {
public Snipe() {
// TODO Auto-generated constructor stub
}
public Snipe(Component component){
this.component=component;
}
@Override
public void operation() {
// TODO Auto-generated method stub
super.operation();
System.out.println("Snipe is very cuddly and shewd animal");
}
 
}
7.Test 测试类:
public class Test {
 
public static void main(String[] args) {
// TODO Auto-generated method stub
Fish fish=new Fish(new ConcreteComponent());
Bird bird=new Bird(fish);
new Snipe(bird).operation();
}
 
 8.上述代码的输出结果为:
I can do everything
fish can swim in the water 
virtue people like bird ,the can overview all the landscape 
Snipe is very cuddly and shewd animal
 
而在io中设计者正式充分用了这个装饰模式对concreteComponent进行不断扩充的。
例如几个最常见的装饰:
E:\\Test.java表示一个路径
1.读取写入文件的装饰:BufferedReader in=newBufferedReader(new FileReader("E:\\Test.java"));  BufferedWriter out=new BufferedWriter(new FileWriter("E:\\Test.java"))
2.给读取写入的文件前加上编号:只需给1中的前面在加上一个修饰即:LineNumberReader即可。也就是:new LineNumberReader(in)和new LineNumberReader(out);
3.读取写入压缩文件的修饰:
  写一个压缩文件的修饰如下:
//write a file E:\\Ouyangfeng.gz

FileOutputStream f=new FileOutputStream("E:\\Ouyangfeng.gz");
CheckedOutputStream csum=new CheckedOutputStream(f,new Adler32());
ZipOutputStream zip=new ZipOutputStream(csum);
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(zip));

读一个压缩文件的修饰如下:

//Read file ouyangfeng.gz
FileInputStream fi=new FileInputStream("E:\\ouyangfeng.gz");
CheckedInputStream csumi=new CheckedInputStream(fi, new Adler32());
ZipInputStream zis=new ZipInputStream(csumi);
BufferedReader in2=new BufferedReader(new InputStreamReader(zis));

 .....

当然io流中的修饰远不止这些,但是我们只要知道这种装饰的思想就可以轻松的驾驭它。

下面我想把我写的压缩文件的程序分享给大家希望大家能掌握这种思想:

代码如下:

package com.qls.compression;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipCompressionTest5 {

public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
//把普通文本文件放入压缩文件中
FileOutputStream f=new FileOutputStream("E:\\ouyangfeng.gz");
CheckedOutputStream csum=new CheckedOutputStream(f, new Adler32());
ZipOutputStream zos=new ZipOutputStream(csum);
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(zos));
for(int i=1;i<=4;i++){
String str="E:\\Test"+i+".java";
zos.putNextEntry(new ZipEntry(str));
String s;
BufferedReader in=new BufferedReader(new FileReader(str));
while((s=in.readLine())!=null){
out.write(s+System.getProperty("line.separator"));
// out.newLine();//实现换行。

}
in.close();
out.flush();
}
out.close();
//Read file ouyangfeng.gz
FileInputStream fi=new FileInputStream("E:\\ouyangfeng.gz");
CheckedInputStream csumi=new CheckedInputStream(fi, new Adler32());
ZipInputStream zis=new ZipInputStream(csumi);
BufferedReader in2=new BufferedReader(new InputStreamReader(zis));
/*//第一种方法
ZipEntry ze;
while( (ze=zis.getNextEntry())!= null){
System.out.println("Reading file: "+ze);
String s;
while((s=in2.readLine())!=null){
System.out.println(s);
}
int x;
while((x=in2.read())!=-1){
System.out.print((char)x);
}
}*/
//第二种方法
ZipFile zf=new ZipFile("E:\\ouyangfeng.gz");
Enumeration<ZipEntry> e = (Enumeration<ZipEntry>) zf.entries();
while(e.hasMoreElements()){
ZipEntry ze = e.nextElement();
System.out.println("reading file:"+ze);
BufferedReader br=new BufferedReader(new FileReader(ze.toString()));
String s;
while((s=br.readLine())!=null){
System.out.println(s);
}

}
}

}

 

 
posted @ 2016-11-25 11:57  技术让世界更精彩  阅读(393)  评论(0编辑  收藏  举报