JAVA I/O流就是使用装饰模式这种情景设计的
装饰模式(Decorator)原理:
装饰模式又名包装(Wrapper)模式。装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。装饰模式以对客户透明的方式动态的给一个对象附加上更多的责任。换言之,客户端并不会觉得对象在装饰前和装饰后有什么不同。装饰模式可以在不创造更多子类的情况下,装饰模式把客户端的调用委派到被装饰类。装饰模式的关键在于这种扩展完全是透明的。装饰模式是在不必改变原类文件和使用继承的情况下,动态的扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象
装饰模式的角色:
--抽象构件角色(Component):给出一个抽象接口,以规范准备接收附加责任的对象。相当于I/O流的抽象类OutputStream/InputStream
--具体构件角色(Concrete Component):定义一个将要接收附加责任的类。
相当于I/O流的节点流FileOutputStream/FileInputStream
--装饰角色(Decorator):持有一个构件(Component)对象的引用,并定义一个与抽象构件接口一致的接口。相当于I/O流的FilterOutputStream/FilterInputStream
--具体装饰角色(Concrete Decotator):负责给构件对象“贴上”附加的责任。相当于I/O流的FilterOutputStream/FilterInputStream的子类(缓冲流:BufferedInputStream/BufferedOutputStream;数据流:DataOutputStream/DataInputStream)
下面使用装饰模式设计:
定义Component这个接口类似于I/O流的抽象类OutputStream
/*
* 定义抽象构件角色(Component):给出一个抽象接口,
* 以规范准备接收附加责任的对象 ,定义这个接口类似于I/O流的抽象类OutputStream/InputStream
*/
package com.decorator;
public interface Component {
public void doSomething();
}
定义ConcreteComponent这个类相当于I/O流的节点流FileOutputStream/FileInputStream
/*
* 具体构件角色(Concrete Component):定义一个将要接收附加责任的类。
*/
package com.decorator;
public class ConcreteComponent implements Component {
@Override
public void doSomething() {
System.out.println("功能A");
}
}
定义这个Decorator类相当于I/O流的过滤流(FilterOutputStream/FilterInputStream)
/*
* 装饰角色(Decorator):持有一个构件(Component)对象的引用,
* 并定义一个与抽象构件接口一致的接口。
*/
package com.decorator;
public class Decorator implements Component {
private Component component; //持有接口的引用;
public Decorator(Component component) {
this.component = component;
}
@Override
public void doSomething() {
component.doSomething();
}
}
定义下面两个类ConcteteDecorator,ConcteteDecorator2都继承了父类Decorator,相当于I/O流的(缓冲流:BufferedInputStream/BufferedOutputStream;数据流:DataOutputStream/DataInputStream)继承了过滤流FilterOutputStream/FilterInputStream
/*
* 具体构件角色(Concrete Component):定义一个将要接收附加责任的类。
*/
package com.decorator;
public class Decorator implements Component {
private Component component; //持有接口的引用;
public Decorator(Component component) {
this.component = component;
}
@Override
public void doSomething() {
component.doSomething();
}
}
/*
* 具体构件角色(Concrete Component):定义一个将要接收附加责任的类。
*/
package com.decorator;
public class ConcteteDecorator2 extends Decorator {
public ConcteteDecorator2(Component component) {
super(component);
}
@Override
public void doSomething() {
// TODO Auto-generated method stub
super.doSomething();
this.doAnotherThing();
}
private void doAnotherThing() {
System.out.println("功能C");
}
}
结果测试
/*
* 测试结果
*/
package com.decorator;
public class Client {
public static void main(String[] args) {
//节点流
Component component = new ConcreteComponent();
//过滤流
Component component2 = new ConcteteDecorator(component);
//过滤流
Component component3 = new ConcteteDecorator2(component2);
component3.doSomething();
/*
* 以面的几行代码整合起来等同于下面这段精简代码,和I/O流的 DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream("data.txt")))相同
*/
// Component component5 = new ConcteteDecorator2(new ConcteteDecorator(new ConcreteComponent()));
// component5.doSomething();
}
}
分享到:
相关推荐
003装饰着模式" I/ N1 V8 f( X 004单例模式) m) x8 z7 t( q5 P4 }4 u# v 005工厂模式+ [% H7 o% z% F7 b' N* n# {& K; _ 006命令模式9 D% c+ V8 l$ {* n, y/ N 007适配器模式 008外观模式5 d3 j: D% K3 ` t 009模板...
1:模式的简史和形而上学;2:统一建模语言UML简介;3:软件的可维护性与可复用性;4:开-闭 原则;5:java语言接口;6:抽象类;7:里氏代换原则;...25:装饰模式;26:设计模式在JAVA I/O设计原则;28:代理模式;....
com.javapatterns.decorator 装饰模式 com.javapatterns.dip 依赖倒转原则 com.javapatterns.doubledispatch 专题:单分派与多分派 com.javapatterns.facade 门面模式 com.javapatterns.factorymethod 工厂方法...
I/O: 简化 I/O 操作, 特别是对 I/O 流和文件的操作, for Java 5 and 6. 十. Hashing: 提供比 Object.hashCode() 更复杂的 hash 方法, 提供 Bloom filters. 十一. EventBus: 基于发布-订阅模式的组件通信,...
29.设计模式之装饰者模式 30.设计模式之外观模式 31.设计模式之代理模式 32.设计模式之观察者模式 33.设计模式之策略模式 34.设计模式之命令模式 35.设计模式之迭代器模式 36.设计模式之中介者模式 37.设计模式之享...
o m m u n i c a t i n go b j e c t)的重复模式。这些模式解决特定的设计问题,使面向对象设计更灵活、优雅,最终复用性更 好。它们帮助设计者将新的设计建立在以往工作的基础上,复用以往成功的设计方案。 一个...
装饰性的装饰性装饰物,装饰性的装饰性装饰物,类的装饰性装饰物。 伊尼萨尔州 安装Transpilador / executor ts-node npm i -g ts-node OsPadrões设计 执行主管main.ts relativo a cadadiretório ts-node main....
深入理解JavaScript系列(29):设计模式之装饰者模式 深入理解JavaScript系列(30):设计模式之外观模式 深入理解JavaScript系列(31):设计模式之代理模式 深入理解JavaScript系列(32):设计模式之观察者...
深入理解JavaScript系列(29):设计模式之装饰者模式 深入理解JavaScript系列(30):设计模式之外观模式 深入理解JavaScript系列(31):设计模式之代理模式 深入理解JavaScript系列(32):设计模式之观察者...
I/O 篇一文掌握 Linux 性能分析之内存篇一文掌握 Linux 性能分析之 CPU 篇2019-03-14Golang任务队列machinery使用与源码剖析(一)Golang任务队列machinery使用与源码剖析(二)有赞延迟队列设计golang设计模式:...
for(int i=0;i;i++){ Thread t=new Thread(inc); t.start(); t=new Thread(dec); t.start(); } } private synchronized void inc(){ j++; System.out.println(Thread.currentThread().getName()+"-inc:"+j); } ...
2分法-通用存储过程分页(top max模式)版本(性能相对之前的not in版本极大提高) 分页存储过程:排序反转分页法 优化后的通用分页存储过程 sql语句 一些Select检索高级用法 SQL server 2005中新增的排序函数及应用 ...
5.6 字符串的I/O操作 5.7 读写压缩文件 5.8 固定大小记录的文件迭代 5.9 读取二进制数据到可变缓冲区中 5.10 内存映射的二进制文件 5.11 文件路径名的操作 5.12 测试文件是否存在 5.13 获取文件夹中的文件...
11.9 用线程实现GUI和异步I/O 的结合 417 11.10 在Tkinter中使用IDLE的 Tree部件 421 11.11 在Tkinter Listbox中支持单行多值 423 11.12 在Tkinter部件之间复制Geometry方法和选项 427 11.13 在Tkinter中实现一...
如果句柄不用在I/O文件中,它是毫无用处的。 句柄是Windows用来标志应用程序中建立的或是使用的唯一整数,Windows使用了大量的句柄来标志很多对象。 一、MFC AppWizard 1、MFC(Microsoft Foundation Class,微软...
Tim O'Brien (Sonatype, Inc.) , John Casey (Sonatype, Inc.) , Brian Fox (Sonatype, Inc.) , Bruce Snyder () , Jason Van Zyl (Sonatype, Inc.) , Juven Xu () Abstract Maven权威指南是一本关于Apache Maven...
I 第一章程序和程序设计 .......................................................................................................1 §1.1 什么是程序 ?....................................................
卷 data 的文件夹 PATH 列表 卷序列号为 64ED-8C1D D:\我的酷盘\FTP\学员面试资料 │ 164个完整Java代码.zip │ J2EE综合--Struts常见错误的全面汇总.txt │ java程序员面试资料.zip │ JAVA笔试题(上海释锐)....
卷 data 的文件夹 PATH 列表 卷序列号为 64ED-8C1D D:\我的酷盘\FTP\学员面试资料 │ 164个完整Java代码.zip │ J2EE综合--Struts常见错误的全面汇总.txt │ java程序员面试资料.zip │ JAVA笔试题(上海释锐)....