[GO] Chain of Responsibility pattern in Java

Introduction

Introducing the design patterns of [GoF](https://ja.wikipedia.org/wiki/Gang of for _ (Information Engineering)) ["Introduction to Design Patterns Learned in the Augmented and Revised Java Language"]( https://www.amazon.co.jp/ Augmented and revised edition Introduction to design patterns learned in Java language-Hiroshi Yuki / dp / 4797327030 / ref = pd_lpo_sbs_14_t_0? _ Encoding = UTF8 & psc = 1 & refRID = 2ZE4GPYNN55JGDR5QMHP) I will summarize about.

Chain of Responsibility pattern

What is Chain of Responsibility?

Translated into Japanese, it means "chain of responsibility". ** A pattern that connects multiple objects like a chain and walks through each object in order to determine the target object ** is called the ** Chain of Responsibility pattern **. As an image, when trying to submit work documents, the submission destination is changed to Mr. A of the Human Resources Department → Mr. B of the Accounting Department → Mr. C of the General Affairs Department ** I think it's easy to understand. By applying this pattern, it is possible to weaken the connection between the "requesting side" and the "performing side", so each can be made into parts. On the contrary, if this pattern is not used, the "requester of processing" must have centralized information that this processing needs to be requested from this object, and if this is done, the independence as a part will be lost. It will be difficult to use.

Character

The Chain of Responsibility pattern is used by the classes that appear in the class diagram below. image.png

Abstract class

Implementation class

Concrete example

As a concrete example, it will be explained based on the following class. image.png

Other classes

-** Trouble class **

Trouble.java


public class Trouble {

	private int number; //Trouble number
	private String content; //Trouble content
	private int level; //Trouble level

	public Trouble(int number, String content, int level) { //Generate constructor trouble
		this.number = number;
		this.content = content;
		this.level = level;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int revel) {
		this.level = revel;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public int getNumber() {
		return number;
	}

	public String toString() { //Trouble string representation
		return "[#" + number + " " + content + "(" + level + ")" + "]";
	}

}

The Trouble class is a class that represents the trouble that has occurred. It has a trouble number, trouble content, and trouble level in the field. There is nothing particularly difficult.

Abstract class

-** Responsible class **

Responsible.java


public abstract class Responsible {

	private String name; //The name of this troubleshooter
	private Responsible next; //The tip of the tub

	public Responsible(String name) { //Generate constructor trouble solver
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public Responsible setNext(Responsible next) { //Set the tip of the tub
		this.next = next;
		return next;
	}

	public void support(Trouble trouble) { //Trouble-solving procedure
		if (resolve(trouble)) {//If the trouble can be solved
			done(trouble);
		} else if (next != null) {//When the trouble could not be solved but the destination of the next turn is set
			next.support(trouble);
		} else {//When the trouble cannot be solved and the destination of the next turn is not set
			fail(trouble);
		}
	}

	protected abstract boolean resolve(Trouble trouble); //Resolving method

	protected void done(Trouble trouble) { //Solution
		System.out.println(trouble + "Is" + getName() + "Has been resolved.");
	}

	protected void fail(Trouble trouble) { //unsolved
		System.out.println("【warning】" + trouble + "Could not be solved by anyone.");
	}

}

The Responsible class is a class for creating a chain that solves problems. The points are the following four points.

** 1. ** Have a destination in the next field for the next turn. ** 2. ** Set and return the destination to be turned around with the setNext method. ** 3. ** The support method defines the procedure for troubleshooting. ** 4. ** The resolve method is defined as an abstract method and implemented in a subclass.

I will supplement points 1, 2 and 3. Regarding point 1, the next set destination is the subclass (implementation class) of the Responsible class, so subclass A of Responsible → subclass B of Responsible → subclass C of Responsible ... You can create a chain of responsibilities like this. Next, regarding point 2, set the destination to turn around with the setNext method. Since the destination set at this time is returned, it is possible to execute methods such as hoge.setNext (foo) .setNext (bar) ..... Finally, regarding point 3, the support method has a problem to be solved as an argument, the abstract method resolve is called, and if the return value is true (solved), the problem is solved by the done method. Shows what you have done. If the return value is false (unresolved) and the next tying destination is set, call the support method of the next tying destination to entrust the solution of the trouble to the next tying destination. I will. However, if the return value is false (unresolved) and the next traverse destination is not set, it means that it is the end of the chain and none of the objects could be processed, so fail Displays that the method could not resolve the problem.

Implementation class

-** Employee class **

Employee.java


public class Employee extends Responsible {

	private int limitLevel = 1; //It can be solved if it is below this level

	public Employee(String name) { //constructor
		super(name);
	}

	@Override
	protected boolean resolve(Trouble trouble) { //Resolving method
		if (trouble.getLevel() <= limitLevel) {
			return true;
		} else {
			return false;
		}
	}

}

-** Chief class **

Chief.java


public class Chief extends Responsible {

	private int limitLevel = 5; //It can be solved if it is below this level

	public Chief(String name) { //constructor
		super(name);
	}

	@Override
	protected boolean resolve(Trouble trouble) { //Resolving method
		if (trouble.getLevel() <= limitLevel) {
			return true;
		} else {
			return false;
		}
	}

}

-** Manager class **

Manager.java


public class Manager extends Responsible {

	private int limitLevel = 10; //It can be solved if it is below this level

	public Manager(String name) { //constructor
		super(name);
	}

	@Override
	protected boolean resolve(Trouble trouble) { //Resolving method
		if (trouble.getLevel() <= limitLevel) {
			return true;
		} else {
			return false;
		}
	}

}

-** President class **

President.java


public class President extends Responsible {

	private int limitLevel = 20; //It can be solved if it is below this level

	public President(String name) { //constructor
		super(name);
	}

	@Override
	protected boolean resolve(Trouble trouble) { //Resolving method
		if (trouble.getLevel() <= limitLevel) {
			return true;
		} else {
			return false;
		}
	}

}

The ʻEmployeeclass,Chief class, Managerclass, andPresident class are implementation classes of the Responsibleclass. Each has an upper limit of the trouble level that can be solved by itself, and theresolve` method of the abstract method is overridden so that troubles within the range that does not exceed this upper limit can be solved.

Execution class

-** Main class **

Main.java


public class Main {
	
	public static void main(String[] args) {

		//Creating a troubleshooter
		Responsible employee = new Employee("A employee");
		Responsible chief = new Chief("Chief B");
		Responsible manager = new Manager("Director C");
		Responsible president = new President("President D");

		//Chain formation
		employee.setNext(chief).setNext(manager).setNext(president);

		//Various troubles occur
		employee.support(new Trouble(1, "Dealing with customer complaints", 1));
		employee.support(new Trouble(2, "Support for overseas business trips", 10));
		employee.support(new Trouble(3, "world peace", 100));
		employee.support(new Trouble(4, "Estimate creation support", 5));
		employee.support(new Trouble(5, "Management policy formulation", 20));

	}
	
}

First, create a troubleshooter. Next, the chain is formed by calling the setNext method for each object. Finally, create the trouble you want to solve and pass it as an argument to the support method of the ʻemployee` object.

Execution result

The result of executing Main.java is as follows. You can see that we are dealing within the trouble level of each object. In addition, there is an output that the trouble that no one could handle could not be solved.

Execution result


[#1 Customer complaint handling(1)]Was resolved by A employee.
[#2 Support for overseas business trips(10)]Was resolved by Director C.
【warning】[#3 World peace(100)]Could not be solved by anyone.
[#4 Support for quotation(5)]Was resolved by Chief B.
[#5 Management policy formulation(20)]Was resolved by President D.

merit

The advantages of the Chain of Responsibility pattern are as follows. ** 1. ** By weakening the connection between the requesting side and the processing side, the independence of processing (partification) is enhanced. ** 2. ** Since only the processing within the scope of its own responsibility is implemented in the class on the processing side, it can be described concisely.

Demerit

On the contrary, the disadvantages of the Chain of Responsibility pattern are as follows. ** 1. ** Processing is slow because the chain needs to be traced from scratch. → If the relationship between request and processing is fixed and processing speed is important, this pattern should not be applied.

Summary

You've learned about the Chain of Responsibility pattern, which determines which objects to process by turning responsibility around. The sample code is uploaded below, so please refer to it if you like.

-Chain of Responsibility sample code

In addition, other design patterns are summarized below, so please refer to them as well.

-[Updated from time to time] Summary of design patterns in Java

References

-[Introduction to Design Patterns Learned in the Augmented and Revised Java Language](https://www.amazon.co.jp/ Introduction to Design Patterns Learned in the Augmented and Revised Java Language-Hiroshi Yuki / dp / 4797327030 / ref = pd_lpo_sbs_14_t_0? _Encoding = UTF8 & psc = 1 & refRID = 2ZE4GPYNN55JGDR5QMHP)

Recommended Posts

Chain of Responsibility pattern in Java
Learn the design pattern "Chain of Responsibility" in Python
Facade pattern in Java
Singleton pattern in Java
Flyweight pattern in Java
Iterator pattern in Java
Prototype pattern in Java
Proxy pattern in Java
[Gang of Four] Design pattern learning --Chain of Responsibility
Template Method pattern in Java
Pattern recognition learning in video Part 1 Field of Pattern Recognition
Singleton pattern in Python
Linux permissions in Java
I studied about design patterns (personal memo) Part 6 (Chain of Responsibility pattern, Facade pattern, Mediator pattern)
Use DataFrame in Java
Check for the existence of BigQuery tables in Java
Summary of Prototype patterns introductory design patterns learned in Java language
Measure the execution result of the program in C ++, Java, Python.
The result of Java engineers learning machine learning in Python www
Sample source of Observer pattern realized by Java, PHP, Python
Summary of Singleton patterns introductory design patterns learned in Java language
Partial in case of trouble
List of nodes in diagrams
Equivalence of objects in Python
Implement method chain in Python
Implementation of quicksort in Python
How to implement Java code in the background of RedHat (LinuxONE)
Chapter 4 Summary of Introduction to Design Patterns Learned in Java Language
Summary of Chapter 3 of Introduction to Design Patterns Learned in Java Language