详略。。。设计模式4——装饰模式。。。studying

本文通过一个具体的例子展示了装饰模式的应用——创建一个MyBufferedReader类来增强字符流的功能,包括提高读取速度和按行读取的能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设计模式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;
	}

}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值