SlideShare a Scribd company logo
Design Pattern
Chain of Responsibility
Introduction
• Category : Behavioral
• Founder : Gang of Four
UML Diagram
Intent
• 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.
• Launch-and-leave requests with a single processing pipeline
that contains many possible handlers.
• An object-oriented linked list with recursive traversal.
Problem
• There is a potentially variable number of "handler" or
"processing element" or "node" objects, and a stream of
requests that must be handled.
• Need to efficiently process the requests without hard-wiring
handler relationships and precedence, or request-to-handler
mappings.
Solution
• Encapsulate the processing elements inside a "pipeline" abstraction; and have
clients "launch and leave" their requests at the entrance to the pipeline.
• The pattern chains the receiving objects together, and then passes any request
messages from object to object until it reaches an object capable of handling the
message. The number and type of handler objects isn't known a priori, they can be
configured dynamically.
• The chaining mechanism uses recursive composition to allow an unlimited number
of handlers to be linked.
• Chain of Responsibility simplifies object interconnections. Instead of senders and
receivers maintaining references to all candidate receivers, each sender keeps a
single reference to the head of the chain, and each receiver keeps a single reference
to its immediate successor in the chain.
• Make sure there exists a "safety net" to "catch" any requests which go unhandled.
• Do not use Chain of Responsibility when each request is only handled by one
handler, or, when the client object knows which service object should handle the
request.
Architecture
• The derived classes know how to satisfy Client requests. If the "current"
object is not available or sufficient, then it delegates to the base class,
which delegates to the "next" object, and the circle of life continues.
• Multiple handlers could contribute to the handling of each request. The
request can be passed down the entire length of the chain, with the last link
being careful not to delegate to a "null next".
Code Snippet
Code Snippet
Code Snippet
Code Snippet
Code Snippet
Real Life Example
• ATM use the Chain of Responsibility in money giving
mechanism.
• User enters the amount to be retrieved and the ATM dispense
amount in terms of defined currency bills such as 100 Rs, 50
Rs, 20 Rs, 5 Rs, etc.
Points to Remember
• The base class maintains a "next" pointer.
• Each derived class implements its contribution for handling
the request.
• If the request needs to be "passed on", then the derived class
"calls back" to the base class, which delegates to the "next"
pointer.
• The client (or some third party) creates and links the chain
(which may include a link from the last node to the root node).
• The client "launches and leaves" each request with the root of
the chain.
• Recursive delegation produces the illusion of magic.

More Related Content

What's hot

Ooad sequence diagram lecture
Ooad sequence diagram lectureOoad sequence diagram lecture
Ooad sequence diagram lecture
Technology & Education
 
Mediator pattern
Mediator patternMediator pattern
Mediator pattern
Shakil Ahmed
 
Sequence diagram
Sequence diagramSequence diagram
Sequence diagram
Kaushik Ghosh
 
Enterprise java unit-2_chapter-3
Enterprise  java unit-2_chapter-3Enterprise  java unit-2_chapter-3
Enterprise java unit-2_chapter-3
sandeep54552
 
Sequence Diagram
Sequence DiagramSequence Diagram
React hooks
React hooksReact hooks
React hooks
Assaf Gannon
 
Sequence diagram- UML diagram
Sequence diagram- UML diagramSequence diagram- UML diagram
Sequence diagram- UML diagram
Ramakant Soni
 
Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
Shakil Ahmed
 
Fragment
Fragment Fragment
The Ultimate Sequence Diagram Tutorial
The Ultimate Sequence Diagram TutorialThe Ultimate Sequence Diagram Tutorial
The Ultimate Sequence Diagram Tutorial
Creately
 
Object Oriented Design
Object Oriented DesignObject Oriented Design
Object Oriented Design
Sudarsun Santhiappan
 
Proxy design pattern (Class Ambassador)
Proxy design pattern (Class Ambassador)Proxy design pattern (Class Ambassador)
Proxy design pattern (Class Ambassador)
Sameer Rathoud
 
