装饰设计模式
2014-11-20 23:07
369 查看
/* 自定义读取缓冲区 装饰设计模式: 当需要对对象进行功能增强的时候,可以定义类,将原有对象传入,基于原有功能,扩展功能 */ package myBufferedReader; import java.io.*; class MyBufferedReader { public static void main(String[] args) throws IOException { //人吃饭举例 Person p=new Person(); SuperPerson sp=new SuperPerson(p); sp.superEat(); //自定义读取缓冲区 FileReader r=new FileReader("BufferedCopyDemo.java"); MyReadLine mrl=new MyReadLine(r); System.out.println(mrl.readLine()); } } //自定义缓冲区 class MyReadLine { private FileReader r; MyReadLine(FileReader r) { this.r=r; } //读一行 public String readLine() throws IOException { StringBuilder sb=new StringBuilder(); int ch=0; while((ch=r.read())!=-1) { if(ch=='\r') continue; if(ch=='\n') return sb.toString(); sb.append((char)ch); } if(sb.length()!=0) return sb.toString(); return null; } //关闭 public void myClose() throws IOException { r.close(); } } //人类 class Person { public void eat() { System.out.println("吃饭"); } } //高级人类 class SuperPerson { Person p; SuperPerson(Person p) { this.p=p; } public void superEat() { System.out.println("开胃酒"); System.out.println("吃饭"); System.out.println("饭后果"); } }
相关文章推荐
- 装饰设计模式与代理设计模式的异同
- 装饰设计模式(decorator)与IO包
- 装饰设计模式和继承的区别?
- 装饰设计模式简析
- 装饰设计模式
- 装饰设计模式
- 装饰设计模式简析
- 装饰设计模式
- 装饰设计模式初探
- 装饰设计模式
- 让DMA66 硬盘工作在DMA33 模式
- 特效天地——淡入淡出(真彩模式)
- OpenSource产品的商业操作模式
- COM组件中的线程模式
- 打开你的 DMA 模式 (转)
- 水木-Emacs 的 texinfo 模式 (三)
- 水木-Emacs 的 texinfo 模式 (二)
- 水木-Emacs 的 texinfo 模式(一)
- 虚拟8086模式的内存管理
- 16位BIT模式下的ALPHA运算