Chain Of Responsibility pattern

What is the Chain of Responsibility pattern?

Connect multiple objects like a chain, How to determine the object of interest by walking through the chain of objects in sequence. If the request can be processed, it will be processed, and if it cannot be processed, it will be processed. Next, turn around. The pattern is that the next one will continue to work.

The role of Handler

A role that defines the interface for processing requests. Keep the next person, and if you get a request that you can't handle, send it to that person. The next person also plays the role of Handler.

package chainOfResponsibility;

public abstract class Support {
	private String name;
	private Support next;

	public Support(String name) {
		this.name = name;
	}

	public Support setNext(Support next) {
		this.next = next;
		return next;
	}

	public final void support(Trouble trouble) {
		if (resolve(trouble)) {
			done(trouble);
		} else if (next != null) {
			next.support(trouble);
		} else {
			fail(trouble);
		}
	}

	@Override
	public String toString() {
		return "[" + name + "]";
	}

	protected abstract boolean resolve(Trouble trouble);

	protected void done(Trouble trouble) {
		System.out.println(trouble + "is resolved by " + this + ".");
	}

	protected void fail(Trouble trouble) {
		System.out.println(trouble + " cannot be resolved.");
	}
}

The role of ConcreteHandler

The specific role of processing the request.

package chainOfResponsibility;

public class NoSupport extends Support{

	public NoSupport(String name) {
		super(name);
	}

	@Override
	protected boolean resolve(Trouble trouble) {
		return false;
	}
}
package chainOfResponsibility;

public class LimitSupport extends Support{
	private int limit;

	public LimitSupport(String name, int limit) {
		super(name);
		this.limit = limit;
	}

	@Override
	protected boolean resolve(Trouble trouble) {
		return trouble.getNumber() < limit;
	}
}
package chainOfResponsibility;

public class SpecialSupport extends Support{
	private int number;

	public SpecialSupport(String name, int number) {
		super(name);
		this.number = number;
	}

	@Override
	protected boolean resolve(Trouble trouble) {
		return trouble.getNumber() == number;
	}

}
package chainOfResponsibility;

public class OddSupport extends Support{

	public OddSupport(String name) {
		super(name);
	}

	@Override
	protected boolean resolve(Trouble trouble) {
		return trouble.getNumber() % 2 == 0;
	}
}

The role of Client

The role of making a request to the first ConcreteHandler role.

package chainOfResponsibility;

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");
		Support fred = new LimitSupport("Fred", 300);
		//Chain formation
		alice.setNext(bob).setNext(charlie).setNext(diana).setNext(elmo).setNext(fred);
		//Various troubles occur
		for (int i = 0; i < 500; i += 33) {
			alice.support(new Trouble(i));
		}
	}
}

Class that represents trouble

package chainOfResponsibility;

public class Trouble {
	private int number;

	 public Trouble(int number) {
		 this.number = number;
	}

	 public int getNumber() {
		 return number;
	 }

	 @Override
	 public String toString() {
		 return "[Trouble " + number + "]";
	 }
}

Execution result

スクリーンショット 2020-09-17 18.22.06.png

https://github.com/aki0207/chainOfResponsibility

I used this as a reference. Augmented and Revised Introduction to Design Patterns Learned in Java Language

Recommended Posts

Chain of Responsibility Pattern
Chain Of Responsibility pattern
Design pattern ~ Chain of Responsibility ~
Java8 Lambda Expression & Stream Design Pattern Rethinking --Chain of Responsibility Pattern -
Which man do you like? Identifying love targets using the Chain of Responsibility pattern
Application example of design pattern (No. 1)