装饰器设计模式初探及Java中实际应用举例
本篇随笔主要介绍用Java实现简单的装饰器设计模式:
先来看一下装饰器设计模式的类图:
从图中可以看到,我们可以装饰Component接口的任何实现类,而这些实现类也包括了装饰器本身,装饰器本身也可以再被装饰。
下面是用Java实现的简单的装饰器设计模式,提供的是从基本的加入咖啡入手,可以继续加入牛奶,巧克力,糖的装饰器系统。
interface Component {
void method();
}
class Coffee implements Component { @Override
public void method() {
// TODO Auto-generated method stub
System.out.println("倒入咖啡");
} }
class Decorator implements Component {
public Component comp;
public Decorator(Component comp) {
this.comp = comp;
}
@Override
public void method() {
// TODO Auto-generated method stub
comp.method();
} }
class ConcreteDecorateA extends Decorator {
public Component comp;
public ConcreteDecorateA(Component comp) {
super(comp);
this.comp = comp;
}
public void method1() {
System.out.println("倒入牛奶");
}
public void method2() {
System.out.println("加入糖 ");
}
public void method() {
super.method();
method1();
method2();
}
}
class ConcreteDecorateB extends Decorator {
public Component comp;
public ConcreteDecorateB(Component comp) {
super(comp);
this.comp = comp;
}
public void method1() {
System.out.println("加入巧克力");
}
public void method() {
super.method();
method1();
}
}
public class TestDecoratePattern {
public static void main(String[] args) {
Component comp = new Coffee();
comp.method();
System.out.println("--------------------------------------------------");
Component comp1 = new ConcreteDecorateA(comp);
comp1.method();
System.out.println("--------------------------------------------------");
Component comp2 = new ConcreteDecorateB(comp1);
comp2.method();
System.out.println("--------------------------------------------------");
Component comp3 = new ConcreteDecorateB(new ConcreteDecorateA(new Coffee()));
comp3.method();
System.out.println("--------------------------------------------------");
Component comp4 = new ConcreteDecorateA(new ConcreteDecorateB(new Coffee()));
comp4.method();
}
}
运行结果:
装饰器设计模式可以使得我们自由的,以任意顺序导入巧克力,牛奶,咖啡和糖。可以实现多层,任意顺序的装饰。
Java中实际应用举例:
1、java io流
以下一句代码即体现了装饰器设计模式的应用:
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(new File(filePath), true)));
PrintWriter类及BufferedWriter类就相当于上面装饰器设计模式类图中的ConcreteDecorateA 与 ConcreteDecorateB,FileWriter类则相当于上面类图中的ConcreteComponent类,PrintWriter类的构造器实际接受的是一个Writer类的对象,在这里即为BufferedWriter类的对象,然后对这个Writer类的write方法进行装饰。
2、web应用中在filter类中实现自定义的输入输出
filter类实现如下:
public class AllFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
} @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//自定义输出流
ServletResponse compressionResponse = new CompressionResponseWrapper((HttpServletResponse) servletResponse); //把自定义的输出流传递到用户实现的servlet中去
filterChain.doFilter(servletRequest, compressionResponse); } @Override
public void destroy() {
}
}
其中自定义的输出流 CompressionResponseWrapper 类就是装饰器设计模式的一个应用。CompressionResponseWrapper类实现如下:
public class CompressionResponseWrapper extends HttpServletResponseWrapper {
private HttpServletResponse response;
public CompressionResponseWrapper(HttpServletResponse response) {
super(response);
this.response = response;
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
System.out.println("在这里可对输出流进行定制操作,例如进行压缩,返回压缩后的新的输出流");
return response.getOutputStream();
}
}
这里CompressionResponseWrapper类相当于上述装饰器设计模式类图中的ConcreteDecorateA类,HttpServletResponse类则相当于待装饰的接口。CompressionResponseWrapper类还可以再被装饰添加其他功能,这就是装饰器设计模式的强大之处。
装饰器设计模式初探及Java中实际应用举例的更多相关文章
- java设计模式之装饰器模式以及在java中作用
在JAVA I/O类库里有很多不同的功能组合情况,这些不同的功能组合都是使用装饰器模式实现的,下面以FilterInputStream为例介绍装饰器模式的使用 FilterInputStream和F ...
- 11 IO流(八)——装饰器设计模式,Filter装饰流
声明:本文部分图片及内容引用自:https://www.cnblogs.com/qiumingcheng/p/5219631.html java装饰器设计模式 举一个形象的例子,人可以说话,而扩音器可 ...
- java 实现装饰器设计模式
package com.gylhaut.base; /** * 装饰器 * 类与类之间的关系 * 1.依赖:形参(局部变量) * 2.关联:属性 * 聚合 属性 整体和部分 不一致的生命周期 人和手 ...
- 设计模式学习笔记——java中常用的设计模式
单例设计模式(Singleton Pattern) 观察者模式(Observer Pattern) 工厂模式(Factory Pattern) 策略模式(Strategy Pattern) 适配器模式 ...
- .NET中的装饰器设计模式
- 适配器设计模式初探(Java实现)
本篇随笔主要介绍Java实现设配器设计模式. 先来看下待解决的问题: (图片转自http://blog.csdn.net/jason0539) 由上图的情况可知,欧洲壁式插座只有三足插口,如果我们想要 ...
- Java中MySQL事务处理举例
实例(以sql语句中的insert语句为例) import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pr ...
- [译]Java 设计模式之装饰器
(文章翻译自Java Design Pattern: Decorator – Decorate your girlfriend) 1.装饰模式的来历 让我们假设你在寻找一个女朋友.有来自像没美国中国日 ...
- C#中的 Attribute 与 Python/TypeScript 中的装饰器是同个东西吗
前言 最近成功把「前端带师」带入C#的坑(实际是前端带师开始从cocos转unity游戏开发了) 某天,「前端带师」看到这段代码后问了个问题:[这个是装饰器]? [HttpGet] public Re ...
随机推荐
- DCloud-流应用:杂项
ylbtech-DCloud-流应用:杂项 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回 ...
- final,finally和finalize三者的区别和联系
对于初学者而言(当然也包括我)对于这三者真的不是很陌生,经常会看到它们.但对于三者之间的区别和联系一直是懵懵懂~~ 今天心情不错,那就简单总结一下它们几个的区别和联系吧.如果又不对的地方欢迎批评指正~ ...
- iOS下拉图片放大
效果图 开始简单的代码过程 其实思路很简单 就是 让tableView偏移 一图片的高度,然后在把图片添加到tableView中,然后再监听didScrollView,在里面改变图片的frame - ...
- 如何解决SSH登录Solaris主机速度慢的问题
SSH登录速度慢可能有多种原因. 1. 与DNS有关 缺省情况下,当客户端用SSH登录solaris服务器时,服务器会试图反向解析客户端的IP 地址(即把IP地址解析成机器名).如果Solaris系统 ...
- 斐波那契数列-java实现
1,1,2,3,5,8,13,21...... 以上的数列叫斐波那契数列,今天的面试第一题,输出前50个,这里记录下. 方式一 package com.geenk.demo.my; /** * @au ...
- The Independent JPEG Group's JPEG software Android源码中 JPEG的ReadMe文件
The Independent JPEG Group's JPEG software========================================== README for rele ...
- extends与implements
implements一般是实现接口. extends 是继承类. 接口一般是只有方法声明没有定义的, 那么java特别指出实现接口是有道理的,因为继承就有感觉是父类已经实现了方法,而接口恰恰是没有实现 ...
- matlab基础功能实践
一.matlab在高等数学中的应用(<数学建模算法与应用>P453) 1.求极限 syms x b=limit((sqrt(1+x^2)-1)/(1-cos(x))) syms x a b ...
- 【总结整理】json数据请求简化版理解(祺哥的成果)
在同源js目录下新建.txt文件 { "news":[ {"title":"审计管理","time":"201 ...
- SQL查询语句 [2]
一.快捷查询 快捷查询方式是一种多字段查询的简化写法,在多个字段之间用'|'隔开表示OR,用'&'隔开表示 AND. 1.不同字段相同查询条件 在 Home/controller/UserC ...