Download - Chain of Responsibility Pattern
![Page 1: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/1.jpg)
Chain of Responsibility
GoF Behavioral Design Pattern
![Page 2: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/2.jpg)
Scenario: User Authentication
Authentication Module
Using email/password
Using Local Network Login/pass
Using Remote login/pass
![Page 3: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/3.jpg)
Let’s code!
![Page 4: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/4.jpg)
![Page 5: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/5.jpg)
This mass is available at https://gist.github.com/hlegius/e1a721ebbc7de2a91fb1
![Page 6: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/6.jpg)
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!)
![Page 7: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/7.jpg)
So, let’s go forward on that ?
![Page 8: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/8.jpg)
![Page 9: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/9.jpg)
This mass is available at https://gist.github.com/hlegius/e1a721ebbc7de2a91fb1
WTF dude!?
Wrong design decision indicator!
![Page 10: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/10.jpg)
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.
![Page 11: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/11.jpg)
This code is available at https://gist.github.com/hlegius/7188168
![Page 12: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/12.jpg)
![Page 13: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/13.jpg)
This code is available at https://gist.github.com/hlegius/7188168
![Page 14: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/14.jpg)
This code is available at https://gist.github.com/hlegius/7188168
![Page 15: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/15.jpg)
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;
![Page 16: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/16.jpg)
Known Uses
• Handle Events – Also known as “Event Handler” or “Responder”
• ET++ used Chain of Responsibility to handle Graphical Updates. (Took from GoF book)
![Page 17: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/17.jpg)
Macro point of view - UML
Client UserAuthenticator
Handler Authenticator <abstract>
ConcreteHandler1 EmailAuthenticator
ConcreteHandler2 NetworkAuthenticator
ConcreteHandler3 PartnerAuthenticator
![Page 18: Chain of Responsibility Pattern](https://reader034.vdocument.in/reader034/viewer/2022052618/554bb375b4c90530298b45c8/html5/thumbnails/18.jpg)
Don't get me wrong!
You can apply this with PHP, Python, Ruby and so on.