详略。。。设计模式4——装饰模式。。。studying
来源:互联网 发布:淘宝竞争情报 编辑:程序博客网 时间:2024/05/17 17:42
设计模式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;}@Overridepublic 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;}@Overridepublic void close() throws IOException {fr.close();}@Overridepublic int read(char[] cbuf, int off, int len) throws IOException {return 0;}}
0 1
- 详略。。。设计模式4——装饰模式。。。studying
- 详略。。设计模式1——单例。。。。studying
- 详略。。设计模式2——单例变形(多例).。。。studying
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式——装饰设计模式
- 设计模式——装饰设计模式
- 设计模式——装饰设计模式
- 详解。。。设计模式2——工厂。。。studying
- 详解。。。设计模式3——值对象。。。studying
- 详解。。设计模式5——DAO。。studying
- 【设计模式】——Decorator装饰模式
- 每日设计模式——装饰模式
- 设计模式——装饰者模式
- 设计模式——装饰模式
- JPanel中添加一个新的JPanel,结果新的JPanel会自动占满父JPanel
- UNIX环境高级编程(4):UNIX标准化及实现(2)
- ios_按钮圆角边设置
- 开源项目
- 安装完Source Insight后出现a valid serial number was not entered
- 详略。。。设计模式4——装饰模式。。。studying
- Android中的缩略图加载-不浪费一点多余的内存
- java urlrewrite 使用
- OJ刷题---弟弟的作业
- Vim命令使用总结
- java join的用法
- Kuhn_Munkres最大权匹配算法C++模板
- SysBench 0.5 安装
- Add Binary