【design pattern】结构型模式之—装饰者模式(Decorator)
来源:互联网 发布:男穿紧身牛仔裤知乎 编辑:程序博客网 时间:2024/06/16 10:19
Motivation
你可以通过继承,静态的扩展对象的功能(编译期);然而有时候你需要在对象被使用的时候,动态的扩展对象的功能(运行期);这个时候你就需要装饰者模式。
Intent
装饰者模式的目的是 动态地为 一个对象 增加额外的职责;
Implement
UML 图
解释:
Component: 被装饰对象的父类接口
ConcreteComponent:被装饰的具体实现类
Decorator:装饰者接口,包含了一个对 Component 的引用
ConcreteDecorators:具体的装饰者实现类
Examples
咖啡 和 调料 的装饰关系
Beverage.java
package com.ycit.head.first.decorator;/** * 星巴兹咖啡抽象类,添加不同的调料价格不同 * Created by xlch on 2017/5/10. */public abstract class Beverage { public String description = "unknown Beverage"; public String getDescription() { return description; } public abstract double cost();}
Espresso.java
package com.ycit.head.first.decorator.impl;import com.ycit.head.first.decorator.Beverage;/** * 咖啡实现类:浓缩咖啡 * Created by xlch on 2017/5/10. */public class Espresso extends Beverage { public Espresso() { description = "Espresso"; } @Override public double cost() { return 1.99; }}
CondimentDecorator.java
package com.ycit.head.first.decorator;/** * 调料装饰器,继承 Beverage * Created by xlch on 2017/5/10. */public abstract class CondimentDecorator extends Beverage { public abstract String getDescription();}
Mocha.java
package com.ycit.head.first.decorator.impl;import com.ycit.head.first.decorator.Beverage;import com.ycit.head.first.decorator.CondimentDecorator;/** * mocha 调料 * Created by xlch on 2017/5/10. */public class Mocha extends CondimentDecorator { Beverage beverage; public Mocha(Beverage beverage) { this.beverage = beverage; } @Override public String getDescription() { return beverage.getDescription() + ",Mocha"; } @Override public double cost() { return beverage.cost() + .20; }}
Soy.java
package com.ycit.head.first.decorator.impl;import com.ycit.head.first.decorator.Beverage;import com.ycit.head.first.decorator.CondimentDecorator;/** * soy 调料 * Created by xlch on 2017/5/10. */public class Soy extends CondimentDecorator { Beverage beverage; public Soy (Beverage beverage) { this.beverage = beverage; } @Override public String getDescription() { return beverage.getDescription() + ", Soy"; } @Override public double cost() { return beverage.cost() + .20; }}
还可以有很多其他调料;
测试类:
package com.ycit.head.first.decorator.impl;/** * Created by xlch on 2017/5/10. */public class Main { public static void main(String [] args) { /**我需要一杯mocha 浓缩咖啡**/ Espresso espresso = new Espresso(); Mocha mocha = new Mocha(espresso); System.out.println(mocha.getDescription()); System.out.println(mocha.cost()); /**我需要一杯mocha + soy 浓缩咖啡**/ Soy soy = new Soy(mocha); System.out.println(soy.getDescription()); System.out.println(soy.cost()); }}
Case
Jdk 中的 输入输出流的设计利用了 装饰器模式;
其中的 FilterInputStream 相当于该处的 Decorator,
具体的实现类包括:
- BufferedInputStream
- DataInputStream
- PushbackInputStream
0 0
- 【design pattern】结构型模式之—装饰者模式(Decorator)
- 设计模式(结构型)之装饰者模式(Decorator Pattern)
- 装饰设计模式(decorator design pattern)
- 结构型模式之装饰模式(Decorator Pattern)C++实现
- 结构型模式: 装饰者模式(Decorator Pattern)
- 结构型模式3:装饰模式(Decorator Pattern)
- 结构型模式之装饰者模式(Decorator)
- Design Pattern 设计模式 Decorator 装饰者模式
- 结构型模式之装饰(Decorator)
- 设计模式2--装饰模式(the decorator design pattern)
- "围观"设计模式(13)--结构型之装饰模式(Decorator Pattern)
- 设计模式笔记之装饰者模式(Decorator Pattern)
- C#设计模式之装饰者模式(Decorator Pattern)
- 设计模式总结之Decorator Pattern(装饰者模式)
- Java之装饰模式(Decorator Pattern)
- Java之装饰模式(Decorator Pattern)
- Java之装饰模式(Decorator Pattern)
- 23种设计模式(8)_结构型_装饰者模式(Decorator Pattern)
- 拖动改变Div大小
- bzoj1827[Usaco2010 Mar]gather 奶牛大集会 树上DP
- 程序员技术怀旧_晒晒那些过去的经典
- css元素水平垂直居中的十种方法
- OpenWrt:路由器上的Linux
- 【design pattern】结构型模式之—装饰者模式(Decorator)
- JZOJ 2113——括号问题
- 20170516_C++ 虚函数表解析
- 理解volatile
- 让 ESP32 连接到你的 WiFi 热点
- linux环境下安装python的cx_Oracle包
- c-选择
- NUI的DataGrid中想进行列的数据统计该如何实现?
- Python——使用matplotlib绘制柱状图