装饰者模式(二)

2014-11-23 21:44:05 · 作者: · 浏览: 12
)); } public int read(byte[] b, int offset, int len) throws IOException { int result = super.read(b, offset, len); for (int i = offset; i < offset+result; i++) { b[i] = (byte)Character.toLowerCase((char)b[i]); } return result; } }
再来个测试类:

import java.io.*;

public class InputTest {
	public static void main(String[] args) throws IOException {
		int c;

		try {
			InputStream in = 
				new LowerCaseInputStream(
					new BufferedInputStream(
						new FileInputStream("test.txt")));

			while((c = in.read()) >= 0) {
				System.out.print((char)c);
			}

			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

对吧,是不是很灵活,自己需要什么条件,写个装饰者类就可以动态改变了。

更多设计模式讲解,关注我吧,我会不时地更新,还有我还会发一些其他如java方法、框架、Linux等一些的心得,我会努力成为大神,并帮助大家一起成为大神的。大笑