I am in need of some advice regarding my design.
Here's the basic jist of what i'm doing:
i have a hierarchy of objects that contain logic for the whole game.
the base class of the hierarchy is Action, which contains all data and members needed for every type of action to be able to run it's logic. it uses the template method pattern (Update() calls onUpdate(), which is pure virtual) to run some basic code before letting the derived class to take control.
class Action
{
public:
Action(String type);
void Update(TIME dt)
{
if(active)
onUpdate(dt);
}
void Init(ticpp::Element* xml)
{
if(xml == NULL)
return;
//default data extraction, like priority, timing etc
onInit(xml);
}
protected:
virtual void onUpdate(TIME dt) = 0;
virtual void onInit(ticpp::Element* xml) = 0;
};
all derived actions define their own factory class, which uses the static registration trick, also called pluginable factories, if i'm not mistaken. they also hide their constructor, and define static Make() methods for each constructor variant. this way, i only have to write the code for a new action, define it's factory class, which registers itself during static initialization, and can create actions in two ways: via Derived::Make() and via a ActionFactory::Make() which in turn calls the corresponding derived factory.
class Accelerate : public Action
{
public:
static Accelerate* Make();
static Accelerate* Make(ticpp::Element* xml);
onInit(ticpp::Element* xml);
onUpdate(TIME dt);
private:
Accelerate();
class Maker : public ActionFactory
{
static Maker _self;
public:
Maker() : ActionFactory(Accelerate::_type) {}
Accelerate* Make(ticpp::Element* xml);
};
};
now, in order to not replicate a LOT of code which differs in just the type of derived action, i made a template class ActionTemplate<T>, which defines the factory class as a template class too with the same type param.
template<class T>
class ActionTemplate : public Action
{
public:
ActionTemplate() : Action(_type) {}
static T* Make(ticpp::Element* xml)
{ T* ptr = T::Make(); ptr->Init(xml); return ptr; }
protected:
static String _type;
template<T>
class TMaker : public ActionFactory
{
static TMaker _self;
public:
Maker() : ActionFactory(ActionTemplate<T>::_type) {}
T* Make(ticpp::Element* xml) { return T::Make(xml); }
};
};
ActionTemplate derives from Action, doesn't override any methods but adds some new ones, and all concrete derived actions inherit from it using the curiously recurring template pattern, like so:
class Accelerate : public ActionTemplate<Accelerate>
{
...
};
so, here's the short of what i have: base class Action, from which ActionTemplate<T> is derived in order to save me from making a lot of type differing code. from ActionTemplate<T> are derived all others actions, in which i only define the logic (onUpdate(), onInit(), etc) of the concrete action.
and now here's the problem:
i need is a set of actions (for now i seem to need only 3, but...) that are composed of other actions, and update them in a certain order that differs from each container-action. this set of actions should implement an ActionContainer interface which has 2 methods: Add(Action*) and Remove(Action*). basically, i need actions that are dynamically composed of other actions.
i have looked into multiple inheritance, but it doesn't seem right.
i have looked at mixins, and am trying a solution similair to them (template<class T, class Base> class ActionTemplate : public Base {}), but it seems like a lot of problems might come out from it.
i cant inherit the ActionContainer interface from ActionTemplate<T> because the templated code wouldnt work (as can be figured from the code snippet above).
i was thinking about trying composition, but these container-actions ARE Actions, and ARE ActionContainers, so technically they should inherit from them, not compose them.
what i'm looking for, really, is any alternative method or advice to solving this problem of mine.
thank you in advance ^^