Iterator Design Pattern
Iterator Design PatternIterator Design Pattern
Iterator Design Pattern
Varun Arora
 
Lecture11 use case sequence diagram
Lecture11 use case sequence diagramLecture11 use case sequence diagram
Lecture11 use case sequence diagram
Shahid Riaz
 
Uml sequence diagrams
Uml sequence diagramsUml sequence diagrams
Uml sequence diagrams
ASHOK KUMAR PALAKI
 
Collaboration diagram- UML diagram
Collaboration diagram- UML diagram Collaboration diagram- UML diagram
Collaboration diagram- UML diagram
Ramakant Soni
 
GRID VIEW PPT
GRID VIEW PPTGRID VIEW PPT
Communication diagram Introduction
Communication diagram IntroductionCommunication diagram Introduction
Communication diagram Introduction
Dineesha Suraweera
 
Proxy design pattern
Proxy design patternProxy design pattern
Proxy design pattern
Sase Kleckovski
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
Satyamevjayte Haxor
 

What's hot (20)

Ooad sequence diagram lecture
Ooad sequence diagram lectureOoad sequence diagram lecture
Ooad sequence diagram lecture
 
Mediator pattern
Mediator patternMediator pattern
Mediator pattern
 
Sequence diagram
Sequence diagramSequence diagram
Sequence diagram
 
Enterprise java unit-2_chapter-3
Enterprise  java unit-2_chapter-3Enterprise  java unit-2_chapter-3
Enterprise java unit-2_chapter-3
 
Sequence Diagram
Sequence DiagramSequence Diagram
Sequence Diagram
 
React hooks
React hooksReact hooks
React hooks
 
Sequence diagram- UML diagram
Sequence diagram- UML diagramSequence diagram- UML diagram
Sequence diagram- UML diagram
 
Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 
Fragment
Fragment Fragment
Fragment
 
The Ultimate Sequence Diagram Tutorial
The Ultimate Sequence Diagram TutorialThe Ultimate Sequence Diagram Tutorial
The Ultimate Sequence Diagram Tutorial
 
Object Oriented Design
Object Oriented DesignObject Oriented Design
Object Oriented Design
 
Proxy design pattern (Class Ambassador)
Proxy design pattern (Class Ambassador)Proxy design pattern (Class Ambassador)
Proxy design pattern (Class Ambassador)
 
Iterator Design Pattern
Iterator Design PatternIterator Design Pattern
Iterator Design Pattern
 
Lecture11 use case sequence diagram
Lecture11 use case sequence diagramLecture11 use case sequence diagram
Lecture11 use case sequence diagram
 
Uml sequence diagrams
Uml sequence diagramsUml sequence diagrams
Uml sequence diagrams
 
Collaboration diagram- UML diagram
Collaboration diagram- UML diagram Collaboration diagram- UML diagram
Collaboration diagram- UML diagram
 
GRID VIEW PPT
GRID VIEW PPTGRID VIEW PPT
GRID VIEW PPT
 
Communication diagram Introduction
Communication diagram IntroductionCommunication diagram Introduction
Communication diagram Introduction
 
Proxy design pattern
Proxy design patternProxy design pattern
Proxy design pattern
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 

Similar to Chain of responsibility pattern

Salesforce Omnichannel flow
Salesforce Omnichannel flowSalesforce Omnichannel flow
Salesforce Omnichannel flow
chakravarthy vivek
 
note2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptxnote2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptx
ReemaAsker1
 
queuing theory/ waiting line theory
queuing theory/ waiting line theoryqueuing theory/ waiting line theory
queuing theory/ waiting line theory
Arushi Verma
 
Patterns of Distributed Application Design
Patterns of Distributed Application DesignPatterns of Distributed Application Design
Patterns of Distributed Application Design
GlobalLogic Ukraine
 
System modeling and simulations
System modeling and simulationsSystem modeling and simulations
System modeling and simulations
Seydi Alkan
 
Design pattern and their application
Design pattern and their applicationDesign pattern and their application
Design pattern and their application
Hiệp Tiến
 
