We will summarize the ** Chain of Responsibility pattern ** in the GoF design pattern.
--The English word Chain means ** chain **, the English word Responsibility means ** responsibility **, that is, Chain of Responsibility means ** chain of responsibility **. Actually, it is easier to understand if you think of it as a structure that performs ** turning **. --The Chain of Responsibility pattern is a method that ** connects multiple objects with a chain and walks through the chains of the objects in sequence to determine the target object **. --A request comes to a person, and if that person can handle it, handle it. If it cannot be processed, the request is circulated to the "next person". Repeat after that ... This is the Chain of Responsibility pattern. --The GoF design patterns are classified as ** behavioral design patterns **.
It is a program that one of the supports will solve the trouble you entered.
This class represents the trouble that has occurred.
Trouble.java
public class Trouble {
//Trouble number
private int number;
public Trouble(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
public String toString() {
return "[Trouble " + number + "]";
}
}
An abstract class that solves problems.
Support.java
public abstract class Support {
//The name of this troubleshooter
private String name;
//Turn the tub
private Support next;
public Support(String name) {
this.name = name;
}
public Support setNext(Support next) {
this.next = next;
return next;
}
//Trouble-solving procedure
public void support(Trouble trouble) {
if (resolve(trouble)) {
done(trouble);
} else if (next != null) {
next.support(trouble);
} else {
fail(trouble);
}
}
public String toString() {
return "[" + name + "]";
}
//Resolving method
protected abstract boolean resolve(Trouble trouble);
//Solution
protected void done(Trouble trouble) {
System.out.println(trouble + " is resolved by " + this + ".");
}
//unsolved
protected void fail(Trouble trouble) {
System.out.println(trouble + " cannot be resolved.");
}
}
This is a concrete class that solves problems. Does not always process.
NoSupport.java
public class NoSupport extends Support {
public NoSupport(String name) {
super(name);
}
protected boolean resolve(Trouble trouble) {
return false;
}
}
This is a concrete class that solves problems. You can solve problems with numbers less than the specified number.
LimitSupport.java
public class LimitSupport extends Support {
private int limit;
public LimitSupport(String name, int limit) {
super(name);
this.limit = limit;
}
//Can be resolved if less than limit
protected boolean resolve(Trouble trouble) {
if (trouble.getNumber() < limit) {
return true;
} else {
return false;
}
}
}
This is a concrete class that solves problems. You can solve the trouble of odd numbers.
OddSupport.java
public class OddSupport extends Support {
public OddSupport(String name) {
super(name);
}
//Odd number problems can be solved
protected boolean resolve(Trouble trouble) {
if (trouble.getNumber() % 2 == 1) {
return true;
} else {
return false;
}
}
}
This is a concrete class that solves problems. You can solve the trouble of a specific number.
SpecialSupport.java
public class SpecialSupport extends Support {
private int number;
public SpecialSupport(String name, int number) {
super(name);
this.number = number;
}
//Can be solved if number
protected boolean resolve(Trouble trouble) {
if (trouble.getNumber() == number) {
return true;
} else {
return false;
}
}
}
This class performs the main processing.
Main.java
public class Main {
public static void main(String[] args) {
Support alice = new NoSupport("Alice");
Support bob = new LimitSupport("Bob", 100);
Support charlie = new SpecialSupport("Charlie", 429);
Support diana = new LimitSupport("Diana", 200);
Support elmo = new OddSupport("Elmo");
alice.setNext(bob).setNext(charlie).setNext(diana).setNext(elmo);
//Various troubles occur
for (int i = 0; i < 500; i += 33) {
alice.support(new Trouble(i));
}
}
}
[Trouble 0] is resolved by [Diana].
[Trouble 33] is resolved by [Diana].
[Trouble 66] is resolved by [Diana].
[Trouble 99] is resolved by [Diana].
[Trouble 132] is resolved by [Diana].
[Trouble 165] is resolved by [Diana].
[Trouble 198] is resolved by [Diana].
[Trouble 231] is resolved by [Elmo].
[Trouble 264] cannot be resolved.
[Trouble 297] is resolved by [Elmo].
[Trouble 330] cannot be resolved.
[Trouble 363] is resolved by [Elmo].
[Trouble 396] cannot be resolved.
[Trouble 429] is resolved by [Charlie].
[Trouble 462] cannot be resolved.
[Trouble 495] is resolved by [Elmo].
The point of the Chain of Responsibility pattern is that it ** loosely connects the "requester (Main)" and the "requester (Support)" **. When the "requester (Main)" makes a request to the first person, the request flows through the chain, and the request is processed by the appropriate person. Without the Chain of Responsibility pattern, someone needs to know that this request should be handled by this person. If that knowledge is given to the "requester (Main)", the independence as a part will be impaired. Please note that the ** Chain of Responsibility pattern is more flexible, but it will be slower due to the ramblings **. If processing speed is very important, you shouldn't use the Chain of Responsibility pattern. (Not to write in the merit column! W)
-** GoF design pattern summary **
This article and sample program were created based on the following books.
-** Introduction to design patterns learned in Java language **
It was very easy to understand and I learned a lot. Thank you. The detailed explanations of the design patterns and sample programs are written, so please take a look at the books as well.
Recommended Posts