Decorator Pattern (装饰者模式)
装饰者模式( Decorator Pattern )
意图 : 动态的给一个对象添加一些额外的功能,IO这块内容体现出了装饰模式,Decorator模式相比生成子类更为灵活。
角色 :
1)抽象构件角色(Component)--- 定义成一个接口类型
2)具体构件角色 (ConcreteComponent) --- 该类(被装饰者)实现了 Component 接口,
3)装饰角色 (Decorator) --- 该类实现了 Component 接口,并持有 Component接口的引用
4)具体装饰角色 (ConcreteDecorator) --- 该类继承了装饰类
UML实现:

代码实现:
Component.java
- package com.decorator ;
- //抽象构件角色
- public interface Component
- {
- public void operation() ;
- }
ConcreteComponent.java
- package com.decorator ;
- //具体构件角色
- public class ConcreteComponent implements Component
- {
- public void operation()
- {
- System.out.println("实现功能A") ;
- }
- }
Decorator.java
- package com.decorator ;
- //装饰角色,持有一个构件角色的引用
- public class Decorator implements Component
- {
- Component component = null ;
- public Decorator(Component component)
- {
- this.component = component ;
- }
- public void operation()
- {
- this.component.operation() ;
- }
- }
ConcreteDecoratorA.java
- package com.decorator ;
- //具体装饰角色A
- public class ConcreteDecoratorA extends Decorator
- {
- public ConcreteDecoratorA(Component component)
- {
- super(component) ;
- }
- public void operation()
- {
- super.operation() ;
- System.out.println("实现功能B") ;
- }
- }
ConcreteDecoratorB.java
- package com.decorator ;
- //具体装饰角色B
- public class ConcreteDecoratorB extends Decorator
- {
- public ConcreteDecoratorB(Component component)
- {
- super(component) ;
- }
- public void operation()
- {
- super.operation() ;
- System.out.println("实现功能C") ;
- }
- }
Client.java
- package com.decorator ;
- public class Client
- {
- public static void main(String[] args)
- {
- //装饰者一般不用出现在客户端 , 因它内部自己会处理
- //ConcreteComponent cc = new ConcreteComponent() ;
- //ConcreteDecoratorA cd = new ConcreteDecoratorA(cc) ;
- //ConcreteDecoratorB cd2 = new ConcreteDecoratorB(cd) ;
- //cd2.operation() ;
- //上面的代码等价于下面的代码
- ConcreteDecoratorB cd = new ConcreteDecoratorB(new ConcreteDecoratorA(new ConcreteComponent())) ;
- cd.operation() ;
- }
- }
小结:
装饰者和被装饰者拥有共同的接口;
装饰者一般不用客户端去调用 , 因它内部自己会处理;
可以用一个或多个装饰者去包装一个对象,具体装饰类和装饰类可以组合成多种行为;
Decorator Pattern (装饰者模式)的更多相关文章
- Decorate Pattern 装饰者模式
装饰模式的定义: 动态地将责任附加到对象向,若要扩展功能,装饰模式提供了比继承更有弹性的替代方案. 遵循的设计原则是开闭原则,也是对扩展开放,对修改关闭. 下面是类图 示例代码 /** *定义被装饰者 ...
- Decorator(装饰)模式
1. 概述 若你从事过面向对象开发,实现给一个类或对象增加行为,使用继承机制,这是所有面向对象语言的一个基本特性.如果已经存在的一个类缺少某些方法,或者须要给方法添加更多的功能(魅力),你也许会仅仅继 ...
- 浅谈设计模式--装饰者模式(Decorator Pattern)
挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...
- java之装饰器模式
Decorator Pattern(装饰器模式),定义:Attach additional responsibilities to an object dynamically. Decorators ...
- 来杯咖啡-装饰者模式(Decorator)
前言 上篇[观察者模式]发布已经近一个月了,个人感觉反应并不太理想,因为大家响应都不是很积极,不知是文章那里写得有问题,而且也没有人提出过有价值的改进建议,多少感觉有些失望L!因为工作繁忙,所以不可能 ...
- [C++设计模式] decorator 装饰者模式
<head first>中 的样例:咖啡店有各种咖啡饮料,能够往咖啡里面加各种调料变成还有一种饮料.假设使用继承的方式来为每一种饮料设计一个类,代码的复杂度非常easy膨胀,并且会继承父类 ...
- PHP设计模式之装饰器模式(Decorator)
PHP设计模式之装饰器模式(Decorator) 装饰器模式 装饰器模式允许我们给一个类添加新的功能,而不改变其原有的结构.这种类型的类属于结构类,它是作为现有的类的一个包装 装饰器模式的应用场景 当 ...
- 设计模式系列之装饰模式(Decorator Pattern)
装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装.这种模式创建了一个装饰类,用来包装原 ...
- 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)
在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...
随机推荐
- Matrix-tree定理 spoj HIGH
Matrix-tree定理,给出一个无向图,问求出的生成树方案有多少种方案,利用Matrix-tree定理,主对角线第i行是i的度数,(i,j) 值为i和j之间边的数量,然后删去第一行第一列,利用初等 ...
- Codeforces Round #262 (Div. 2) E. Roland and Rose 暴力
E. Roland and Rose Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...
- Linux使用C语言链接MsSQL
1.安装gcc编译器 yum install gcc 2.下载freetds wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched ...
- Colorful Lecture Note
Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorithm lectu ...
- 两个不同网段的PC直连是否可以Ping通,肯定可以Ping的通(转)
在这一篇文章中http://blog.csdn.net/zhangdaisylove/article/details/46892917的案例,明确的说明两个不同网段的PC不能Ping的通,其实他给出的 ...
- KD100遥控生成仪
KD100是KEYDIY公司开发的一个强大的车用/民用遥控器生成工具,所生成的遥控器都具备不重码,质量稳定的特点. 通过采用英飞凌和NXP等公司开发的超级芯片,KD100巧妙的解决了各类型遥控器的兼容 ...
- mOByDiC E90C2600 EOBD/OBDII to RS232 gateway
http://www.ozenelektronik.com/downs/pdf/oe90c2600.pdf Features • Compatible with EOBD/OBDII standard ...
- linux下的系统调用函数到内核函数的追踪
http://blog.csdn.net/maochengtao/article/details/23598433
- Framebuffer重要结构体说明
l fb_var_screeninfo:记录了帧缓冲设备和指定显示模式的可修改记录.包括屏幕的分辨率,像素信息和一些时序变量 struct fb_var_screeninfo { __u32 xre ...
- Multiline ComboBox
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...