设计模式之装饰(Decorator)模式
来源:互联网 发布:淘宝店铺账小号 编辑:程序博客网 时间:2024/05/16 13:07
别名
包装(Wrapper)模式
定义
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
在对象上动态地附加责任,装饰者为子类扩展功能提供了一个灵活的选择。
结构
组成
组件
- 定义一个能让对象动态地附加责任的接口
具体组件
- 定义一个可以附加额外责任的对象
装饰者
- 保持对组件对象的引用
- 定义与组件相符的接口
具体装饰者
- 在组件上添加责任
应用
JDK中的IO流,输入流、输出流、读者、写者……
代码
组件
仓库
https://github.com/luoxin1992/DesignPatterns/tree/master/src/prj/designpatterns/decorator
组件
package prj.designpatterns.decorator.prototype;/** * 组件 * * @author LuoXin * */public abstract class Component {public abstract void operate();}具体组件
package prj.designpatterns.decorator.prototype;/** * 具体组件 * * @author LuoXin * */public class ConcreteComponent extends Component {@Overridepublic void operate() {System.out.println("我是未装饰的组件");}}装饰者
package prj.designpatterns.decorator.prototype;/** * 装饰者 * * @author LuoXin * */public abstract class Decorator extends Component {private Component component;public Decorator(Component component) {this.component = component;}public void operate() {this.component.operate();}}具体装饰者
package prj.designpatterns.decorator.prototype;/** * 具体装饰者 * * @author LuoXin * */public class ConcreteDecoratorA extends Decorator {public ConcreteDecoratorA(Component component) {super(component);}public void decorate() {System.out.println("我是装饰A");}@Overridepublic void operate() {this.decorate();super.operate();}}客户端
package prj.designpatterns.decorator.prototype;/** * 客户端 * * @author LuoXin * */public class Client {public static void main(String[] args) {ConcreteComponent component = new ConcreteComponent();ConcreteDecoratorA decoratorA = new ConcreteDecoratorA(component);ConcreteDecoratorB decoratorB = new ConcreteDecoratorB(decoratorA);decoratorB.operate();}}
仓库
https://github.com/luoxin1992/DesignPatterns/tree/master/src/prj/designpatterns/decorator
0 0
- 【设计模式】之装饰模式(Decorator)
- 设计模式之装饰模式(Decorator)
- 设计模式之 装饰模式(Decorator)
- 设计模式之装饰模式(Decorator)
- 设计模式之装饰(Decorator)模式
- 设计模式之装饰模式(Decorator)
- 设计模式之装饰模式(Decorator)
- 设计模式之---装饰(decorator)
- 设计模式之Decorator(装饰者)
- 设计模式之decorator装饰模式
- 设计模式之-Decorator--装饰器模式
- 设计模式之Decorator(装饰者模式)
- 设计模式之(三、装饰模式Decorator)
- 设计模式之(九)装饰模式Decorator
- 设计模式之Decorator(装饰模式)
- 设计模式之装饰模式(Decorator Pattern)
- 【设计模式】之 Decorator 装饰模式
- 设计模式之八 --- 装饰模式(Decorator)
- 操作系统的文件系统基本调用命令
- tango with django ch5 template&static file
- Illegal resource reference: @*android resources are private and not always present 解决方案
- 导航框架
- 如何建立一个快速显示桌面的快捷方式?
- 设计模式之装饰(Decorator)模式
- Dubbo高级篇_07_Dubbo服务接口的设计原则
- EhCache简介
- as-build.gradle
- Spring MultipartResolver 和ServletFileUpload 冲突导致获取不到上传文件的url
- 图文详解 IntelliJ IDEA 15 创建 Maven 构建的 Java Web 项目(使用 Jetty 容器)
- Android 生成keystore,两种方式
- iOS 明明导入了图片,可Xcode却说找不到图片
- UVA 10200 Prime Time【暴力,精度】