SlideShare a Scribd company logo
1 of 17
S.Ducasse 1
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Stéphane Ducasse
Stephane.Ducasse@univ-savoie.fr
http://www.listic.univ-savoie.fr/~ducasse/
Chain of Responsibility
S.Ducasse 2
License: CC-Attribution-ShareAlike 2.0
http://creativecommons.org/licenses/by-sa/2.0/
S.Ducasse 3
Chain of Responsibility
Avoid coupling the sender of
a request to its receiver by
giving more than one object a
chance to handle the request.
Chain the receiving objects and
pass the request along the chain
until an object handles it.
S.Ducasse 4
Chain of Responsibility
Avoid coupling the sender of a request to its receiver by
giving more than one object a chance to handle the
request.
Chain the receiving objects and pass the request along
the chain until an object handles it.
S.Ducasse 5
Motivation
The problem here is that the object that ultimately
provides the help isn't known explicitly to the object
(e.g., the button) that initiates the help request.
How to decouple senders and receivers?
By giving multiple objects a chance to handle a request.
The request gets passed along a chain of objects until
one of them handles it.
S.Ducasse 6
Chain of Resp. Possible Structure
S.Ducasse 7
Participants
Handler
defines an interface for handling requests
may implement the successor link
ConcreteHandler
handles requests it is responsible for
can access its successor
Client
initiates the request to a concreteHandler
S.Ducasse 8
Dynamic
The first object in the chain receives the request and
either handles it or forwards it to the next candidate on
the chain, which does likewise.
The object that made the request has no explicit
knowledge of who will handle it
S.Ducasse 9
Chain
Can be a linked list
But also a tree (cf. Composite Pattern)
Usually order can represent
specific to more general
priority: more important (security... in SmallWiki)
S.Ducasse 10
Consequences (i)
Reduced coupling. The pattern frees an object from
knowing which other object handles a request.
An object only has to know that a request will be handled
"appropriately."
Both the receiver and the sender have no explicit
knowledge of each other, and an object in the chain doesn't
have to know about the chain's structure.
Simplify object interconnections. Instead of objects
maintaining references to all candidate receivers, they keep
a single reference to their successor
S.Ducasse 11
Consequences (II)
Added flexibility in assigning responsibilities to
objects.
flexibility in distributing responsibilities among objects.
can add or change responsibilities for handling a request by
adding to or otherwise changing the chain at run-time.
Receipt isn't guaranteed.
no guarantee it'll be handled: the request can fall off the
end of the chain without ever being handled.
A request can also go unhandled when the chain is not
configured properly.
S.Ducasse 12
Differences with Decorator
A Decorator usually wraps the decorated object: clients
point to the decorator and not the object
A Decorator does not have to forward the same
message
A decorated object does not have to know that it is
wrapped
With a chain of responsibility, the client asks the first
chain objects explicitly.
S.Ducasse 13
Variations
Do the work or pass? or both?
the DP says that the handler either does the work or
passes it to its successor but it can also do part of the
job (see OO recursion)
S.Ducasse 14
OO Recursion: Hash, = and copy
Person>>= aPerson
^ self name = aPerson name
PersonName>>= aPersonName
^ (self firstName = aPersonName firstName)
and: [(self lastName = aPersonName lastName)]
String>>= aString
...
S.Ducasse 15
OO Recursion: Hash, = and copy
Person>>hash
^ self name hash
PersonName>>hash
^ self firstName hash bitXor: self lastName hash
S.Ducasse 16
OO Recursion
With Chain of Responsibility you may recur from leave
to root, from most specific to more general.
Default in root, specific and recursion in leaves
With OO recursion, from composite (person) to
components (leaves)
Default in leave (String =)
S.Ducasse 17
Smalltalk Specific
Automatic Forwarding with doesNotUnderstand:
can work
but can be dangerous

More Related Content

Viewers also liked (20)

Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop 439-decorator
Stoop 439-decoratorStoop 439-decorator
Stoop 439-decorator
 
Stoop 303-advanced blocks
Stoop 303-advanced blocksStoop 303-advanced blocks
Stoop 303-advanced blocks
 
05 seaside
05 seaside05 seaside
05 seaside
 
Debugging VisualWorks
Debugging VisualWorksDebugging VisualWorks
Debugging VisualWorks
 
Stoop 300-block optimizationinvw
Stoop 300-block optimizationinvwStoop 300-block optimizationinvw
Stoop 300-block optimizationinvw
 
Stoop 436-strategy
Stoop 436-strategyStoop 436-strategy
Stoop 436-strategy
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 
Stoop 413-abstract classes
Stoop 413-abstract classesStoop 413-abstract classes
Stoop 413-abstract classes
 
11 - OOP - Numbers
11 - OOP - Numbers11 - OOP - Numbers
11 - OOP - Numbers
 
01 intro
01 intro01 intro
01 intro
 
Stoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesignStoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesign
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop 431-visitor
Stoop 431-visitorStoop 431-visitor
Stoop 431-visitor
 
