gpt4 book ai didi

c++ - 装饰器模式 : Detaching responsibilities

转载 作者:搜寻专家 更新时间:2023-10-31 02:13:05 26 4
gpt4 key购买 nike

对于Decorator设计模式,GoF明确指出:

With decorators, responsibilities can be added and removed at run-time simply by attaching and detaching them.

我正在尝试用 C++ 构建一个框架代码,在其中我可以很容易地看到这种模式如何向未修饰的对象添加职责。

我的问题是如何免除特定责任。删除最后一个包装的责任可以很容易地完成。但是我也可以删除中间添加的责任吗?

这是我的示例代码:

class Icecream { //Component
public :
virtual void addToppings() = 0 ;
virtual Icecream *getUndecorated() = 0 ;
} ;
class PlainIcecream : public Icecream { //Concrete Component
public :
void addToppings() { cout<<"I am just plain icecream, you may add more flavors\n" ; }
Icecream * getUndecorated() {return this ; }
} ;
class Topping : public Icecream { //Decorator
protected :
Icecream *icecream ;
public :
Topping(Icecream *i) : icecream(i) {}
void addToppings() { icecream->addToppings() ; }
} ;
class PeanutToppings : public Topping { //Concrete Component A
public :
PeanutToppings(Icecream *i) : Topping(i) {}
void addToppings() {
Topping::addToppings() ;
cout<<"Adding peanut toppings for great taste\n" ;
}
Icecream * getUndecorated() {return icecream ; }
} ;
class CandyToppings : public Topping { //Concrete Component A
public :
CandyToppings(Icecream *i) : Topping(i) {}
void addToppings() {
Topping::addToppings() ;
cout<<"Adding candy toppings for yummy flavour\n" ;
}
Icecream * getUndecorated() {return icecream ; }
} ;
main() {
Icecream *icecream_with_candy_and_peanuts = new CandyToppings(new PeanutToppings(new PlainIcecream)) ;
icecream_with_candy_and_peanuts->addToppings() ;
cout<<"_________________________________________________\n" ;
Icecream *icecream_with_only_peanuts = icecream_with_candy_and_peanuts->getUndecorated() ;
icecream_with_only_peanuts->addToppings() ;
cout<<"_________________________________________________\n" ;
Icecream *icecream_plain = icecream_with_only_peanuts->getUndecorated() ;
icecream_plain->addToppings() ;
}

现在我想知道是否有可能从 icecream_with_candy_and_peanuts 中得到一个只有糖果顶部的冰淇淋。请不要考虑我为什么要这样做。我只是想了解这个词

detaching responsibilities

如 GoF 中所述。

最佳答案

Removing the last wrapped responsibility can be done easily

不需要单独的 getUndecorated 方法。这不是标准 Decorator 模式定义的一部分。

Now I want to know if it is possible to have an icecream with only candy topping from icecream_with_candy_and_peanuts

是的,你可以。就装饰器模式而言,附加分离 职责之间没有太大区别。要分离一个责任,你只需要再次重新创建你的组件:

//attaching responsibilities
Icecream *icecream = new new PlainIcecream();
*icecream = new PeanutToppings(icecream);//wrap around with new respnsibility
*icecream = new CandyToppings(icecream);//wrap around with new responsiblity

//detaching responsibilities
*icecream = new CandyToppings(new PlainIcecream());

请注意我在这两种情况下是如何使用 *icecream 的。您可以通过限制在单个界面(即 IceCream)来附加分离职责。这就是 Decorator 模式背后的理念。

也就是说,在您希望能够任意添加或删除现有实例的职责。

额外提示:Decorator 模式中的装饰器理想情况下不应是可实例化的(Toppings 在您的情况下)。将 Toppings 重命名为 ToppingsDecorator 也会有所帮助。 (我知道这不是你要问的直接问题。这纯粹是为了其他读者的利益)

关于c++ - 装饰器模式 : Detaching responsibilities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41873464/

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