责任链模式-Chain of Responsibility(Java实现), 例1

责任链模式-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));
        }
    }
}

 

posted @ 2018-05-13 23:50  GoldArowana  阅读(455)  评论(0编辑  收藏  举报