标签:style blog color io os ar java sp div
装饰设计模式:当需要对某些类功能进行增强时,可以把这个类的对象传到自定义类中,然后利用此对象具备的方法在此基础之上进行功能的增强。
与继承的区别:扩展性强一些
代码举例:
import java.io.*; class MyBufferedReader { private FileReader fileReader; public MyBufferedReader(FileReader fileReader) { this.fileReader = fileReader; } public String MyReadLine()throws IOException { StringBuilder sb = new StringBuilder(); int readNum = 0; while((readNum = fileReader.read()) != -1) { char readChar = (char)readNum; if(readChar ==‘\n‘ || readChar == ‘\r‘) return sb.toString(); else sb.append(readChar); } return null; } }
标签:style blog color io os ar java sp div
原文地址:http://www.cnblogs.com/fantasy01/p/3999576.html