Guest User

Untitled

a guest
Sep 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. Interface Bloated with Callbacks
  2. struct Event {
  3. enum e { A, B, /*...*/ };
  4. e name;
  5. };
  6.  
  7. class IEventListener {
  8. public:
  9. virtual void event( Event e ) = 0;
  10. };
  11.  
  12. // an event dispatcher implementation:
  13. using namespace std;
  14.  
  15. class EventDispatcher {
  16. public:
  17. typedef std::shared_ptr<IEventListener> IEventListenerPtr;
  18. map<Event::e,vector<IEventListenerPtr>> listeners;
  19.  
  20. void event(Event e){
  21. const vector<IEventListenerPtr> e_listeners=listeners[e.name].second;
  22. //foreach(begin(e_listeners)
  23. // ,end(e_listeners)
  24. // ,bind(IEventListener::event,_1,e));
  25. for(vector<IEventListenerPtr>::const_iterator it=e_listeners.begin()
  26. ; it!=e_listeners.end()
  27. ; ++it)
  28. {
  29. (*it)->event(e);
  30. }
  31. }
  32. };
  33.  
  34. Main main;
  35.  
  36. EventEventDispatcher f1;
  37.  
  38. f1.listeners[Event::A].push_back(listener1);
  39.  
  40. main.listener=f1;
  41.  
  42. class GameEvents
  43. {
  44. public Action<Player> PlayerKilled = p => {};
  45. public Func<Entity, bool> EntityValid = e => true;
  46. public Action ItemPickedUp = () => {};
  47. public Action FlagPickedUp = () => {};
  48. }
  49.  
  50. class IRules
  51. {
  52. GameEvents Events { get; }
  53. }
  54.  
  55. class CaptureTheFlag : IRules
  56. {
  57. GameEvents events = new GameEvents();
  58. public GameEvents Events
  59. {
  60. get { return events; }
  61. }
  62.  
  63. public CaptureTheFlag()
  64. {
  65. events.FlagPickedUp = FlagPickedUp;
  66. }
  67.  
  68. public void FlagPickedUp()
  69. {
  70. score++;
  71. }
  72. }
Add Comment
Please, Sign In to add comment