Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Interface Bloated with Callbacks
- struct Event {
- enum e { A, B, /*...*/ };
- e name;
- };
- class IEventListener {
- public:
- virtual void event( Event e ) = 0;
- };
- // an event dispatcher implementation:
- using namespace std;
- class EventDispatcher {
- public:
- typedef std::shared_ptr<IEventListener> IEventListenerPtr;
- map<Event::e,vector<IEventListenerPtr>> listeners;
- void event(Event e){
- const vector<IEventListenerPtr> e_listeners=listeners[e.name].second;
- //foreach(begin(e_listeners)
- // ,end(e_listeners)
- // ,bind(IEventListener::event,_1,e));
- for(vector<IEventListenerPtr>::const_iterator it=e_listeners.begin()
- ; it!=e_listeners.end()
- ; ++it)
- {
- (*it)->event(e);
- }
- }
- };
- Main main;
- EventEventDispatcher f1;
- f1.listeners[Event::A].push_back(listener1);
- main.listener=f1;
- class GameEvents
- {
- public Action<Player> PlayerKilled = p => {};
- public Func<Entity, bool> EntityValid = e => true;
- public Action ItemPickedUp = () => {};
- public Action FlagPickedUp = () => {};
- }
- class IRules
- {
- GameEvents Events { get; }
- }
- class CaptureTheFlag : IRules
- {
- GameEvents events = new GameEvents();
- public GameEvents Events
- {
- get { return events; }
- }
- public CaptureTheFlag()
- {
- events.FlagPickedUp = FlagPickedUp;
- }
- public void FlagPickedUp()
- {
- score++;
- }
- }
Add Comment
Please, Sign In to add comment