chain of responsibility pattern

19
Chain of Responsibility GoF Behavioral Design Pattern

Upload: helio-costa-e-silva

Post on 08-May-2015

1.111 views

Category:

Real Estate


1 download

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.

TRANSCRIPT

Page 1: Chain of Responsibility Pattern

Chain of Responsibility

GoF Behavioral Design Pattern

Page 2: Chain of Responsibility Pattern

Scenario: User Authentication

Authentication Module

Using email/password

Using Local Network Login/pass

Using Remote login/pass

Page 3: Chain of Responsibility Pattern

Let’s code!

Page 4: Chain of Responsibility Pattern
Page 5: Chain of Responsibility Pattern

This mass is available at https://gist.github.com/hlegius/e1a721ebbc7de2a91fb1

Page 6: Chain of Responsibility Pattern

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

So, let’s go forward on that ?

Page 8: Chain of Responsibility Pattern
Page 9: Chain of Responsibility Pattern

This mass is available at https://gist.github.com/hlegius/e1a721ebbc7de2a91fb1

WTF dude!?

Wrong design decision indicator!

Page 10: Chain of Responsibility Pattern

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

This code is available at https://gist.github.com/hlegius/7188168

Page 12: Chain of Responsibility Pattern
Page 13: Chain of Responsibility Pattern

This code is available at https://gist.github.com/hlegius/7188168

Page 14: Chain of Responsibility Pattern

This code is available at https://gist.github.com/hlegius/7188168

Page 15: Chain of Responsibility Pattern

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

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

Macro point of view - UML

Client UserAuthenticator

Handler Authenticator <abstract>

ConcreteHandler1 EmailAuthenticator

ConcreteHandler2 NetworkAuthenticator

ConcreteHandler3 PartnerAuthenticator

Page 18: Chain of Responsibility Pattern

Don't get me wrong!

You can apply this with PHP, Python, Ruby and so on.

Page 19: Chain of Responsibility Pattern

Thank you!

Reference: GoF bookhttp://programe.me