装饰模式

本文介绍了一种软件设计模式——装饰者模式,通过实例演示了如何使用该模式来动态地为对象添加新的职责,而不破坏原有的类结构。装饰者模式允许在运行时决定是否要为对象增加额外的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

结构

当客户端只在意CoreFunctionality.doThis(),对OptionalOne.doThis()OptionalTwo.doThis()并不关心。这时这些类会被Decorator基类所委托,而Decorator基类会以包含wrappee对象的方式进行委托。

image

具体操作

  1. 通常需要确保有一个核心控件,多个可选的装饰类以及一个接口
  2. 创建一个通用的接口
  3. 创建第二级基类(Decorator)用于支持可选的装饰类
  4. 核心类和Decorator类都从接口继承
  5. Decorator类与接口形成一种组合关系,而这个数据成员会在构造函数中被初始化
  6. 为每个装饰定义Decorator的子类
  7. Decorator子类实现了装饰的具体功能
  8. 客户端配置Core和Decorator对象的类型和顺序

案例

1.
#include <iostream>
using namespace std;

// 1. "lowest common denominator"
class Widget
{
  public:
    virtual void draw() = 0;
};

class TextField: public Widget
{
    // 3. "Core" class & "is a"
    int width, height;
  public:
    TextField(int w, int h)
    {
        width = w;
        height = h;
    }

    /*virtual*/
    void draw()
    {
        cout << "TextField: " << width << ", " << height << '\n';
    }
};

// 2. 2nd level base class
class Decorator: public Widget  // 4. "is a" relationship
{
    Widget *wid; // 4. "has a" relationship
  public:
    Decorator(Widget *w)
    {
        wid = w;
    }

    /*virtual*/
    void draw() 
    {
        wid->draw(); // 5. Delegation
    }
};

class BorderDecorator: public Decorator
{
  public:
    // 6. Optional embellishment
    BorderDecorator(Widget *w): Decorator(w){}

    /*virtual*/
    void draw()
    {
        // 7. Delegate to base class and add extra stuff
        Decorator::draw();
        cout << "   BorderDecorator" << '\n';
    }
};

class ScrollDecorator: public Decorator
{
  public:
    // 6. Optional embellishment
    ScrollDecorator(Widget *w): Decorator(w){}

    /*virtual*/
    void draw()
    {
        // 7. Delegate to base class and add extra stuff
        Decorator::draw();
        cout << "   ScrollDecorator" << '\n';
    }
};

int main()
{
  // 8. Client has the responsibility to compose desired configurations
  Widget *aWidget = new BorderDecorator(new BorderDecorator(new ScrollDecorator
    (new TextField(80, 24))));
  aWidget->draw();
}
Output
TextField: 80, 24
   ScrollDecorator
   BorderDecorator
   BorderDecorator
2.
#include <iostream>
#include <string>
using namespace std;

class Interface
{
  public:
    virtual ~Interface(){}
    virtual void write(string &) = 0;
    virtual void read(string &) = 0;
};

class Core: public Interface
{
  public:
    ~Core()
    {
        cout << "dtor-Core\n";
    }
     /*virtual*/void write(string &b)
    {
        b += "MESSAGE|";
    }
     /*virtual*/void read(string &);
};

class Decorator: public Interface
{
    Interface *inner;
  public:
    Decorator(Interface *c)
    {
        inner = c;
    }
    ~Decorator()
    {
        delete inner;
    }
     /*virtual*/void write(string &b)
    {
        inner->write(b);
    }
     /*virtual*/void read(string &b)
    {
        inner->read(b);
    }
};

class Wrapper: public Decorator
{
    string forward, backward;
  public:
    Wrapper(Interface *c, string str): Decorator(c)
    {
        forward = str;
        string::reverse_iterator it;
        it = str.rbegin();
        for (; it != str.rend(); ++it)
          backward +=  *it;
    }
    ~Wrapper()
    {
        cout << "dtor-" << forward << "  ";
    }
    void write(string &);
    void read(string &);
};

int main()
{
  Interface *object = new Wrapper(new Wrapper(new Wrapper(new Core(), "123"), 
    "abc"), "987");
  string buf;
  object->write(buf);
  cout << "main: " << buf << endl;
  object->read(buf);
  delete object;
}
Output
main: 987]abc]123]MESSAGE|321]cba]789]
Wrapper: 987
Wrapper: abc
Wrapper: 123
Core: MESSAGE
Wrapper: 321
Wrapper: cba
Wrapper: 789
dtor-987  dtor-abc  dtor-123  dtor-Core
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值