Chain of Responsibility パターン
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); } } protected abstract boolean resolve(Trouble trouble); // 解決用メソッド protected void done(Trouble trouble) { // 解決 System.out.println(trouble + " is resolved by " + this + "."); } }
public class LimitSupport extends Support { private int limit; // この番号未満なら解決できる public LimitSupport(String name, int limit) { // コンストラクタ super(name); this.limit = limit; } protected boolean resolve(Trouble trouble) { // 解決用メソッド if (trouble.getNumber() < limit) { return true; } else { return false; } } }