装饰者设计模式演示示例

package com.msmiles.study;

import java.io.FileReader;
import java.io.IOException;

/**
 * 装饰者设计模式的演示
 * MyBuffereReader对FileReader进行了增强,
 * FileReader作为对象进行构造参数传入
 * MyBuffereReader称为装饰类
 */
public class MyBuffereReader {
	
	private FileReader fr;

	MyBuffereReader(FileReader fr) {
		this.fr = fr;
	}

	public String myReadLine() throws IOException {
		StringBuilder sb = new StringBuilder();
		int ch = 0;
		while ((ch = fr.read()) != -1) {
			if (ch == '\r')
				continue;
			if (ch == '\n')
				return sb.toString();
			else
				sb.append((char) ch);
		}
		if (sb.length() != 0)
			return sb.toString();
		return null;
	}

	public void myClose() throws IOException {
		if (fr != null)
			fr.close();
	}
}

 

你可能感兴趣的:(设计模式)