处理流和装饰者模式

处理流使用实例:BufferedReader  字符输入处理流

BufferedReader类里面有个方法叫readLine(),读取一行数据并返回这行数据

生成BufferedReader对象的方法:

BufferedReader br = new BufferedReader(new FileReader(“C:\”))

import java.io.*;
class Test{
	public static void main(String args[]){
		FileReader fr = null;
		BufferedReader br =null;
		try{
			fr = new FileReader("F:/Android/Java4Android/34/src/users.txt");
			br = new BufferedReader(fr);
			String s = br.readLine();
			System.out.println(s);
		}
		catch(Exception e){
			System.out.println(e);
		}
		finally{
			try{
				fr.close();
				br.close();
			}
			catch(Exception e){
				System.out.println(e);
			}
		}
	}
}

处理流和装饰者模式_第1张图片

 

装饰者模式举例:

处理流和装饰者模式_第2张图片

interface Worker{
	public void doSomeWork();
}
class Carpenter implements Worker{
	public void doSomeWork(){
		System.out.println("修门窗");
	}
}
class Plumber implements Worker{
	public void doSomeWork(){
		System.out.println("修水管");
	}
}
class Aworker implements Worker{
	private Worker worker;
	public Aworker(Worker aworker){
		this.worker = aworker;
	}
	public void doSomeWork(){
		System.out.println("你好");
		worker.doSomeWork();
	}
}
class Test{
	public static void main(String args[]){
		Worker carpenter = new Carpenter();
		Aworker carpenterWorker = new Aworker(carpenter);
		carpenterWorker.doSomeWork();
		
		Worker plumber = new Plumber();
		Aworker plumberWorker = new Aworker(plumber);
		plumberWorker.doSomeWork();
	}
}

处理流和装饰者模式_第3张图片

  

 

你可能感兴趣的:(装饰者模式)