Using the Chain of Responsibility Design Pattern in Java
Join the DZone community and get the full member experience.Join For Free
Here, I am with another article on design patterns — Chain of Responsibility. This article will also conclude explanation of all basic design patterns. Please visit my profile to find the other basic design patterns for reference.
Chain of Responsibility Design Pattern
- The Chain of Responsibility Design Pattern is one of the Gang of Four design patterns which creates a chain of receiver objects for a request.
- The Chain of Responsibility pattern is a behavioral pattern which is used to avoid coupling the sender of the request to the receiver and gives more than one receiver object the opportunity to handle the request.
- As per Gang of Four design patterns, the Chain of Responsibility pattern is defined as:
"Gives more than one object an opportunity to handle a request by linking receiving objects together."
- The Chain of Responsibility pattern allows a number of classes to attempt to handle a request independently.
- The Receiver objects of the requests are free from the order and can be use in any order for processing.
- This pattern decouples sender and receiver objects based on type of request.
- This pattern defines a chain of receiver objects having the responsibility, depending on run-time conditions, to either handle a request or forward it to the next receiver on the chain.
- This pattern helps us avoiding coupling of sender and receiver objects of a requests and allows us to have more than one receiver as well for the request.
- ATM withdrawal using different currency notes is one of the great example of Chain of Responsibility pattern.
- Using different types of Loggers in our software is another example of the pattern.
- A call to technical support at different level of discussion is also a good example.
ATM Money Dispenser Using Chain of Responsibility Design Pattern
Let's define a class to hold the amount to withdraw first. Here's the code for PaperCurrency class:
Now lets define a dispenser abstract class with two main purpose:
- Process the dispense for a specific face-value of currency note.
- Go to another dispenser for processing smaller face value of currency note.
Here's the code for PaperCurrencyDispenser abstract class:
Here's the code for HundredDispenser class:
Here's the code for FiftyDispenser class:
Here's the code for TwentyDispenser class:
Here's the code for TenDispenser class:
Now we will define the ATM withdrawal process in order to define the use of paper currency dispenser. We will arrange the paper currency dispensers in higher to lower face value order.
Here's the code for ATMWithdrawal class:
Now its time to write our Main class to execute and test the output:
Below is the output of the program:
The Source Code can be found here: Chain-of-Responsibility-Design-Pattern-Sample-Code
The Chain of Responsibility, Command, Mediator and Observer offers various ways of connecting senders and receivers of requests.
- The Chain of Responsibility pattern is used to sequentially pass a request along a dynamic chain of receivers until at least one of them handles it.
- The Command pattern is used to establish unidirectional connection between sender and receiver objects.
- The Mediator pattern is used to eliminate direct coupling between sender and receiver objects and force them to communicate indirectly via a mediator object.
- The Observer pattern allows receivers to dynamically subscribe and unsubscribe for the requests.
I hope this tutorial helps in understanding of the chain of responsibility design pattern.
Liked the article? Please don't forget to press that like button. Happy coding!
This article also conclude my series of articles on basic design patterns. Please comment to tell me how much you like the articles? Based on your response, I will come with more articles on various technologies and languages like C++, Scala, Groovy, Big Data, Database along with Java.
Need more articles, please visit my profile: Brijesh Saxena
Opinions expressed by DZone contributors are their own.