本篇随笔主要介绍用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中实际应用举例的更多相关文章

  1. java设计模式之装饰器模式以及在java中作用

    在JAVA I/O类库里有很多不同的功能组合情况,这些不同的功能组合都是使用装饰器模式实现的,下面以FilterInputStream为例介绍装饰器模式的使用  FilterInputStream和F ...

  2. 11 IO流(八)——装饰器设计模式,Filter装饰流

    声明:本文部分图片及内容引用自:https://www.cnblogs.com/qiumingcheng/p/5219631.html java装饰器设计模式 举一个形象的例子,人可以说话,而扩音器可 ...

  3. java 实现装饰器设计模式

    package com.gylhaut.base; /** * 装饰器 * 类与类之间的关系 * 1.依赖:形参(局部变量) * 2.关联:属性 * 聚合 属性 整体和部分 不一致的生命周期 人和手 ...

  4. 设计模式学习笔记——java中常用的设计模式

    单例设计模式(Singleton Pattern) 观察者模式(Observer Pattern) 工厂模式(Factory Pattern) 策略模式(Strategy Pattern) 适配器模式 ...

  5. .NET中的装饰器设计模式

  6. 适配器设计模式初探(Java实现)

    本篇随笔主要介绍Java实现设配器设计模式. 先来看下待解决的问题: (图片转自http://blog.csdn.net/jason0539) 由上图的情况可知,欧洲壁式插座只有三足插口,如果我们想要 ...

  7. Java中MySQL事务处理举例

    实例(以sql语句中的insert语句为例) import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pr ...

  8. [译]Java 设计模式之装饰器

    (文章翻译自Java Design Pattern: Decorator – Decorate your girlfriend) 1.装饰模式的来历 让我们假设你在寻找一个女朋友.有来自像没美国中国日 ...

  9. C#中的 Attribute 与 Python/TypeScript 中的装饰器是同个东西吗

    前言 最近成功把「前端带师」带入C#的坑(实际是前端带师开始从cocos转unity游戏开发了) 某天,「前端带师」看到这段代码后问了个问题:[这个是装饰器]? [HttpGet] public Re ...

随机推荐

  1. canvas渐变

    代码: 1 /** 2 * Created by Administrator on 2016/1/29. 3 */ 4 function draw(id){ 5 var canvas = docume ...

  2. Java-API-Package:java.util

    ylbtech-Java-API-Package:java.util 1.返回顶部 1. java.util Interfaces Collection Comparator Deque Enumer ...

  3. 新手编译开发OpenWrt入门教程(自定义固件、ubuntu学习)

    转自:   http://www.znck007.com/forum.php?mod=viewthread&tid=21571 由于openwrt编译教程资料很多,不同的cpu芯片只需要选择对 ...

  4. CPU, PSU, SPU的区别

    It all started in January 2005 with Critical Patch Updates (CPU).  Then Patch Set Updates (PSU) were ...

  5. VotingClassifier

    scores : array of float, shape=(len(list(cv)),) Array of scores of the estimator for each run of the ...

  6. 如何判断python的数据类型,用type函数

    用 type 函数 In [29]:  type(dataset) Out[29]: list 查询list的行数 In [38]: len(dataset) In [39]: Out[38]: 36 ...

  7. leetcode653

    class Solution { public: bool findTarget(TreeNode* root, int k) { queue<TreeNode> Q; vector< ...

  8. linux&nbsp;dev/dsp&nbsp;声卡学习笔记

    原文地址:dev/dsp 声卡学习笔记">linux dev/dsp 声卡学习笔记作者:ziyou飞翔       无论是从声卡读取数据,或是向声卡写入数据,事实上都具有特定的格式(f ...

  9. day17 10.jdbc的crud操作

    每次都是注册驱动,获取连接,然后执行.每次都写很累,肯定能抽取出来一些东西.Java里面是这样的,相同的东西可以抽取做成一个方法.用的时候调这方法就OK了.这方法抽取到什么程度呢? package c ...

  10. css知多少(3)——样式来源与层叠规则(转)

    css知多少(3)——样式来源与层叠规则   上一节<css知多少(2)——学习css的思路>有几个人留言表示思路很好.继续期待,而且收到了9个赞,我还是比较欣慰的.没看过的朋友建议先去看 ...