Chain of Responsibility Pattern in Java

Chain of Responsibility Pattern is a Behavioral Pattern in which, a Request is sent through a chain of handling Objects without knowing which Object will handle it. With Chain of Responsibility Pattern, we can avoid coupling the Sender of a request to received Objects and create a processing sequence automatically based on type of request.


Chain of Responsibility Pattern defines a set of Handlers that receive request from Sender. Each Handler processes it if possible or pass to another Handler, or does both: process and pass it on.
This is a simple diagram of the Pattern:
– We have an Abstract Handler class that contains inner Successor and handle method for processing Request and pass it to another Successor.
– All Handlers extend Abstract Handler class. Each Handler sets its own Successor (the next Handler).
– Client just gives Request for the first Handler to process, next Handler will handle Request automatically anytime it receives.

1. Project Overview

The customer has a problem with a specific level of difficulty. We have 3 people with different standards: Regular Developer, Senior and Expert. Depending on the problem level, we choose person to give customer advice. So we pass problem (level) to Regular Developer first, if he is not qualified enough to solve the problem (his level is not equal problem level), he will pass the problem to people who has higher standard, and so on.

2. Step by Step

2.1- Create AbstractConsultant class:

2.2- Create ProblemLevel enum:

2.3- Create some Handlers class that extend AbstractConsultant class:
RegularDeveloper can give advice for problem level up to 2, Senior is up to 5, and Expert is up to 7.

2.4- Create Client Test Class:

2.5- Run the code, the console window shows:



Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *