设计模式(9)——Decorator(装饰)模式
来源:互联网 发布:农村淘宝快递加盟 编辑:程序博客网 时间:2024/04/28 23:46
什么是装饰模式?
装饰模式是动态地(在运行时)给一个对象添加一些额外的职责。
装饰模式的适用范围
1.在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责;
2. 处理那些可以撤销的职责
3. 当不能采用生成子类的方式进行扩充时。
装饰模式的示例
/** * Window Interface * * Component window */public interface Window {public void renderWindow();}
/** * Window implementation * * Concrete implementation */public class SimpleWindow implements Window {@Overridepublic void renderWindow() {// implementation of rendering details}}
/** * */public class DecoratedWindow implements Window{/** * private reference to the window being decorated */private Window privateWindowRefernce = null;public DecoratedWindow( Window windowRefernce) {this.privateWindowRefernce = windowRefernce;}@Overridepublic void renderWindow() {privateWindowRefernce.renderWindow();}}
/** * Concrete Decorator with extended state * * Scrollable window creates a window that is scrollable * * */public class ScrollableWindow extends DecoratedWindow{/** * Additional State */private Object scrollBarObjectRepresentation = null;public ScrollableWindow(Window windowRefernce) {super(windowRefernce);}@Overridepublic void renderWindow() {// render scroll bar renderScrollBarObject();// render decorated windowsuper.renderWindow();}private void renderScrollBarObject() {// prepare scroll bar scrollBarObjectRepresentation = new Object();// render scrollbar }}
0 0
- 设计模式(9)——Decorator(装饰)模式
- 设计模式(9)——装饰 Decorator
- 设计模式c#描述——装饰(Decorator)模式
- 设计模式c#描述——装饰(Decorator)模式
- 设计模式——装饰模式(Decorator)
- 设计模式之——装饰者模式(Decorator)
- 设计模式——39、装饰模式(Decorator)
- java设计模式——装饰模式(Decorator)
- 设计模式——装饰者模式(Decorator)
- java设计模式7——装饰模式(Decorator)
- 设计模式——装饰模式(Decorator)
- 39、设计模式——装饰模式(Decorator)
- 【设计模式】——装饰模式(Decorator)
- 设计模式 —— 装饰器模式(Decorator Pattern)
- 设计模式——装饰模式(Decorator)
- Java设计模式——装饰模式(Decorator Pattern)
- 设计模式——装饰模式(Decorator)
- Python设计模式——装饰模式(Decorator)
- 河南省第八届程序设计大赛有感
- arcgis api for js中使用d3.js标注点的属性值代替textsymbol方式
- 从终端读取一行数据,fgets,gets,c++ string类定义的getline
- c++枚举类型
- (10-1)redis 安装、配置
- 设计模式(9)——Decorator(装饰)模式
- asp .net mvc ajax 传值到后台,并放回JSon值解析
- 一个例子说明C语言和Java的区别
- Jmeter启用英文界面
- 4.linux复习---exec函数族
- (10-2)redis 基本命令
- 《赌玉》--------人生就是一场赌博,眼看准,下手狠
- 如何给UIViewController瘦身
- 杭电2031(进制转换)