Chain of Responsibility Pattern in Java
Tags: java, responsibility, pattern
Published: 2021-05-10 12:14:29

https://grokonez.com/design-pattern/chain-of-responsibility-pattern-in-java

Chain of Responsibility Pattern in Java

Chain of Responsibility Pattern is a <strong>Behavioral Pattern</strong> 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.

<!--more-->
<h2>I. OVERVIEW</h2>
Chain of Responsibility Pattern defines a set of <strong>Handlers</strong> that receive <strong>request</strong> from <strong>Sender</strong>. Each <strong>Handler</strong> processes it if possible or pass to another <strong>Handler</strong>, or does both: process and pass it on.
<img src="https://grokonez.com/wp-content/uploads/2017/03/chain-of-responsibility-overview.png" alt="chain-of-responsibility-overview-" width="699" height="400" class="alignnone size-full" />
This is a simple diagram of the Pattern:
<img src="https://grokonez.com/wp-content/uploads/2017/03/chain-of-responsibility-diagram-.png" alt="chain-of-responsibility-diagram" width="699" height="481" class="alignnone size-full" />
- We have an <strong>Abstract Handler</strong> class that contains inner <strong>Successor</strong> and <strong>handle</strong> method for processing <strong>Request</strong> and pass it to another <strong>Successor</strong>.
- All <strong>Handlers</strong> extend <strong>Abstract Handler</strong> class. Each <strong>Handler</strong> sets its own <strong>Successor</strong> (the next <strong>Handler</strong>).
- Client just gives <strong>Request</strong> for the first <strong>Handler</strong> to process, next <strong>Handler</strong> will handle <strong>Request</strong> automatically anytime it receives.
<h2>II. PRACTICE</h2>
<h3>1. Project Overview</h3>
The customer has a problem with a specific <strong>level</strong> of difficulty. We have 3 people with different standards: <strong>Regular Developer</strong>, <strong>Senior</strong> and <strong>Expert</strong>. Depending on the problem <strong>level</strong>, we choose person to give customer advice. So we pass problem (<strong>level</strong>) to <strong>Regular Developer</strong> 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.
<img src="https://grokonez.com/wp-content/uploads/2017/03/chain-of-responsibility-demo-.png" alt="chain-of-responsibility-demo" width="699" height="554" class="alignnone size-full" />
<h3>2. Step by Step</h3>
2.1- Create <code>AbstractConsultant</code> class:
<pre><code class="language-java">
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) {
			advise(level);
		} else {
			nextConsultant.giveAdvice(level);
		}
	}

	abstract protected void advise(int level);
}
</code></pre>
2.2- Create <code>ProblemLevel</code> enum:
More at:

https://grokonez.com/design-pattern/chain-of-responsibility-pattern-in-java

Chain of Responsibility Pattern in Java