
Java programmer expert
Today we will look at the first Java design pattern from the category of behavioural patterns – Chain of Responsibility. Design patterns in this category deal with the interaction between objects and their responsibility.
The Chain of Responsibility design pattern is a pattern that allows multiple objects to process requests without the object needing to know who its successor in the chain is. Each object in the chain has the ability to process the request, but if it doesn’t know how to process it, it passes it on to the next object in the chain.
It solves the problem of processing requests in a chain, so that instead of the request being sent to a specific object, the request passes through a chain of objects, where each object in the chain can be responsible for processing the request, but if it doesn’t process it, it passes it on to the next object in the chain. The goal is to allow multiple objects to process a request without having to specify exactly which object will process it, thus reducing the dependency between the sender and receiver of the request.
We will now write a program that uses the Chain of Responsibility design pattern to simulate the approval of employee requests in a company. The aim of this pattern is to allow multiple objects to process a request without knowing exactly which object will process it. The objects are chained together and each object in the chain decides whether it can handle the request or pass it on to the next object in the chain.
There are three types of employee in this program – Employee, Supervisor and Director, each of which implements an abstract class, RequestApprover, which contains common functionality for all employees.
RequestApprover.java
package designpatterns;
public abstract class SchvalovatelPoziadavky {
String meno;
SchvalovatelPoziadavky dalsiSchvalovatel;
private SchvalovatelPoziadavky(){
}
public SchvalovatelPoziadavky(String meno){
this.meno = meno;
}
public abstract void nastavitDalsiehoSchvalovatela(SchvalovatelPoziadavky dalsiSchvalovatel);
public void posuditPoziadavku(String poziadavka)
{
if(this.dalsiSchvalovatel != null)
this.dalsiSchvalovatel.posuditPoziadavku(poziadavka);
else
System.out.println(poziadavka + " - poziadavka bola zamietnuta!");
}
}
The RequestApprover abstract class defines common methods and contains a reference to the next approver in the chain.
Employee.java
package designpatterns;
public class Zamestnanec extends SchvalovatelPoziadavky {
public Zamestnanec() {
super("zamestnanec");
}
@Override
public void nastavitDalsiehoSchvalovatela(SchvalovatelPoziadavky dalsiSchvalovatel) {
this.dalsiSchvalovatel = dalsiSchvalovatel;
}
@Override
public void posuditPoziadavku(String poziadavka) {
if(poziadavka == "NAVSTEVA_LEKARA") {
System.out.println(poziadavka + " - " + meno + " schvalil poziadavku");
}
else {
super.posuditPoziadavku(poziadavka);
}
}
}
Supervisor.java
package designpatterns;
public class Veduci extends SchvalovatelPoziadavky {
public Veduci() {
super("veduci");
}
@Override
public void nastavitDalsiehoSchvalovatela(SchvalovatelPoziadavky dalsiSchvalovatel) {
this.dalsiSchvalovatel = dalsiSchvalovatel;
}
@Override
public void posuditPoziadavku(String poziadavka) {
if(poziadavka == "DOVOLENKA") {
System.out.println(poziadavka + " - " + meno + " schvalil poziadavku");
}
else {
super.posuditPoziadavku(poziadavka);
}
}
}
Director.java
package designpatterns;
public class Riaditel extends SchvalovatelPoziadavky {
public Riaditel() {
super("riaditel");
}
@Override
public void nastavitDalsiehoSchvalovatela(SchvalovatelPoziadavky dalsiSchvalovatel) {
this.dalsiSchvalovatel = dalsiSchvalovatel;
}
@Override
public void posuditPoziadavku(String poziadavka) {
if(poziadavka == "ZVYSENIE_PLATU") {
System.out.println(poziadavka + " - " + meno + " schvalil poziadavku");
}
else {
super.posuditPoziadavku(poziadavka);
}
}
}
Employee, Supervisor, Director are specific classes representing individual employees. Each of them can approve certain types of requests, but if they are not authorized for some of them, they will pass them on to the next approver in the chain.
Main.java
import designpatterns.Riaditel;
import designpatterns.SchvalovatelPoziadavky;
import designpatterns.Veduci;
import designpatterns.Zamestnanec;
public class Main {
public static void main(String[] args) {
// Clanky retaze
SchvalovatelPoziadavky zamestnanec = new Zamestnanec();
SchvalovatelPoziadavky veduci = new Veduci();
SchvalovatelPoziadavky riaditel = new Riaditel();
// Vytvorenie retaze zodpovednosti
zamestnanec.nastavitDalsiehoSchvalovatela(veduci);
veduci.nastavitDalsiehoSchvalovatela(riaditel);
// Posudenie poziadaviek
zamestnanec.posuditPoziadavku("NAVSTEVA_LEKARA");
zamestnanec.posuditPoziadavku("DOVOLENKA");
zamestnanec.posuditPoziadavku("ZVYSENIE_PLATU");
zamestnanec.posuditPoziadavku("FIREMNE_AUTO");
}
}
The main method is to set up a chain of responsibility, create a hierarchy of individual employees and then consider the various requests. Each person tries to approve the request. If they cannot approve it, they delegate it to the next approver in the chain.
The Chain of Responsibility design pattern is used in software design when we want to achieve a flexible way of processing requests, rather than creating a rigid link between the sender and the processor of the request and passing it to a chain of objects for processing.
We have prepared the files with the above example in the form of code that you can run directly in Java. Download the Java Chain of Responsibility code here.
If you’re a Java developer looking for work, check out our employee benefits and respond to our job offers!