`

Decorator pattern and Proxy pattern

 
阅读更多

 

You want to add behavior or state to individual objects at run-time. Inheritance is not feasible because it is static and applies to an entire class.


Typical Example as follows, with the typical traits of attaching additional responsibilities to an object dynamically.
Widget* aWidget = new BorderDecorator(
     new ScrollDecorator( 
                 new TextWidget( 80, 24 )
            )
        )
    );
aWidget->draw();
 

The Famous Remark:
Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface. [GOF, p216]

Decorator is designed to let you add responsibilities to objects without subclassing. Composite's focus is not on embellishment but on representation. These intents are distinct but complementary. Consequently, Composite and Decorator are often used in concert. [GOF, p220]

Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests. [GOF, p220]

Decorator lets you change the skin of an object. Strategy lets you change the guts. [GOF, p184]

The code for the calling class above, the routines should like that:

// Decorator.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class Widget{
public:
    virtual void draw()= 0;
};

class TextWidget:public Widget{
public:
    TextWidget(int w, int h){
    _w = w; _h = h;
    }
    void draw(){
        cout<<"TextWidget..."<<endl;
       //...Some real draw action....
    };
protected:
    Widget* _widget;
    int _w, _h; 
};

class Decorator: public Widget{
public:
    Decorator(Widget* w){ 
        _widget = w;
    }
    void draw(){
       cout<<"Decorator..."<<endl;
       _widget->draw();
    }
protected:
    Widget* _widget;
};

class ScrollDecorator: public Decorator{
public:
    ScrollDecorator(Widget* w):Decorator(w){};
    void draw(){
       cout<<"ScorllDecorator..."<<endl;
       _widget->draw();
    }
};

class BorderDecorator: public Decorator{
public:
    BorderDecorator(Widget* w):Decorator(w){};
     void draw(){
        cout<<"BorderDecorator.."<<endl;
       _widget->draw();
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Widget* aWidget = new BorderDecorator(
           new ScrollDecorator(
                 new TextWidget( 80, 24 )
            )
    );
    aWidget->draw(); 

    return 0;
}

There is some mistakes in the routines above.
_
widget->draw() just get Widget instance to do draw(), but it has not call the Decorator, so it should be Decorator::draw() to do the real draw().
分享到:
评论

相关推荐

    C#版 24种设计模式

    备忘录模式(Memento Pattern) 策略模式(Strategy Pattern) 抽象工厂模式(Abstract Factory Pattern) 代理模式(Proxy Pattern) 单例模式(Singleton Pattern) 迭代器模式(Iterator Pattern) 访问者模式(Visitor ...

    Java24种设计模式,Java24种设计模式,24种设计模式,学会了这24种设计模式,可以打遍天下无敌手,设计模式非常重要

    2、代理模式PROXY PATTERN 3、单例模式SINGLETON PATTERN 4、多例模式MULTITION PATTERN 5、工厂方法模式FACTORY METHOD PATTERN 6、抽象工厂模式ABSTRACT FACTORY PATTERN 7、门面模式FACADE PATTERN 8、适配器...

    用Java实现23种设计模式

    装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式(Proxy Pattern) 3. 行为型模式 责任链模式(Chain of Responsibility Pattern) 命令模式(Command ...

    C#设计模式_设计模式_C#_

    装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 行为型: 13. 模板方法(Template Method) 14. 命令...

    23种设计模式 (创建型,结构型,行为型)

    装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令...

    设计模式PPT

     装饰者模式(Decorator Pattern)  外观模式(Facade Pattern)  享元模式(Flyweight Pattern)  代理模式(Proxy Pattern) 行为型模式用来对类或对象怎样交互和怎样分配职责进行描述,主要包含以下11种...

    设计模式代码——c#

    8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 行为型 13. 模板方法(Template Method) ...

    DesignPattern.zip

    在`DecoratorPattern`目录中,可以看到装饰者和被装饰对象的接口及具体实现。 5. **策略模式(Strategy)**:定义一系列算法,并将每一个算法封装起来,使它们可以相互替换。策略模式让算法的变化独立于使用算法的...

    C#设计模式-吕震宇

    C#设计模式(12)-Decorator Pattern C#设计模式(11)-Composite Pattern C#设计模式(10)-Adapter Pattern C#设计模式(9)-Prototype Pattern C#设计模式(8)-Builder Pattern C#设计模式(7)-Singleton...

    Design*Pattern*Framework*4.5

    结构型模式关注如何组合对象和类以形成更大的结构,例如适配器(Adapter)、装饰器(Decorator)和代理(Proxy);行为型模式涉及到对象间如何交互,如策略(Strategy)、观察者(Observer)和责任链(Chain of ...

    Software Architecture Design Pattern In Java

    3. **装饰器模式(Decorator Pattern)**: - 定义:动态地给一个对象添加一些额外的职责。 - 应用场景:可以用来扩展功能而不改变原有类的结构,如网络通信中的数据加密解密。 4. **观察者模式(Observer ...

    33种JAVA设计模式DEMO

    装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式(Proxy Pattern) 3 行为型模式 这些设计模式特别关注对象之间的通信。 责任链模式(Chain of ...

    JAVA设计模式.rar

    代理模式【PROXY PATTERN】 单例模式【SINGLETON PATTERN】  多例模式【MULTITION PATTERN】  工厂方法模式【FACTORY METHOD PATTERN】 抽象工厂模式【ABSTRACT FACTORY PATTERN】 门面模式【FACADE ...

    design-pattern-java.pdf

    树形结构的处理——组合模式(二) 树形结构的处理——组合模式(三) 树形结构的处理——组合模式(四) 树形结构的处理——组合模式(五) 装饰模式-Decorator Pattern 扩展系统功能——装饰模式(一) 扩展系统...

    Object-Oriented Analysis and Design 第六章

    - Proxy pattern provides a surrogate or placeholder for another object to control access, provide additional functionality, or optimize performance. Object Oriented Analysis and Design 14Behavioral ...

    DesignPattern.rar

    8. **装饰器模式(Decorator Pattern)**:装饰器模式可以在运行时动态地给对象添加新的行为或责任,同时保持类的封装性。 9. **代理模式(Proxy Pattern)**:代理模式为其他对象提供一种代理以控制对这个对象的访问。...

    32种设计模式

    装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template ...

Global site tag (gtag.js) - Google Analytics