我正在尝试使用复合设计模式来应用装饰器。我遇到的大多数资源表明这两种模式通常以这种方式协同工作,但没有一种显示其工作原理的示例。可能是一个延伸,但在这种情况下,我可以通过一个例子来推断实现吗?
我的课程概述如下。
ThorSoldier继承自AvengerSoldier,AvengerSoldier继承自Unit。每个“复仇者”都遵循类似的结构。
class Unit // Component (Not abstract, contains all the implementations that units have)
{
public:
virtual void levelUp(){}
// Other implementations
};
class NickFurySoldier // Manager (Composite)
{
public:
virtual void levelUp();// Should apply the decorators to primatives
void add(Unit* avenger);
private:
std::vector<Unit*> avengers;
};
class ThorSoldier: public AvengerSoldier
{
// Example of a primative that has to be decorated
};
class ThorDecorator: public ThorSoldier
{
public:
ThorDecorator(ThorSoldier* soldier);
// Other Implementations
};
class LightningThor: public ThorDecorator // Example of the decoration that should be implemented
{
public:
LightningThor(ThorSoldier* soldier);
// Other implementations
};