gpt4 book ai didi

c++ - 如何使用C++中的装饰器模式在一行代码中装饰对象?

转载 作者:太空宇宙 更新时间:2023-11-04 16:23:42 25 4
gpt4 key购买 nike

我正在从 Head First Design Patterns book 学习装饰器模式 ,下面是我编写的 (C++) 代码来让模式工作:

#include <iostream>


class AbstractType
{
public:
virtual double value() const = 0;
};


class FirstConcreteType
:
public AbstractType
{
public:
double value() const
{
return 1;
}
};

class SecondConcreteType
:
public AbstractType
{
public:
double value() const
{
return 2;
}
};

class DecoratorType
:
public AbstractType
{
const AbstractType* decoratedObject_;

public:

DecoratorType(const AbstractType& abstractObject)
:
decoratedObject_(&abstractObject)
{}

DecoratorType(const DecoratorType& decoratorObject)
:
decoratedObject_(&decoratorObject)
{}

virtual double value() const = 0;

const AbstractType& getObject() const
{
return *decoratedObject_;
}
};

class FirstDecoratorType
:
public DecoratorType
{
public:
FirstDecoratorType(const AbstractType& abstractObject)
:
DecoratorType(abstractObject)
{}

FirstDecoratorType(const DecoratorType& decoratorObject)
:
DecoratorType(decoratorObject)
{}

double value() const
{
const AbstractType& object = getObject();

return 1 + object.value();
}
};

class SecondDecoratorType
:
public DecoratorType
{
public:
SecondDecoratorType(const AbstractType& abstractObject)
:
DecoratorType(abstractObject)
{}

SecondDecoratorType(const DecoratorType& decoratorObject)
:
DecoratorType(decoratorObject)
{}

double value() const
{
const AbstractType& object = getObject();

return 2 + object.value();
}
};

using namespace std;

int main()
{
// When I decorate sequentially, it works fine

SecondConcreteType secondConc;

FirstDecoratorType firstDec(secondConc);
cout << firstDec.value() << endl;

SecondDecoratorType secondDec(firstDec);
cout << secondDec.value() << endl;

FirstDecoratorType firstDecSecond (secondDec);
cout << firstDecSecond.value() << endl;

// Decorating in a single line, messes things up, since there is no
// constructor taking the value argument defined.
//FirstDecoratorType firstDynamicDec (SecondConcreteType());
//cout << firstDynamicDec.value() << endl;

return 0;
};

在主程序中,必须首先创建 ConcreteType 的对象,然后使用指向 AbstractType 的指针(在 DecoratorType 内)的组合对其进行装饰。如果我创建具体对象,并一个接一个地创建新的装饰对象,它工作正常。

为了使 DecoratorType 能够在单行代码(示例代码中的注释行)中使用组合来装饰对象,我需要做什么?这样的东西在“现实世界”中有用吗?我(显然)在使用设计模式方面没有太多经验.. 所以我很难看出我应该瞄准什么功能。

编辑:

这是一个使用基本指针的版本(valgrind 显示没有内存泄漏,并声明没有内存泄漏是可能的):

#include <iostream>


class AbstractType
{
public:
virtual double value() const = 0;

virtual ~AbstractType() {};
};

class FirstConcreteType
:
public AbstractType
{
public:
double value() const
{
return 1;
}
};

class SecondConcreteType
:
public AbstractType
{
public:
double value() const
{
return 2;
}
};

class DecoratorType
:
public AbstractType
{
const AbstractType* decoratedObject_;
bool own_;

public:

DecoratorType(const AbstractType& abstractObject)
:
decoratedObject_(&abstractObject),
own_(false)
{}

DecoratorType(const DecoratorType& decoratorObject)
:
decoratedObject_(&decoratorObject),
own_(false)

{}

DecoratorType (AbstractType* abstractPtr)
:
decoratedObject_(abstractPtr),
own_(true)
{}

DecoratorType (DecoratorType* decoratorPtr)
:
decoratedObject_(decoratorPtr),
own_(true)
{}

virtual ~DecoratorType()
{
if (own_)
{
delete decoratedObject_;
decoratedObject_ = 0;
}
}

virtual double value() const = 0;

const AbstractType& getObject() const
{
return *decoratedObject_;
}
};

class FirstDecoratorType
:
public DecoratorType
{
public:
FirstDecoratorType(const AbstractType& abstractObject)
:
DecoratorType(abstractObject)
{}

FirstDecoratorType(const DecoratorType& decoratorObject)
:
DecoratorType(decoratorObject)
{}

FirstDecoratorType (AbstractType* abstractPtr)
:
DecoratorType(abstractPtr)
{}

FirstDecoratorType (FirstDecoratorType* decoratorPtr)
:
DecoratorType(decoratorPtr)
{}


double value() const
{
const AbstractType& object = getObject();

return 1 + object.value();
}
};

class SecondDecoratorType
:
public DecoratorType
{
public:
SecondDecoratorType(const AbstractType& abstractObject)
:
DecoratorType(abstractObject)
{}

SecondDecoratorType(const DecoratorType& decoratorObject)
:
DecoratorType(decoratorObject)
{}

SecondDecoratorType (AbstractType* abstractPtr)
:
DecoratorType(abstractPtr)
{}

SecondDecoratorType (SecondDecoratorType* decoratorPtr)
:
DecoratorType(decoratorPtr)
{}

double value() const
{
const AbstractType& object = getObject();

return 2 + object.value();
}
};

using namespace std;

int main()
{
// When I decorate sequentially, it works fine

SecondConcreteType secondConc;

FirstDecoratorType firstDec(secondConc);
cout << firstDec.value() << endl;

SecondDecoratorType secondDec(firstDec);
cout << secondDec.value() << endl;

FirstDecoratorType firstDecSecond (secondDec);
cout << firstDecSecond.value() << endl;

// Decorating in a single line, messes things up, since there is no
// constructor taking the value argument defined.
FirstDecoratorType firstDynamicDec (new SecondDecoratorType (
new FirstDecoratorType (new SecondConcreteType())));

cout << firstDynamicDec.value() << endl;

return 0;
};

最佳答案

FirstDecoratorType firstDynamicDec (SecondConcreteType()); 

问题在于它没有定义对象。相反,它声明一个函数。在此站点上查找 C++ 中的 most-vexing-parse,您将获得很多关于它的主题。

简短说明:函数名称是 firstDynamicDec,其返回类型是 FirstDecoratorType,它采用的参数也是返回 SecondConcreteType 并采用没有争论。

关于c++ - 如何使用C++中的装饰器模式在一行代码中装饰对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14173471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com