欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-25 18:23:14
责任链模式, Chain of Responsibility, Java实现 ......

责任链模式-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, "保护国王"));
    }
}