Chain of Responsibility Pattern

1,646 views

Published on

Talk presented at National Institute For Space Research (INPE) in Brazil.

Explain how Chain of Responsibility pattern can help you to build a better and decoupled code.

Published in: Real Estate, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,646
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
16
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Chain of Responsibility Pattern

  1. 1. Chain of Responsibility GoF Behavioral Design Pattern
  2. 2. Scenario: User Authentication Using email/password Using Local Network Login/pass Authentication Module Using Remote login/pass
  3. 3. Let’s code!
  4. 4. This mass is available at https://gist.github.com/hlegius/e1a721ebbc7de2a91fb1
  5. 5. Code Review • • • • • • • We got a Service class; Skinny Controllers; Meaningful (and cool) names; No temp variable; Short methods; A lot of non-sense `ifs` statements; (Very) Complicated logic. (WTF award!)
  6. 6. So, let’s go forward on that ?
  7. 7. WTF dude!? Wrong design decision indicator! This mass is available at https://gist.github.com/hlegius/e1a721ebbc7de2a91fb1
  8. 8. Chain of Responsibility • Decouple client from receivers when you got more than one object that can handle it; • Giving you the chance to test each part (sender and receivers) in isolated. • Create one point of change if new Receiver arrives. • Added flexibility in assigning responsibilities to objects; • Boost your code readability like a boss.
  9. 9. This code is available at https://gist.github.com/hlegius/7188168
  10. 10. This code is available at https://gist.github.com/hlegius/7188168
  11. 11. This code is available at https://gist.github.com/hlegius/7188168
  12. 12. Chain of Responsibility Bad Consequences • More Lines of Code than first version; • Complex if you have one or two fixes receivers; • Receipt is not guaranteed that will be handled;
  13. 13. Known Uses • Handle Events – Also known as “Event Handler” or “Responder” • ET++ used Chain of Responsibility to handle Graphical Updates. (Took from GoF book)
  14. 14. Macro point of view - UML Client UserAuthenticator Handler Authenticator <abstract> ConcreteHandler1 EmailAuthenticator ConcreteHandler2 NetworkAuthenticator ConcreteHandler3 PartnerAuthenticator
  15. 15. Don't get me wrong! You can apply this with PHP, Python, Ruby and so on.
  16. 16. Thank you! http://programe.me Reference: GoF book

×