结构型模式:Decorator(装饰)
来源:互联网 发布:网络歌曲我想静静 编辑:程序博客网 时间:2024/05/15 17:14
http://zh.wikipedia.org/wiki/%E4%BF%AE%E9%A5%B0%E6%A8%A1%E5%BC%8F
通过使用修饰模式,可以在运行时扩充一个类的功能。原理是:增加一个修饰类包裹原来的类,包裹的方式一般是通过在将原来的对象作为修饰类的构造函数的参数。装饰类实现新的功能,但是,在不需要用到新功能的地方,它可以直接调用原来的类中的方法。修饰类必须和原来的类有相同的接口。
修饰模式是类继承的另外一种选择。类继承在编译时候增加行为,而装饰模式是在运行时增加行为。
当有几个相互独立的功能需要扩充时,这个区别就变得很重要。在有些面向对象的编程语言中,类不能在运行时被创建,通常在设计的时候也不能预测到有哪几种功能组合。这就意味著要为每一种组合创建一个新类。相反,修饰模式是面向运行时候的对象实例的,这样就可以在运行时根据需要进行组合。一个修饰模式的示例是JAVA里的Java I/O Streams的实现。
// the Window interfaceinterface Window { public void draw(); // draws the Window public String getDescription(); // returns a description of the Window} // implementation of a simple Window without any scrollbarsclass SimpleWindow implements Window { public void draw() { // draw window } public String getDescription() { return "simple window"; }}
// abstract decorator class - note that it implements Windowabstract class WindowDecorator implements Window { protected Window decoratedWindow; // the Window being decorated public WindowDecorator (Window decoratedWindow) { this.decoratedWindow = decoratedWindow; }} // the first concrete decorator which adds vertical scrollbar functionalityclass VerticalScrollBarDecorator extends WindowDecorator { public VerticalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); } public void draw() { drawVerticalScrollBar(); decoratedWindow.draw(); } private void drawVerticalScrollBar() { // draw the vertical scrollbar } public String getDescription() { return decoratedWindow.getDescription() + ", including vertical scrollbars"; }} // the second concrete decorator which adds horizontal scrollbar functionalityclass HorizontalScrollBarDecorator extends WindowDecorator { public HorizontalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); } public void draw() { drawHorizontalScrollBar(); decoratedWindow.draw(); } private void drawHorizontalScrollBar() { // draw the horizontal scrollbar } public String getDescription() { return decoratedWindow.getDescription() + ", including horizontal scrollbars"; }}
public class DecoratedWindowTest { public static void main(String[] args) { // create a decorated Window with horizontal and vertical scrollbars Window decoratedWindow = new HorizontalScrollBarDecorator ( new VerticalScrollBarDecorator(new SimpleWindow())); // print the Window's description System.out.println(decoratedWindow.getDescription()); }}
- 结构型模式-----装饰模式(decorator)
- Decorator模式(装饰模式 结构型)
- Decorator 装饰(结构型模式)
- 结构型模式:Decorator(装饰)
- 装饰器模式Decorator(结构型)
- Decorator装饰设计模式(结构型)
- 结构型模式之装饰(Decorator)
- 装饰模式Decorator(结构型)
- Decorator装饰(结构型模式)
- 【结构型模式】decorator(装饰)
- 结构型模式-装饰(decorator)
- 设计模式(3)-结构型-装饰模式(Decorator)
- 装饰模式decorator(结构型设计模式)
- 结构型模式之装饰者模式(Decorator)
- 设计模式--装饰器模式Decorator(结构型)
- 结构型模式3:装饰模式(Decorator Pattern)
- 结构型模式之装饰模式(Decorator)
- 结构型模式之装饰模式(Decorator)
- Java WEB开发中路径问题的总结
- torrent文件分析
- shell中nice函数
- 基于Angle游戏引擎相关的文档说明
- 第一次在csdn上开篇 因为我还是菜鸟
- 结构型模式:Decorator(装饰)
- 二级指针的两种用法
- SIPp安装配置
- cocos2d-x for android:ubuntu 环境搭建
- firefox os架构
- sysctl命令详解
- JSP内置对象之response对象
- 数论经典题目
- cocos2d-x for android:HelloWorld 解读