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

2018-06-18 01:08:04来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

责任链模式-Chain of Responsibility

在这种模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。

咱们在这里换一个场景:

国王(King类), 经常会遇到各种麻烦(Trouble类), 而国王一旦遇到麻烦, 则会把麻烦交给手下去做.

国王有一天上了战场, 任务法派的顺序是这样的, 骑兵先上, 骑兵不行步兵上, 步兵不行就换贴身侍卫上.

TroubleType枚举

在这里枚举了, 国王在战场上有三种事情会分派给手下去做

public enum TroubleType {
    DEFEND, ATTACK, PROTECT
}  

Trouble类

这里是国王分配的事情

public class Trouble {
    private final TroubleType troubleType;
    private final String requestDescription;

    public Trouble(final TroubleType troubleType, final String requestDescription) {
        this.troubleType = Objects.requireNonNull(troubleType);
        this.requestDescription = Objects.requireNonNull(requestDescription);
    }

    public String getRequestDescription() {
        return requestDescription;
    }

    public TroubleType getRequestType() {
        return troubleType;
    }

    @Override
    public String toString() {
        return getRequestDescription();
    }
}

Support抽象类

国王的手下解决Trouble都会实现这个Support借口, Support借口是解决Trouble的统一定义

public abstract class Support {

    private Support next;

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

    public void handleRequest(Trouble trouble) {
        if (next != null) {
            next.handleRequest(trouble);
        }
    }

    protected void printHandling(Trouble trouble) {
        System.out.printf("%s 执行任务: %s\n", this, trouble);
    }

    @Override
    public abstract String toString();
}

RiderSupport类

骑兵在本例子中只负责进攻

public class RiderSupport extends Support {

    public RiderSupport(Support support) {
        super(support);
    }

    @Override
    public void handleRequest(Trouble trouble) {
        if (trouble.getRequestType().equals(TroubleType.ATTACK)) {
            printHandling(trouble);
        } else {
            super.handleRequest(trouble);
        }
    }

    @Override
    public String toString() {
        return "Rider";
    }

}

SolderSupport类

步兵在本例子中只负责防守

public class SolderSupport extends Support {

    public SolderSupport(Support support) {
        super(support);
    }

    @Override
    public void handleRequest(Trouble trouble) {
        if (trouble.getRequestType().equals(TroubleType.DEFEND)) {
            printHandling(trouble);
        } else {
            super.handleRequest(trouble);
        }
    }

    @Override
    public String toString() {
        return "Soldier";
    }
}

GuardSupport类

本类负责保卫国王

public class GuardSupport extends Support {
    public GuardSupport(Support next) {
        super(next);
    }

    @Override
    public void handleRequest(Trouble trouble) {
        if (trouble.getRequestType().equals(TroubleType.PROTECT)) {
            printHandling(trouble);
        } else {
            super.handleRequest(trouble);
        }
    }

    @Override
    public String toString() {
        return "Guard";
    }
}

King类

本类负责维护责任链.

public class King {
    private Support chain;

    public King() {
        buildChain();
    }

    private void buildChain() {
        chain = new RiderSupport(new SolderSupport(new GuardSupport(null)));
    }

    public void resolve(Trouble trouble) {
        chain.handleRequest(trouble);
    }
}

Main

运行测试

public class Main {
    public static void main(String[] args) {
        King king = new King();
        king.resolve(new Trouble(TroubleType.DEFEND, "防御"));
        king.resolve(new Trouble(TroubleType.ATTACK, "进攻"));
        king.resolve(new Trouble(TroubleType.PROTECT, "保护国王"));
    }
}

  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Unable to register MBean [HikariDataSource (HikariPool-0)] w

下一篇:整理一下面试经常被问到的关于集合的问题