1. 程式人生 > >責任鏈模式-Chain of Responsibility(Java實現), 例1

責任鏈模式-Chain of Responsibility(Java實現), 例1

統一 null 不能 測試 -c 另一個 nds boolean fail

責任鏈模式-Chain of Responsibility, 例1

在這種模式中,通常每個接收者都包含對另一個接收者的引用。如果一個對象不能處理該請求,那麽它會把相同的請求傳給下一個接收者,依此類推。

Trouble類

本類是:待責任鏈來處理的問題Trouble類.

本例子較簡單, Trouble只有一個int型作為待處理的編號.

public class Trouble {
    private int number;

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

    public int getNumber() {
        return number;
    }

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

Support抽象類

Support類是責任鏈中的節點抽象出來的統一定義.

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

    protected abstract boolean solve(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.");
    }

    public void execute(Trouble trouble) {
        if (solve(trouble)) {
            done(trouble);
        } else if (next != null) {
            next.execute(trouble);
        } else {
            fail(trouble);
        }
    }

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

ZeroSupport類

本類專門用於處理trouble編號為0的

/**
 * 本類專門用於處理trouble為0的情況
 */
public class ZeroSupport extends Support {
    public ZeroSupport(String name) {
        super(name);
    }

    @Override
    protected boolean solve(Trouble trouble) {
        if (trouble.getNumber() == 0) {
            return true;
        } else {
            return false;
        }
    }
}

LimitSupport類

只要Trouble的編號小於本類的成員變量limit的值, 那麽LimitSupport類的實例就可以處理這個Trouble

/**
 * 本類專門用於處理trouble值小於limit的情況
 */
public class LimitSupport extends Support {
    private int limit;

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

    @Override
    protected boolean solve(Trouble trouble) {
        if (trouble.getNumber() < limit) {
            return true;
        } else {
            return false;
        }
    }
}

OddSupport類

只要Trouble的編號是奇數, 那麽OddSupport類的實例就可以處理這個Trouble.

/**
 * 本類專門用於處理trouble為奇數的情況
 */
public class OddSupport extends Support {

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

    @Override
    protected boolean solve(Trouble trouble) {
        if (trouble.getNumber() % 2 == 1) {
            return true;
        } else {
            return false;
        }
    }
}

Main

用於測試運行

public class Main {
    public static void main(String[] args) {
        Support charlie = new ZeroSupport("Charlie");
        Support bob = new LimitSupport("Bob", 100);
        Support diana = new LimitSupport("Diana", 200);
        Support elmo = new OddSupport("Elmo");
        Support fred = new LimitSupport("Fred", 300);

        // 形成職責鏈
        charlie.setNext(bob).setNext(diana).setNext(elmo).setNext(fred);

        // 制造各種問題, 並讓這個責任鏈來處理
        for (int i = 0; i < 500; i += 33) {
            charlie.execute(new Trouble(i));
        }
    }
}

責任鏈模式-Chain of Responsibility(Java實現), 例1