10 - OOP - Inheritance (b)
10 - OOP - Inheritance (b)10 - OOP - Inheritance (b)
10 - OOP - Inheritance (b)
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
Double Dispatch
Double DispatchDouble Dispatch
Double Dispatch
 

Similar to Stoop 433-chain

Similar to Stoop 433-chain (12)

9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
 
9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)
 
9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)
 
Stoop 416-lsp
Stoop 416-lspStoop 416-lsp
Stoop 416-lsp
 
Stoop 432-singleton
Stoop 432-singletonStoop 432-singleton
Stoop 432-singleton
 
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
 
Stoop 304-metaclasses
Stoop 304-metaclassesStoop 304-metaclasses
Stoop 304-metaclasses
 
7 - OOP - OO Concepts
7 - OOP - OO Concepts7 - OOP - OO Concepts
7 - OOP - OO Concepts
 
M03_1_Structur alDiagrams.ppt
M03_1_Structur                         alDiagrams.pptM03_1_Structur                         alDiagrams.ppt
M03_1_Structur alDiagrams.ppt
 
Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3
 
Unit two concept of classes and objects
Unit two concept of classes and objects Unit two concept of classes and objects
Unit two concept of classes and objects
 
4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)
 

More from The World of Smalltalk (18)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
10 reflection
10 reflection10 reflection
10 reflection
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
06 debugging
06 debugging06 debugging
06 debugging
 
04 idioms
04 idioms04 idioms
04 idioms
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
02 basics
02 basics02 basics
02 basics
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 

Stoop 433-chain

  • 1. S.Ducasse 1 QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture. Stéphane Ducasse Stephane.Ducasse@univ-savoie.fr http://www.listic.univ-savoie.fr/~ducasse/ Chain of Responsibility
  • 2. S.Ducasse 2 License: CC-Attribution-ShareAlike 2.0 http://creativecommons.org/licenses/by-sa/2.0/
  • 3. S.Ducasse 3 Chain of Responsibility Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
  • 4. S.Ducasse 4 Chain of Responsibility Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
  • 5. S.Ducasse 5 Motivation The problem here is that the object that ultimately provides the help isn't known explicitly to the object (e.g., the button) that initiates the help request. How to decouple senders and receivers? By giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it.
  • 6. S.Ducasse 6 Chain of Resp. Possible Structure
  • 7. S.Ducasse 7 Participants Handler defines an interface for handling requests may implement the successor link ConcreteHandler handles requests it is responsible for can access its successor Client initiates the request to a concreteHandler
  • 8. S.Ducasse 8 Dynamic The first object in the chain receives the request and either handles it or forwards it to the next candidate on the chain, which does likewise. The object that made the request has no explicit knowledge of who will handle it
  • 9. S.Ducasse 9 Chain Can be a linked list But also a tree (cf. Composite Pattern) Usually order can represent specific to more general priority: more important (security... in SmallWiki)
  • 10. S.Ducasse 10 Consequences (i) Reduced coupling. The pattern frees an object from knowing which other object handles a request. An object only has to know that a request will be handled "appropriately." Both the receiver and the sender have no explicit knowledge of each other, and an object in the chain doesn't have to know about the chain's structure. Simplify object interconnections. Instead of objects maintaining references to all candidate receivers, they keep a single reference to their successor
  • 11. S.Ducasse 11 Consequences (II) Added flexibility in assigning responsibilities to objects. flexibility in distributing responsibilities among objects. can add or change responsibilities for handling a request by adding to or otherwise changing the chain at run-time. Receipt isn't guaranteed. no guarantee it'll be handled: the request can fall off the end of the chain without ever being handled. A request can also go unhandled when the chain is not configured properly.
  • 12. S.Ducasse 12 Differences with Decorator A Decorator usually wraps the decorated object: clients point to the decorator and not the object A Decorator does not have to forward the same message A decorated object does not have to know that it is wrapped With a chain of responsibility, the client asks the first chain objects explicitly.
  • 13. S.Ducasse 13 Variations Do the work or pass? or both? the DP says that the handler either does the work or passes it to its successor but it can also do part of the job (see OO recursion)
  • 14. S.Ducasse 14 OO Recursion: Hash, = and copy Person>>= aPerson ^ self name = aPerson name PersonName>>= aPersonName ^ (self firstName = aPersonName firstName) and: [(self lastName = aPersonName lastName)] String>>= aString ...
  • 15. S.Ducasse 15 OO Recursion: Hash, = and copy Person>>hash ^ self name hash PersonName>>hash ^ self firstName hash bitXor: self lastName hash
  • 16. S.Ducasse 16 OO Recursion With Chain of Responsibility you may recur from leave to root, from most specific to more general. Default in root, specific and recursion in leaves With OO recursion, from composite (person) to components (leaves) Default in leave (String =)
  • 17. S.Ducasse 17 Smalltalk Specific Automatic Forwarding with doesNotUnderstand: can work but can be dangerous