Design pattern ~ Chain of Responsibility ~

1.First of all

We will summarize the ** Chain of Responsibility pattern ** in the GoF design pattern.

2. What is the Chain of Responsibility 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 **.

3. Sample class diagram

ChainofResponsibility.PNG

4. Sample program

It is a program that one of the supports will solve the trouble you entered.

4-1. Trouble class

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 + "]";
	}
}

4-2. Support class

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.");
	}
}

4-3. NoSupport class

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;
	}
}

4-4. LimitSupport class

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;
		}
	}
}

4-5. OddSupport class

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;
		}
	}
}

4-6. SpecialSupport class

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;
		}
	}
}

4-7. Main class

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));
		}
	}
}

4-8. Execution result

[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].

5. Benefits

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)

  1. GitHub

7. List of design patterns

-** GoF design pattern summary **

8. Reference

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

Design pattern ~ Chain of Responsibility ~
Chain of Responsibility Pattern
Chain Of Responsibility pattern
Java8 Lambda Expression & Stream Design Pattern Rethinking --Chain of Responsibility Pattern -
Application example of design pattern (No. 1)
Design pattern ~ Builder ~
Design pattern ~ Visitor ~
Java design pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
Design pattern ~ Command ~
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
C ++ design pattern (TemplateMethod pattern)
Design pattern ~ Factory Method ~
Design pattern ~ Abstract Factory ~
GoF design pattern summary
Design pattern ~ Template Method ~
Java design pattern summary
Which man do you like? Identifying love targets using the Chain of Responsibility pattern
[Design pattern] Java core library
[Java] Summary of design patterns
Ruby design pattern template method pattern memo
C # chewed design pattern: Template Method
Java beginner design pattern (Factory Method pattern)