c++ - Get Singleton of class that WILL be included -
i have classes:
activity
, activitymanager
(that extends activity
).
i have 1 method in activity
, gets singleton object of activitymanager
.
after while, saw got "live without life" paradox, activitymanager "created" compiler, after activity is. so, how do sort of stuff?
i mean, how can class of object extended first one?
i don't know how put on google, so, me please! thanks
edit
simplified activity
class activity { public: void start(); }; void activity::start(void){ // start current activity on activitymanager stack activitymanager *am; = activitymanager::getinstance(); am->start(this); }
simplified activitymanager
:
class activitymanager: public activity { private: // singleton object static activitymanager *singleton; public: // singleton method return defauld object instance static activitymanager* getinstance(void); // overload method, used run others activityes void start(activity& activity); }; // singleton method return default activitymanager object instance activitymanager* activitymanager::getinstance(){ if(!singleton) singleton = new activitymanager(); return singleton; } // function called on request start new activity void activitymanager::start(activity& activity){ }
i think composite pattern (http://en.wikipedia.org/wiki/composite_pattern) better needs.
it variation (naming bit screwed in terms of pattern, more code way):
class activity { public: virtual ~activity(); virtual void start(); virtual void add(activity*); virtual void remove(activity*); private: activity(); } class activitymanager : activity { public: concreteactivity(); ~concreteactivity(); void start(); void add(activity*); void remove(activity*); private: list<activity*> _activities; } void activitymanager::start() { list<activity*>::const_iterator concreteactivity; for(concreteactivity = _activities.begin(), concreteactivity != _activities.end(); ++concreteactivity) { *concreteactivity->start(); } } void activitymanager::add(activity* newactivity) { _activities.push_back(newactivity); } void activitymanager::remove(activity* activitytodelete) { _activities.remove(activitytodelete); } class someactivity : activity { public: someactivity(); ~someactivity(); void start(); } class otheractivity : activity { public: otheractivity(); ~otheractivity(); void start(); }
and example of usage:
activitymanager* manager = new activitymanager(); someactivity* activity_one = new someactivity(); otheractivity* activity_two = new otheractivity(); manager->add(activity_one); manager->add(activity_two); manager->start();
and still can make activitymanager singleton, if want. without additional info on project, can't think of better way.
Comments
Post a Comment