1.意图

动态地给一个对象添加一些额外的功能.

2.适用性

  • 动态、透明的方式给单个对象添加职责。
  • 如果不适合适用子类来进行扩展的时候,可以考虑适用装饰模式。
  • 避免子类数目爆炸性增长。

3.结构

4.参与者

  • Component: 定义一个对象接口,可以给这些对象动态地添加职责.
  • ConcreteComponent: 定义一个对象,可以给这个对象添加职责.
  • Decorator: 持有一个指向Component对象的引用,并定义一个与Component的接口一致的接口.
  • ConcreteComponent: 向组件添加职责.

5.效果

1) 优点:

  • 比静态继承更灵活.
  • 避免在层次结果高层的类有太多的特征.

2) 缺点:

  • 产生许多小对象,增加对象复杂度,,不易排错.

6.模式应用

  • Java中的I/O流
  • 面向切面编程AOP(思想相似)

7.实例

1) 场景

一个软件公司由员工Employee组成,员工又分为开发人员Dev、领导Leader、经理Manager、QA部门领导QALeader。Dev职责是开发代码;QALeader职责不但能开发代码,而且能设计测试案例,写测试报告;Manager职责还能开发框架,发布版本。

2)UML图

3)代码

Employee

  1. public abstract class Employee {
  2. public abstract void doSomething();
  3. }

Dev

  1. public class Dev extends Employee {
  2. @Override
  3. public void doSomething() {
  4. // TODO Auto-generated method stub
  5. writeCode();
  6. }
  7. private void writeCode() {
  8. // TODO Auto-generated method stub
  9. System.out.println("I'm a programer,complete the code!");
  10. System.out.println("---------------------------------------");
  11. }
  12. }

Leader

  1. public abstract class Leader extends Employee {
  2. private Employee person;
  3. public Leader(Employee person) {
  4. super();
  5. this.person = person;
  6. }
  7. @Override
  8. public void doSomething() {
  9. person.doSomething();
  10. }
  11. }

Manager

  1. public class Manager extends Leader {
  2. public Manager(Employee person) {
  3. super(person);
  4. // TODO Auto-generated constructor stub
  5. }
  6. public void doSomething(){
  7. begin();
  8. super.doSomething();
  9. end();
  10. }
  11. private void begin() {
  12. // TODO Auto-generated method stub
  13. System.out.println("Design the framework!");
  14. System.out.println("---------------------------------------");
  15. }
  16. private void end() {
  17. // TODO Auto-generated method stub
  18. System.out.println("Release the verion!");
  19. System.out.println("---------------------------------------");
  20. }
  21. }

QALeader

  1. public class QALeader extends Leader {
  2. public QALeader(Employee person){
  3. super(person);
  4. }
  5. @Override
  6. public void doSomething() {
  7. // TODO Auto-generated method stub
  8. begin();
  9. super.doSomething();
  10. end();
  11. }
  12. private void end() {
  13. // TODO Auto-generated method stub
  14. System.out.println("Write the test reporter");
  15. System.out.println("---------------------------------------");
  16. }
  17. private void begin() {
  18. // TODO Auto-generated method stub
  19. System.out.println("Write the test cases!");
  20. System.out.println("---------------------------------------");
  21. }
  22. }

输出结果:
========leader1 doSomething==========
Write the test cases!
---------------------------------------
I'm a programer,complete the code!
---------------------------------------
Write the test reporter
---------------------------------------
========leader2 doSomething==========
Design the framework!
---------------------------------------
Write the test cases!
---------------------------------------
I'm a programer,complete the code!
---------------------------------------
Write the test reporter
---------------------------------------
Release the verion!
---------------------------------------

