An Event-Driven Approach for the Separation of Concerns

5,228 views
5,097 views

Published on

Published in: Technology
1 Comment
5 Likes
Statistics
Notes
No Downloads
Views
Total views
5,228
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
37
Comments
1
Likes
5
Embeds 0
No embeds

No notes for slide

An Event-Driven Approach for the Separation of Concerns

  1. 1. An Event-Driven Approachfor the Separation of Concerns Hayim Makabee Yahoo! Labs Haifa, Israel
  2. 2. Separation of ConcernsDefinition: Separation of Concerns A software system must be decomposed into parts that overlap in functionality as little as possible.YAHOO! CONFIDENTIAL -2-
  3. 3. Coupling and Cohesion• Coupling: The degree of dependency between two modules.• Cohesion: The measure of how strongly-related is the set of functions performed by a module.YAHOO! CONFIDENTIAL -3-
  4. 4. OOP and AOP• OOP reduces coupling: – Encapsulation – Dynamic binding and polymorphism• AOP increases cohesion: – Separates cross-cutting concernsYAHOO! CONFIDENTIAL -4-
  5. 5. Event-Driven Programming (EDP)• Reactive Systems are essentially event-driven.• Hybrid systems use EDP only to implement a specific part of their functionality: – Graphical User Interface – Publish-Subscribe – Observer pattern• In general EDP is not used to separate concerns. It is a modelling tool.YAHOO! CONFIDENTIAL -5-
  6. 6. EDP and the Separation of Concerns• Our claim: EDP should be adopted as an alternative tool for the Separation of Concerns.• An EDP approach can be used to reduce coupling and increase cohesion.• Should be based on the explicit definition of Events and Event Handlers.YAHOO! CONFIDENTIAL -6-
  7. 7. The EventJ Framework• Events: – Immutable objects. – Have state and getter functions. – Have type and are organized in an inheritance hierarchy.YAHOO! CONFIDENTIAL -7-
  8. 8. EventHandlers• EventHandlers: – Execute some action in response to an Event. – May subscribe to different types of Events. – May be stateless or stateful. – May trigger Events themselves. – Receive Events asynchronously. – Do not depend on the results of other EventHandlers. – Run on their own threads. – Manage their own queue of Events.YAHOO! CONFIDENTIAL -8-
  9. 9. Event Dispatcher• EventDispatcher: – Singleton. – Propagates Events to the appropriate EventHandlers. – Events are initially stored in the EventDispatcher’s central queue. – Each Event is asynchronously propagated to all the EventHandlers that have subscribed to its type. – EventHandlers call the EventDispatcher to subscribe to Event types. – The EventDispatcher runs on a separate thread.YAHOO! CONFIDENTIAL -9-
  10. 10. Separating Concerns with Events• Identification: Identify the concerns that may be separated from the main functionality (specific pieces of code that can be moved to some other module).• Triggering: For each concern, define an appropriate type of Event and insert the triggering of this Event in the suitable places in the code.• Handling: For each type of Event, implement the associated EventHandler(s). Explicitly separate the pieces of code from the Identification step and move them to the respective handlers.YAHOO! CONFIDENTIAL - 10 -
  11. 11. Pre-Conditions to Separate Concerns• Concurrency: Since Events are handled asynchronously, it must be possible to execute this separated piece of code in parallel with the rest of the original code.• Non-dependency: Since EventHandlers should not modify any external data, the execution of the original code must not depend on the results of the execution of the piece of code that was separated.YAHOO! CONFIDENTIAL - 11 -
  12. 12. Example: An Instant-Messaging System• Each user has a list of contacts (friends).• Must know who among his contacts is available to chat.• The system must manage the user status.• Whenever there is a change in this status the system must notify all his contacts that are currently online.• Uses a subscription model, in which each online user is subscribed to all his friends.YAHOO! CONFIDENTIAL - 12 -
  13. 13. Login and LogoutLogin(User u){ u.SetStatus(Online); NotificationMgr.NotifyContacts(u); SubscriptionMgr.SubscribeContacts(u);}Logout(User u){ u.SetStatus(Offline); NotificationMgr.NotifyContacts(u); SubscriptionMgr.UnsubscribeContacts(u);}YAHOO! CONFIDENTIAL - 13 -
  14. 14. TimeoutTimeout(User u){ u.SetStatus(Offline); NotificationMgr.NotifyContacts(u); SubscriptionMgr.UnsubscribeContacts(u);}YAHOO! CONFIDENTIAL - 14 -
  15. 15. Separating Cross-cutting Concerns with EventsLogin(User u){ u.SetStatus(Online);}Logout(User u){ u.SetStatus(Offline);}Timeout(User u){ u.SetStatus(Offline);}YAHOO! CONFIDENTIAL - 15 -
  16. 16. UserUser.SetStatus(Status status){ this.status = status; EventDispatcher.Trigger(new UserStatusChangedEvent(this));}YAHOO! CONFIDENTIAL - 16 -
  17. 17. HandlersNotificationHandler.Handle(UserStatusChangedEvent e){ NotificationMgr.NotifyContacts(e.getUser());}SubscriptionHandler.Handle(UserStatusChangedEvent e){ if (e.getUser().isOnline()) { SubscriptionMgr.SubscribeContacts(e.getUser()); } else { SubscriptionMgr.UnsubscribeContacts(e.getUser()); }}YAHOO! CONFIDENTIAL - 17 -
  18. 18. Comparison: EDP vs. AOP• Encapsulation• Inheritance• Coupling• Order of Execution• Invocation• Extensibility• Reusability• Concurrency - 18 -
  19. 19. EncapsulationEncapsulation:• EDP does not violate encapsulation. Event Handlers have no access to the data members of other classes.• AOP allows the violation of encapsulation. An advice may change the value of any variable anywhere in the code.YAHOO! CONFIDENTIAL - 19 -
  20. 20. InheritanceInheritance:• EDP can use inheritance. Event types and EventHandlers can be organized in hierarchies.• AOP does not use inheritance. Pointcuts cannot be organized in a hierarchy, since they are defined by name. Aspects cannot inherit from other aspects.YAHOO! CONFIDENTIAL - 20 -
  21. 21. CouplingCoupling:• EDP supports coupling by type. An EventHandler is coupled to a type of Event.• AOP allows coupling by name. An aspect can execute over a specific function or variable, by name. If the name of this variable is changed, the aspect must be changed as well.YAHOO! CONFIDENTIAL - 21 -
  22. 22. Order of ExecutionOrder of Execution:• EDP uses EventHandlers which are executed asynchronously and in no particular order, since their execution should be independent of each other and should not directly affect the rest of the system.• AOP has aspects whose execution order is not well- defined and thus if several aspects execute as a consequence of the same code, the results may be unpredictable.YAHOO! CONFIDENTIAL - 22 -
  23. 23. InvocationInvocation:• EDP is based on explicit invocations. The Events are triggered explicitly. For each method it is possible to know which Events are triggered by it, and for each Event type it is clear which EventHandlers handle it.• AOP uses implicit invocations. By observing a piece of code there is nothing that indicates that an aspect may be executed. Given an aspect, it is hard to find in the system all pieces of code affected by it.YAHOO! CONFIDENTIAL - 23 -
  24. 24. ExtensibilityExtensibility:• EDP makes it easy to add a new EventHandler for some existing Event type or to trigger an Event of an existing type in some new function.• AOP is not easily extensible. If a new advice must be added to some existing pointcut it is necessary to repeat the pointcut definition in a new aspect. If we want to extend an advice to be applied to more pointcuts, it is necessary to change the code of the original aspect.YAHOO! CONFIDENTIAL - 24 -
  25. 25. ReusabilityReusability:• EDP supports reusability of Events and EventHandlers. Handlers are modular and reusable since they are coupled to Event types, and are independent of the rest of the system.• AOP defines aspects which have small potential for reuse in other systems, since they are coupled to functions and variables by name.YAHOO! CONFIDENTIAL - 25 -
  26. 26. ConcurrencyConcurrency:• EDP supports EventHandlers that can be executed in parallel, since they are encapsulated and do not affect code outside them.• AOP does not support concurrency. Advices cannot be executed in parallel. Their execution must be serialized since they can potentially affect the same piece of code.YAHOO! CONFIDENTIAL - 26 -
  27. 27. Conclusions• We proposed the adoption of an Event-Driven approach for the Separation of Concerns. – Methodology to identify cross-cutting concerns and separate them using events and event handlers. – Defined pre-requisites to perform this change. – Presented concrete example using the EventJ framework. – Compared our approach with AOP.YAHOO! CONFIDENTIAL - 27 -
  28. 28. Thank You! - 28 -

×