我正在尝试使用复合设计模式来应用装饰器。我遇到的大多数资源都表明这两种模式经常以这种方式协同工作,但没有一个例子说明这是如何工作的。可能有点牵强,但我可以举一个例子来推断这种情况下的实现吗?
我的课程大纲如下。
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
};