展开全部
在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也e68a84e8a2ad62616964757a686964616f31333365636132就是装饰来包裹真实的对象。
举例:public class Test {
// 将内容写到另一个文件
public void write() {
String string = "测试包装模式";
try {
FileWriter fw = new FileWriter("D:/copyfile.txt");
// 用到包装模式,扩展FileWriter的功能
BufferedWriter bW = new BufferedWriter(fw);
bW.write(string);
bW.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Test test = new Test();
test.write();
}
}
执行结果: