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:

package com.javasampleapproach.chainofresponsibility.pattern;

public abstract class AbstractConsultant {

	protected int level;

	protected AbstractConsultant nextConsultant;

	public void setNextConsultant(AbstractConsultant nextConsultant) {
		this.nextConsultant = nextConsultant;

	public void giveAdvice(int level) {

		if (this.level >= level) {
		} else {

	abstract protected void advise(int level);

2.2- Create ProblemLevel enum:

package com.javasampleapproach.chainofresponsibility.pattern;

public enum ProblemLevel {
	private int value;

	private ProblemLevel(int value) {
		this.value = value;

	public int value() {
		return value;

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.

package com.javasampleapproach.chainofresponsibility.pattern;

public class RegularDeveloper extends AbstractConsultant {

	public RegularDeveloper() {
		this.level = 2;
	protected void advise(int level) {
		System.out.println("RegularDeveloper helps to solve problem level " + level);

package com.javasampleapproach.chainofresponsibility.pattern;

public class Senior extends AbstractConsultant {

	public Senior() {
		this.level = 5;
	protected void advise(int level) {
		System.out.println("Senior helps to solve problem level " + level);

package com.javasampleapproach.chainofresponsibility.pattern;

public class Expert extends AbstractConsultant {

	public Expert() {
		this.level = 7;

	protected void advise(int level) {
		System.out.println("Expert helps to solve problem level " + level);

2.4- Create Client Test Class:

package com.javasampleapproach.chainofresponsibility;

import com.javasampleapproach.chainofresponsibility.pattern.AbstractConsultant;
import com.javasampleapproach.chainofresponsibility.pattern.Expert;
import com.javasampleapproach.chainofresponsibility.pattern.ProblemLevel;
import com.javasampleapproach.chainofresponsibility.pattern.RegularDeveloper;
import com.javasampleapproach.chainofresponsibility.pattern.Senior;

public class MainApp {

	public static void main(String[] args) {
		AbstractConsultant developer = new RegularDeveloper();
		AbstractConsultant senior = new Senior();
		AbstractConsultant expert = new Expert();
		System.out.println("--- ProblemLevel = NORMAL ---");
		ProblemLevel difficultLevel = ProblemLevel.NORMAL;
		System.out.println("--- ProblemLevel = DIFFICULT ---");
		difficultLevel = ProblemLevel.DIFFICULT;
		System.out.println("--- ProblemLevel = INSANE ---");
		difficultLevel = ProblemLevel.INSANE;

2.5- Run the code, the console window shows:

--- ProblemLevel = NORMAL ---
RegularDeveloper helps to solve problem level 2
--- ProblemLevel = DIFFICULT ---
Senior helps to solve problem level 4
--- ProblemLevel = INSANE ---
Expert helps to solve problem level 7



One thought on “Chain of Responsibility Pattern in Java”

Leave a Reply

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