设计模式之适配器模式(Decorator)的更多相关文章

  1. 大型Java进阶专题(八)设计模式之适配器模式、装饰者模式和观察者模式

    前言 ​ 今天开始我们专题的第八课了.本章节将介绍:三个设计模式,适配器模式.装饰者模式和观察者模式.通过学习适配器模式,可以优雅的解决代码功能的兼容问题.另外有重构需求的人群一定需要掌握装饰者模式. ...

  2. 每天一个设计模式-3 适配器模式(Adapteer)

    每天一个设计模式-3 适配器模式(Adapteer) 1.现实中的情况 旧式电脑的硬盘是串口的,直接与硬盘连接,新硬盘是并口的,显然新硬盘不能直接连在电脑上,于是就有了转接线.好了,今天的学习主题出来 ...

  3. Head First 设计模式之适配器模式与外观模式

    Head First设计模式之适配器模式与外观模式 前言: 之前讲过装饰者模式,将对象包装起来并赋予新的职责,这一章我们也会将对象进行包装,只不过是让它们看起来不像自己而像是别的东西.这样就可以在设计 ...

  4. C#设计模式(7)——适配器模式(Adapter Pattern)

    一.引言 在实际的开发过程中,由于应用环境的变化(例如使用语言的变化),我们需要的实现在新的环境中没有现存对象可以满足,但是其他环境却存在这样现存的对象.那么如果将“将现存的对象”在新的环境中进行调用 ...

  5. Java(Android)编程思想笔记02:组合与继承、final、策略设计模式与适配器模式、内部类、序列化控制(注意事项)

    1.组合和继承之间的选择 组合和继承都允许在新的类中放置子对象,组合是显式的这样做,而继承则是隐式的做. 组合技术通常用于想在新类中使用现有类的功能而非它的接口这种情形.即在新类中嵌入某个对象,让其实 ...

  6. 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)

    原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...

  7. 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)

    原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...

  8. C#设计模式之七适配器模式(Adapter)【结构型】

    一.引言   从今天开始我们开始讲[结构型]设计模式,[结构型]设计模式有如下几种:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式.[创建型]的设计模式解决的是对象创建的问题, ...

  9. 8.3 GOF设计模式二: 适配器模式 Adapter

    GOF设计模式二: 适配器模式 Adapter  为中国市场生产的电器,到了美国,需要有一个转接器才能使用墙上的插座,这个转接 器的功能.原理?复习单实例模式  SingleTon的三个关键点  ...

随机推荐

  1. 《JavaScript 闯关记》之表达式和运算符

    表达式 表达式是由数字.运算符.数字分组符号(如括号).自由变量和约束变量等以能求得数值的有意义排列方法所得的组合.JavaScript 表达式主要有以下几种形式: 原始表达式:常量.变量.保留字. ...

  2. K - Digital Roots(第二季水)

    Description The digital root of a positive integer is found by summing the digits of the integer. If ...

  3. L - 辗转相除法(第二季水)

    Description The least common multiple (LCM) of a set of positive integers is the smallest positive i ...

  4. RtlInitUnicodeString、IoCreateDevice、IoCreateSymbolicLink、IoDeleteDevice 四个 API 驱动函数的使用

    要解释"驱动对象",就得先从 DriverEntry() 说起: 做过C语言开发的都知道程序是从 main() 函数开始执行.在进行 Windows 驱动程序开发的时候没有 mai ...

  5. GDAL显示线性shp文件

    http://pan.baidu.com/s/1qWIDphU  (工程文件在vs2008中编写) 1.使用到的技术 GDAL:读取矢量数据 GDI:    绘制矢量数据 2.详细解释 GDI绘图: ...

  6. (原)java中opencv的width的问题

    调试程序,我这边负责在JNI中将缓冲区中的数据转换成bitmp.测试时用320*240的图像测试正常,但是别人使用的图像宽度为270时,图像出现了错位(没截图,不好理解). 首先想到的是opencv的 ...

  7. Centos7安装JDK

    以下是gz包方式: 1,将jdk-8u51-linux-x64.tar.gz放到/usr/java目录下 2,用tar -zxvf jdk-8u51-linux-x64.tar.gz 解压到当前目录 ...

  8. AspectJ的安装和Eclipse的AJDT插件的配置

    一.安装AspectJ:1.从http://www.eclipse.org/aspectj/downloads.php  下载AspectJ(目前发布的最新版为1.8.6);2.解压下载下来的jar文 ...

  9. php 函数 将数组转换成标量变量:extract()

    格式 extract( array var_array [, int extract_type] [,string prefix] ); 例子 $array4 = array('key1'=>' ...

  10. php的多线程使用

    PHP 5.3 以上版本,使用pthreads PHP扩展,可以使PHP真正地支持多线程.多线程在处理重复性的循环任务,能够大大缩短程序执行时间. 在liunx下的安装 准备工作: 1.下载Threa ...