设计模式4——装饰模式
★ 场景和问题
在不对原有对象类进行修改的基础上,如何给一个或多个已有的类对象提供增强额外的功能?
★ 引例
写一个MyBufferedReader类,使它能够对字符流(如FileReader、InputStreamReader和PipedReader等)进行功能增强:
(1) 提供带缓冲的myRead()方法,对原有的read()方法进行增速;
(2)提供一个能够每次读取一行字符的myReadLine()方法。
图片解释:
Java代码实现思想:
简易版:
package cn.hncu.patterns.exercise.decorator;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test {
static String dir = "a.txt";// 相对路径
/*
* 这里只是为了演示,
* 没有关流
*/
public static void main(String[] args) {
try {
// read();
// myRead();
// readLine();
myReadLine();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void myReadLine() throws IOException {
MyBufferedReader br = new MyBufferedReader(new FileReader(dir));
String aline = "";
while ((aline = br.myReadLine()) != null) {
System.out.println(aline);
}
}
private static void readLine() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(dir));
String aline = "";
while ((aline = br.readLine()) != null) {
System.out.println(aline);
}
}
private static void myRead() throws IOException {
MyBufferedReader br = new MyBufferedReader(new FileReader(dir));
int ch = 0;
while ((ch = br.myRead()) != -1) {
System.out.print((char) ch);
}
System.out.println();
}
private static void read() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(dir));
int ch = 0;
while ((ch = br.read()) != -1) {
System.out.print((char) ch);
}
System.out.println();
}
}
package cn.hncu.patterns.exercise.decorator;
import java.io.Closeable;
import java.io.FileReader;
import java.io.IOException;
/*
* 装潢模式本质:
* 对某个类功能的加强。
*/
public class MyBufferedReader implements Closeable {
/*
* buf-缓存,用来提速
* fr-目标类
* count-记录缓存中数据的个数
* position-记录缓存中数据的位置
*/
private char[] buf = new char[1024];
private FileReader fr = null;
private int count = 0;
private int position = 0;
/*
* 模仿API中的BufferedReader类的构造函数
*/
public MyBufferedReader(FileReader fr) {
this.fr = fr;
}
/*
* 模仿API中的BufferedReader类中的函数,
*/
public int myRead() throws IOException {
if (count == 0) {
count = fr.read(buf);
position = 0;
}
if (count < 0) {
return -1;
}
char ch = buf[position];
position++;
count--;
return (int) ch;
}
public String myReadLine() throws IOException {
StringBuffer sb = new StringBuffer();
int ch = 0;
while ((ch = myRead()) != -1) {
/*
* 回车换行符不要
*/
if (ch == '\r') {
continue;
}
if (ch == '\n') {
return sb.toString();
}
sb.append((char) ch);
}
if (sb.length() != 0) {
return sb.toString();
}
return null;
}
@Override
public void close() throws IOException {
fr.close();
}
}
演示图片:
测试文件:
BufferedReader---read()
BufferedReader---readLine()
MyBufferedReader---myRead()
MyBufferedReader---myReadLine()
实现版:
package cn.hncu.patterns.exercise.decorator;
import java.io.Closeable;
import java.io.IOException;
import java.io.Reader;
/*
* 装潢模式本质:
* 对某个类功能的加强。
*/
public class MyBufferedReader extends Reader implements Closeable {
/*
* buf-缓存,用来提速
* fr-目标类
* count-记录缓存中数据的个数
* position-记录缓存中数据的位置
*/
private char[] buf = new char[1024];
private Reader fr = null;
private int count = 0;
private int position = 0;
/*
* 模仿API中的BufferedReader类的构造函数
*/
public MyBufferedReader(Reader fr) {
this.fr = fr;
}
/*
* 模仿API中的BufferedReader类中的函数,
*/
public int myRead() throws IOException {
if (count == 0) {
count = fr.read(buf);
position = 0;
}
if (count < 0) {
return -1;
}
char ch = buf[position];
position++;
count--;
return (int) ch;
}
public String myReadLine() throws IOException {
StringBuffer sb = new StringBuffer();
int ch = 0;
while ((ch = myRead()) != -1) {
/*
* 回车换行符不要
*/
if (ch == '\r') {
continue;
}
if (ch == '\n') {
return sb.toString();
}
sb.append((char) ch);
}
if (sb.length() != 0) {
return sb.toString();
}
return null;
}
@Override
public void close() throws IOException {
fr.close();
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
return 0;
}
}