C++实现装饰模式,为什么中间环节被“跳过”了?
#include<iostream>
using namespace std;

class Component
{
public:
    virtual void operation() 
    {
        cout << "Component\n";
    }
};

class Decorator: public Component
{
protected:
    Component component;
public:
    void setComponent(Component component)
    {
        this->component = component;
    }
    virtual void operation()
    {
        component.operation();
    }
};

class ConcreteDecoratorA: public Decorator
{
public:
    virtual void operation()
    {
        Decorator::operation();
        cout << "ConcreteDecoratorA\n";
    }
};

class ConcreteDecoratorB : public Decorator
{
public:
    virtual void operation()
    {
        Decorator::operation();
        cout << "ConcreteDecoratorB\n";
    }
};

int main()
{
    Component c;
    ConcreteDecoratorA a;
    ConcreteDecoratorB b;
    a.setComponent(c);
    b.setComponent(a);
    b.operation();
    return 0;
}

输出:
Component
ConcreteDecoratorB

为什么不会输出“ConcreteDecoratorA”呢?

c++

3个回答

b.setComponent(a);的时候的方法是类Decorator里的
参数是Component,而a的类型是ConcreteDecoratorA,被退化为Decorator,再退化为Component
所以自始至终都没有使用过ConcreteDecoratorA:类里的任何东西,自然不会输出ConcreteDecoratorA

operation() 是类的成员方法,由原型对象通过内部指针访问。
源代码 b.operation(); 实际上是调用class ConcreteDecoratorB的成员方法,因此输出 "ConcreteDecoratorB"

#include<iostream>
using namespace std;

class Component
{
public:
    virtual void operation()
    {
        cout << "Component\n";
    }
};

class Decorator : public Component
{
    /* 以下是错误的代码 :
    protected:
        Component component;
    public:
        void setComponent(Component component)
        {
            this->component = component;
        }
        virtual void operation()
        {
            component.operation();
        }
    */
public:
    Decorator() : 
        component(nullptr)
    {
    }
    void setComponent(Component* component)
    {
        this->component = component;
    }
    virtual void operation()
    {
        component->operation();
    }
private:
    Component* component;
};

class ConcreteDecoratorA : public Decorator
{
public:
    virtual void operation()
    {
        Decorator::operation();
        cout << "ConcreteDecoratorA\n";
    }
};

class ConcreteDecoratorB : public Decorator
{
public:
    virtual void operation()
    {
        Decorator::operation();
        cout << "ConcreteDecoratorB\n";
    }
};

int main()
{
    Component c;
    ConcreteDecoratorA a;
    ConcreteDecoratorB b;
    /*  以下是错误的代码 :
        a.setComponent(c);
        b.setComponent(a);
    */
    a.setComponent(&c);
    b.setComponent(&a);
    b.operation();

    // 考虑下面的代码,发现错误的根源:
    Component component1 = a;
    /*  这一行代码等价于:Component component1((const Component&)a);
        调用了Component的默认构造函数:Component(const Component&)
        虽然component1用ConcreteDecoratorA的实例初始化,但component1并不是ConcreteDecoratorA的实例.
    */
    Component component2 = b;
    //  component2并不是ConcreteDecoratorB的实例.
    return 0;
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问