装饰模式的C++实现——设计模式学习(1)

编程目的:理解装饰模式及其用法。

装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。[DP]

装饰模式是一种结构型设计模式,它允许在运行时动态地给对象添加新的行为。这种模式是通过创建一个包装器来实现的,该包装器包含原始对象,并在运行时添加新的行为。在这个过程中,不需要修改原始对象的代码,因此装饰模式是一种非常灵活的模式。

下面是装饰模式的C++实现:

首先,我们需要定义一个基本的组件类,表示我们要给它添加新的行为的对象。在这个例子中,我们以一个简单的字符串类为例:

class Component {
public:
    virtual ~Component() {}
    virtual std::string operation() const = 0;
};

接下来,我们需要实现具体的组件类。在这个例子中,我们以一个简单的字符串类为例:

class ConcreteComponent : public Component {
public:
    std::string operation() const override {
        return "ConcreteComponent";
    }
};

然后,我们需要定义一个装饰器类,用于包装具体的组件并添加新的行为:

class Decorator : public Component {
public:
    Decorator(Component* component) : component_(component) {}
    std::string operation() const override {
        return component_->operation();
    }
protected:
    Component* component_;
};

最后,我们可以实现具体的装饰器类来添加新的行为:

class ConcreteDecoratorA : public Decorator {
public:
    ConcreteDecoratorA(Component* component) : Decorator(component) {}
    std::string operation() const override {
        return "ConcreteDecoratorA(" + Decorator::operation() + ")";
    }
};

class ConcreteDecoratorB : public Decorator {
public:
    ConcreteDecoratorB(Component* component) : Decorator(component) {}
    std::string operation() const override {
        return "ConcreteDecoratorB(" + Decorator::operation() + ")";
    }
};

在这个例子中,我们定义了两个具体的装饰器类,ConcreteDecoratorA和ConcreteDecoratorB。这些装饰器类都是从Decorator类继承而来的,它们的构造函数都接受一个指向组件对象的指针,并将其存储在类成员变量中。在这些具体的装饰器类中,我们重写了操作方法,以便添加新的行为。

现在,我们可以使用这些类来创建一个具有多个行为的对象:

Component* component = new ConcreteComponent();
Component* decoratorA = new ConcreteDecoratorA(component);
Component* decoratorB = new ConcreteDecoratorB(decoratorA);
std::cout << decoratorB->operation() << std::endl;

在这个例子中,我们首先创建了一个具体的组件对象。然后,我们用ConcreteDecoratorA装饰它,并将结果存储在decoratorA变量中。接下来,我们再用ConcreteDecoratorB装饰decoratorA,并将结果存储在decoratorB变量中。最后,我们输出decoratorB的操作结果,它将包含所有添加的行

为。

对于这个例子,如果我们输出decoratorB的操作结果,它将会是以下内容:

ConcreteDecoratorB(ConcreteDecoratorA(ConcreteComponent))

这是因为我们首先将ConcreteComponent装饰成ConcreteDecoratorA,然后再将结果装饰成ConcreteDecoratorB,因此最终的结果包含了所有添加的行为。

需要注意的是,在使用装饰模式时,我们应该避免创建过多的装饰器对象,否则可能会导致代码变得难以维护。此外,我们还应该仔细设计装饰器类的继承结构,以确保它们之间的关系清晰明确。

猜你喜欢

转载自blog.csdn.net/Fan0920/article/details/105774468