设计模式之适配器模式(Decorator)
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类
public abstract class Employee {public abstract void doSomething();}
Dev类
public class Dev extends Employee {@Overridepublic void doSomething() {// TODO Auto-generated method stubwriteCode();}private void writeCode() {// TODO Auto-generated method stubSystem.out.println("I'm a programer,complete the code!");System.out.println("---------------------------------------");}}
Leader类
public abstract class Leader extends Employee {private Employee person;public Leader(Employee person) {super();this.person = person;}@Overridepublic void doSomething() {person.doSomething();}}
Manager类
public class Manager extends Leader {public Manager(Employee person) {super(person);// TODO Auto-generated constructor stub}public void doSomething(){begin();super.doSomething();end();}private void begin() {// TODO Auto-generated method stubSystem.out.println("Design the framework!");System.out.println("---------------------------------------");}private void end() {// TODO Auto-generated method stubSystem.out.println("Release the verion!");System.out.println("---------------------------------------");}}
QALeader类
public class QALeader extends Leader {public QALeader(Employee person){super(person);}@Overridepublic void doSomething() {// TODO Auto-generated method stubbegin();super.doSomething();end();}private void end() {// TODO Auto-generated method stubSystem.out.println("Write the test reporter");System.out.println("---------------------------------------");}private void begin() {// TODO Auto-generated method stubSystem.out.println("Write the test cases!");System.out.println("---------------------------------------");}}
输出结果:
========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)的更多相关文章
- 大型Java进阶专题(八)设计模式之适配器模式、装饰者模式和观察者模式
前言 今天开始我们专题的第八课了.本章节将介绍:三个设计模式,适配器模式.装饰者模式和观察者模式.通过学习适配器模式,可以优雅的解决代码功能的兼容问题.另外有重构需求的人群一定需要掌握装饰者模式. ...
- 每天一个设计模式-3 适配器模式(Adapteer)
每天一个设计模式-3 适配器模式(Adapteer) 1.现实中的情况 旧式电脑的硬盘是串口的,直接与硬盘连接,新硬盘是并口的,显然新硬盘不能直接连在电脑上,于是就有了转接线.好了,今天的学习主题出来 ...
- Head First 设计模式之适配器模式与外观模式
Head First设计模式之适配器模式与外观模式 前言: 之前讲过装饰者模式,将对象包装起来并赋予新的职责,这一章我们也会将对象进行包装,只不过是让它们看起来不像自己而像是别的东西.这样就可以在设计 ...
- C#设计模式(7)——适配器模式(Adapter Pattern)
一.引言 在实际的开发过程中,由于应用环境的变化(例如使用语言的变化),我们需要的实现在新的环境中没有现存对象可以满足,但是其他环境却存在这样现存的对象.那么如果将“将现存的对象”在新的环境中进行调用 ...
- Java(Android)编程思想笔记02:组合与继承、final、策略设计模式与适配器模式、内部类、序列化控制(注意事项)
1.组合和继承之间的选择 组合和继承都允许在新的类中放置子对象,组合是显式的这样做,而继承则是隐式的做. 组合技术通常用于想在新类中使用现有类的功能而非它的接口这种情形.即在新类中嵌入某个对象,让其实 ...
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...
- 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)
原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...
- C#设计模式之七适配器模式(Adapter)【结构型】
一.引言 从今天开始我们开始讲[结构型]设计模式,[结构型]设计模式有如下几种:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式.[创建型]的设计模式解决的是对象创建的问题, ...
- 8.3 GOF设计模式二: 适配器模式 Adapter
GOF设计模式二: 适配器模式 Adapter 为中国市场生产的电器,到了美国,需要有一个转接器才能使用墙上的插座,这个转接 器的功能.原理?复习单实例模式 SingleTon的三个关键点 ...
随机推荐
- Swift语法基础入门一(适合有C, OC开发人员)
Swift开发体验 /*: 创建对象 * OC: alloc initWithXXX 方法 * Swift: (xxx:) */ /*: 调用方法 * OC: [UIColor redColor]; ...
- 配置managed server
managed server往往是部署应用程序的server,所以最好在weblgoic上配置上managed server,不要把应用程序直接部署到admin server上. 一.受管服务器的创建 ...
- 使用SelectClipRgn注意事项
SelectClipRgn 函数功能:该函数选择一个区域作为指定设备环境的当前剪切区域. 函数原型:int SelectClipRgn(HDc hdc, HRGN hrgn): 参数: hdc:设备环 ...
- aspx和razor的区别
两者几乎都不懂,现在要选择一种,只能百度,然后一条一条看,也不知道诸位大神哪个说的对. 两个引擎语法完全不一样,性能上Asp.Net略占优势,语法糖则是razor的强项. 开发MVC3首选razor ...
- MYSQL 转换字符集的 2 种方法
方法 1. convert(expression using character_set); convert('123456789' using ascii); 方法 2. cast(expresio ...
- perl 调用按钮输出到文本框
sub push_b4 { #$txt -> insert('end'); #select $txt; system("expect c:\\\\expect.txt >expe ...
- Android 获取系统内置Intent
1,掉web浏览器 Uri myBlogUri = Uri.parse("http://www.yzmanga.com"); returnIt = new Intent(Inten ...
- GPS功能:百度路书自定义【轨迹回放】
如题所述:百度的编辑界面很直观,修改后就可以运行,地址:http://developer.baidu.com/map/jsdemo.htm#c2_8: 因为同事研究了一下午结果都没搞出来,他copy百 ...
- Geoserver基本使用、WMS服务发布与OpenLayers测试
1.Geoserver与OpenLayers的下载 Geoserver:http://geoserver.org/ OpenLayers:http://openlayers.org/ 2.安装部署Ge ...
- VC++6.0下通过opencv读入图像并反色
第一个opencv测试程序: 不多说,直接上代码,代码注释很详尽: ////////////////////////////////////////////////////////////////// ...