Patterns of Distributed Application Design
Patterns of Distributed Application DesignPatterns of Distributed Application Design
Patterns of Distributed Application Design
Orkhan Gasimov
 
Operating characteristics ofqueuing system
Operating characteristics ofqueuing systemOperating characteristics ofqueuing system
Operating characteristics ofqueuing system
Aminul Tanvin
 
Mendix rest services
Mendix rest servicesMendix rest services
Mendix rest services
G Acellam
 
Dot NET Remoting
Dot NET RemotingDot NET Remoting
Dot NET Remoting
Nikhil Palyekar
 
ppt-3-iotlogicdesign-210125034351.pptx
ppt-3-iotlogicdesign-210125034351.pptxppt-3-iotlogicdesign-210125034351.pptx
ppt-3-iotlogicdesign-210125034351.pptx
johnpragasam1
 
AWS Study Group - Chapter 07 - Integrating Application Services [Solution Arc...
AWS Study Group - Chapter 07 - Integrating Application Services [Solution Arc...AWS Study Group - Chapter 07 - Integrating Application Services [Solution Arc...
AWS Study Group - Chapter 07 - Integrating Application Services [Solution Arc...
QCloudMentor
 
Microservices Practitioner Summit Jan '15 - Microservice Ecosystems At Scale ...
Microservices Practitioner Summit Jan '15 - Microservice Ecosystems At Scale ...Microservices Practitioner Summit Jan '15 - Microservice Ecosystems At Scale ...
Microservices Practitioner Summit Jan '15 - Microservice Ecosystems At Scale ...
Ambassador Labs
 
Effective Akka v2
Effective Akka v2Effective Akka v2
Effective Akka v2
shinolajla
 
common design patterns summary.pdf
common design patterns summary.pdfcommon design patterns summary.pdf
common design patterns summary.pdf
NikolayRaychev2
 
Intro to Microservices Architecture
Intro to Microservices ArchitectureIntro to Microservices Architecture
Intro to Microservices Architecture
Peter Nijem
 
Queuing theory
Queuing theoryQueuing theory
Queuing theory
Shinki Jalhotra
 
Managing Data in Microservices
Managing Data in MicroservicesManaging Data in Microservices
Managing Data in Microservices
Randy Shoup
 
Ppt 3 - IOT logic design
Ppt   3 - IOT logic designPpt   3 - IOT logic design
Ppt 3 - IOT logic design
udhayakumarc1
 
Real-Time Design Patterns
Real-Time Design PatternsReal-Time Design Patterns
Real-Time Design Patterns
GlobalLogic Ukraine
 

Similar to Chain of responsibility pattern (20)

Salesforce Omnichannel flow
Salesforce Omnichannel flowSalesforce Omnichannel flow
Salesforce Omnichannel flow
 
note2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptxnote2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptx
 
queuing theory/ waiting line theory
queuing theory/ waiting line theoryqueuing theory/ waiting line theory
queuing theory/ waiting line theory
 
Patterns of Distributed Application Design
Patterns of Distributed Application DesignPatterns of Distributed Application Design
Patterns of Distributed Application Design
 
System modeling and simulations
System modeling and simulationsSystem modeling and simulations
System modeling and simulations
 
Design pattern and their application
Design pattern and their applicationDesign pattern and their application
Design pattern and their application
 
Patterns of Distributed Application Design
Patterns of Distributed Application DesignPatterns of Distributed Application Design
Patterns of Distributed Application Design
 
Operating characteristics ofqueuing system
Operating characteristics ofqueuing systemOperating characteristics ofqueuing system
Operating characteristics ofqueuing system
 
Mendix rest services
Mendix rest servicesMendix rest services
Mendix rest services
 
Dot NET Remoting
Dot NET RemotingDot NET Remoting
Dot NET Remoting
 
ppt-3-iotlogicdesign-210125034351.pptx
ppt-3-iotlogicdesign-210125034351.pptxppt-3-iotlogicdesign-210125034351.pptx
ppt-3-iotlogicdesign-210125034351.pptx
 
AWS Study Group - Chapter 07 - Integrating Application Services [Solution Arc...
AWS Study Group - Chapter 07 - Integrating Application Services [Solution Arc...AWS Study Group - Chapter 07 - Integrating Application Services [Solution Arc...
AWS Study Group - Chapter 07 - Integrating Application Services [Solution Arc...
 
Microservices Practitioner Summit Jan '15 - Microservice Ecosystems At Scale ...
Microservices Practitioner Summit Jan '15 - Microservice Ecosystems At Scale ...Microservices Practitioner Summit Jan '15 - Microservice Ecosystems At Scale ...
Microservices Practitioner Summit Jan '15 - Microservice Ecosystems At Scale ...
 
Effective Akka v2
Effective Akka v2Effective Akka v2
Effective Akka v2
 
common design patterns summary.pdf
common design patterns summary.pdfcommon design patterns summary.pdf
common design patterns summary.pdf
 
Intro to Microservices Architecture
Intro to Microservices ArchitectureIntro to Microservices Architecture
Intro to Microservices Architecture
 
Queuing theory
Queuing theoryQueuing theory
Queuing theory
 
Managing Data in Microservices
Managing Data in MicroservicesManaging Data in Microservices
Managing Data in Microservices
 
Ppt 3 - IOT logic design
Ppt   3 - IOT logic designPpt   3 - IOT logic design
Ppt 3 - IOT logic design
 
Real-Time Design Patterns
Real-Time Design PatternsReal-Time Design Patterns
Real-Time Design Patterns
 

Recently uploaded

How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
VALiNTRY360
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 

Recently uploaded (20)

How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 

Chain of responsibility pattern

  • 1. Design Pattern Chain of Responsibility
  • 2. Introduction • Category : Behavioral • Founder : Gang of Four
  • 4. Intent • 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. • Launch-and-leave requests with a single processing pipeline that contains many possible handlers. • An object-oriented linked list with recursive traversal.
  • 5. Problem • There is a potentially variable number of "handler" or "processing element" or "node" objects, and a stream of requests that must be handled. • Need to efficiently process the requests without hard-wiring handler relationships and precedence, or request-to-handler mappings.
  • 6. Solution • Encapsulate the processing elements inside a "pipeline" abstraction; and have clients "launch and leave" their requests at the entrance to the pipeline. • The pattern chains the receiving objects together, and then passes any request messages from object to object until it reaches an object capable of handling the message. The number and type of handler objects isn't known a priori, they can be configured dynamically. • The chaining mechanism uses recursive composition to allow an unlimited number of handlers to be linked. • Chain of Responsibility simplifies object interconnections. Instead of senders and receivers maintaining references to all candidate receivers, each sender keeps a single reference to the head of the chain, and each receiver keeps a single reference to its immediate successor in the chain. • Make sure there exists a "safety net" to "catch" any requests which go unhandled. • Do not use Chain of Responsibility when each request is only handled by one handler, or, when the client object knows which service object should handle the request.
  • 7.
  • 8. Architecture • The derived classes know how to satisfy Client requests. If the "current" object is not available or sufficient, then it delegates to the base class, which delegates to the "next" object, and the circle of life continues. • Multiple handlers could contribute to the handling of each request. The request can be passed down the entire length of the chain, with the last link being careful not to delegate to a "null next".
  • 14. Real Life Example • ATM use the Chain of Responsibility in money giving mechanism. • User enters the amount to be retrieved and the ATM dispense amount in terms of defined currency bills such as 100 Rs, 50 Rs, 20 Rs, 5 Rs, etc.
  • 15.
  • 16. Points to Remember • The base class maintains a "next" pointer. • Each derived class implements its contribution for handling the request. • If the request needs to be "passed on", then the derived class "calls back" to the base class, which delegates to the "next" pointer. • The client (or some third party) creates and links the chain (which may include a link from the last node to the root node). • The client "launches and leaves" each request with the root of the chain. • Recursive delegation produces the illusion of magic.