For Decorator design pattern, GoF clearly states that :
With decorators, responsibilities can be added and removed at run-time simply by attaching and detaching them.
I am trying to build a skeleton code in C++ where I can easily see how this pattern can add responsibilities to an undecorated object.
My question is how can I remove a particular responsibility. Removing the last wrapped responsibility can be done easily. But can I also remove a responsility added in between.
Here is my sample code :
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() ;
}
Now I want to know if it is possible to have an icecream with only candy topping from icecream_with_candy_and_peanuts
. Please do not consider why do I want to do so. I am just trying to understand the term
detaching responsibilities
as mentioned in GoF.
Aucun commentaire:
Enregistrer un commentaire