一些其他的想法不使用std ::功能,它使用的std ::設置與您共創你要執行的每個動作類。這個想法是,動作類是在cpp文件中定義的(不是在頭文件中,除非它們是共享的),它們是完全隱藏的。
首先定義動作類,它具有一個純虛執行,優先級和一個指向一些所有者。我們的想法是不產生其他類,元組,但使用行動類來存儲這些值:
class Action
{
public:
Action(const int priority, void * owner) : priority(priority), owner(owner) {}
virtual ~Action() {}
virtual void execute() = 0;
struct PrioritySorter
{
bool operator()(Action* a, Action* b)
{
return a->priority < b->priority;
}
};
bool ownerIs(void * owner) const { return this->owner == owner; }
private:
const int priority;
void * owner;
};
然後創建了一些行動:
class DrawBackgroundAction : public Action
{
public:
DrawBackgroundAction(const int priority, void * owner) : Action(priority, owner) {}
void execute()
{
cout << "drawing background" << endl;
}
};
class DrawForegroundAction : public Action
{
public:
DrawForegroundAction(const int priority, void * owner) : Action(priority, owner) {}
void execute()
{
cout << "drawing foreground!!!" << endl;
}
};
class DrawSomethingElseAction : public Action
{
public:
DrawSomethingElseAction(const int priority, void * owner) : Action(priority, owner) {}
void execute()
{
cout << "drawing something else" << endl;
}
};
的DrawManager需要存儲的護理按優先級排序的集合中的動作,如果想要刪除「擁有」的動作(某些其他類),則可以執行此操作。
class DrawManager
{
public:
DrawManager() {}
~DrawManager()
{
for (ActionList::iterator i = actions.begin(), e = actions.end(); i != e; i++)
{
delete *i;
}
actions.clear();
}
void draw()
{
for (ActionList::iterator i = actions.begin(), e = actions.end(); i != e; i++)
{
(*i)->execute();
}
}
void addAction(Action* action)
{
actions.insert(action);
}
void removeOwnedActions(void * owner)
{
for (ActionList::iterator i = actions.begin(), e = actions.end(); i != e; i++)
{
if ((*i)->ownerIs(owner))
{
delete *i;
actions.erase(i);
}
}
}
private:
typedef std::set<Action*,Action::PrioritySorter> ActionList;
ActionList actions;
};
現在的例子類:
class Example
{
public:
Example()
{
manager.addAction(new DrawForegroundAction(100,this));
manager.addAction(new DrawBackgroundAction(0,this));
manager.addAction(new DrawSomethingElseAction(50,this));
}
void drawAll()
{
manager.draw();
}
void removeTheActionsIfYouWant()
{
manager.removeOwnedActions(this);
}
private:
DrawManager manager;
};
而且測試:
int main()
{
Example ex;
cout << "Drawing all" << endl;
ex.drawAll();
ex.removeTheActionsIfYouWant();
cout << "Drawing again" << endl;
ex.drawAll();
return 0;
}
這裏的東西類似,如果這有助於? http://forums.codeguru.com/showthread.php?489732.html – JohnLBevan
Example類中的DrawManager對象是否被其他對象共享? –
你能舉一個你用'std :: function'做的例子嗎?因爲我無法破譯你的描述。 :) – jalf