Upload
helio-costa-e-silva
View
1.111
Download
1
Embed Size (px)
DESCRIPTION
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.
Citation preview
Chain of Responsibility
GoF Behavioral Design Pattern
Scenario: User Authentication
Authentication Module
Using email/password
Using Local Network Login/pass
Using Remote login/pass
Let’s code!
This mass is available at https://gist.github.com/hlegius/e1a721ebbc7de2a91fb1
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!)
So, let’s go forward on that ?
This mass is available at https://gist.github.com/hlegius/e1a721ebbc7de2a91fb1
WTF dude!?
Wrong design decision indicator!
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.
This code is available at https://gist.github.com/hlegius/7188168
This code is available at https://gist.github.com/hlegius/7188168
This code is available at https://gist.github.com/hlegius/7188168
Chain of ResponsibilityBad 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;
Known Uses
• Handle Events – Also known as “Event Handler” or “Responder”
• ET++ used Chain of Responsibility to handle Graphical Updates. (Took from GoF book)
Macro point of view - UML
Client UserAuthenticator
Handler Authenticator <abstract>
ConcreteHandler1 EmailAuthenticator
ConcreteHandler2 NetworkAuthenticator
ConcreteHandler3 PartnerAuthenticator
Don't get me wrong!
You can apply this with PHP, Python, Ruby and so on.