装饰者模式

interface Worker{
	public void doSomeWork();
}


class Plumber implements Worker{
	public void doSomeWork(){
		System.out.println("水工");
	}
}


class Carpenter implements Worker{
	public void doSomeWork(){
		System.out.println("木匠");
	}
}


class AWorker implements Worker{
	private Worker worker;
	public AWorker(Worker worker){
		this.worker = worker;
	}
	public void doSomeWork(){
		System.out.println("hello");
		worker.doSomeWork();
	}
}


class Test01{
	public static void main(String[] args){
		//生成一个A公司水管工对象
		Plumber plumber = new Plumber();
		AWorker aWorker = new AWorker(plumber);
		aWorker.doSomeWork();
	}
}


————————————————————————————————————————
import java.io.*;

class Test{
	public static void main(String[] args){
		FileReader fileReader = null;
		BufferedReader bufferedReader = null;
		try{
			fileReader = new FileReader("g:/javaTest/buffer/user.txt");
			bufferedReader = new BufferedReader(fileReader);//装饰者模式
			String line = null;
			while(true){
				line = bufferedReader.readLine();
				if(line == null){
					break;
				}
				System.out.println(line);
			}
		}
		catch(Exception e){
			System.out.println(e);
		}
		finally{
			try{
				fileReader.close();
				bufferedReader.close();
			}
			catch(Exception e){
				System.out.println(e);
			}
		}
	}

节点流:FileReader(被装饰者)
处理流:BufferedReader(装饰者),用来装饰节点流,给节点流添加更多功能。

你可能感兴趣的:(java)