责任链模式

责任链模式

  1. 意图
    • 通过给对象一个机会处理请求来避免将请求的发送方耦合到接收方。链接接受对象,并沿链传递请求,直到对象处理它。
  2. 解释
    案例:兽人王大声命令他的军队。最接近他并首先做出反应的是指挥官,然后是军官最后是士兵。指挥官、军官和士兵形成了一条责任链。
    简单说:它帮助构建一条对象链,一个请求从一个对象进入,并不断从一个对象进入另一个对象,直到找到合适的处理者为止。
  3. 样例
    //请求
    import java.util.Objects;
    
    public class Request {
        private final RequestType requestType;
        private final String requestDescription;
        private boolean handled;
    
        public Request(RequestType requestType, String requestDescription) {
            this.requestType = Objects.requireNonNull(requestType);
            this.requestDescription = Objects.requireNonNull(requestDescription);
        }
    
        public RequestType getRequestType() {
            return requestType;
        }
    
        public String getRequestDescription() {
            return requestDescription;
        }
    
        public void markHandled(){
            this.handled = true;
        }
    
        public boolean isHandled(){
            return this.handled;
        }
    
        @Override
        public String toString() {
            return getRequestDescription();
        }
    }
    
    
    //请求类型
    public enum RequestType {
        DEFEND_CASTLE,
        TORTURE_PRISONER,
        COLLECT_TAX;
    }
    
    
    //请求处理者
    public interface RequestHandler {
        boolean canHandleRequest(Request request);
        int getPriority();
        void handle(Request request);
        String name();
    }
    
    
    //指挥官
    public class OrcCommander implements RequestHandler{
    
        @Override
        public boolean canHandleRequest(Request request) {
            return request.getRequestType() == RequestType.DEFEND_CASTLE;
        }
    
        @Override
        public int getPriority() {
            return 2;
        }
    
        @Override
        public void handle(Request request) {
            request.markHandled();
            System.out.println("Orc Commander handled request : " + name() + " : " + request);
        }
    
        @Override
        public String name() {
            return "orc commander";
        }
    }
    
    
    //军官
    public class OrcOfficer implements RequestHandler{
        @Override
        public boolean canHandleRequest(Request request) {
            return request.getRequestType() == RequestType.COLLECT_TAX;
        }
    
        @Override
        public int getPriority() {
            return 3;
        }
    
        @Override
        public void handle(Request request) {
            request.markHandled();
            System.out.println("Orc Officer collects taxes : " + name() + " : " + request);
        }
    
        @Override
        public String name() {
            return "Orc Officer";
        }
    }
    
    
    //士兵
    public class OrcSoldier implements RequestHandler{
    
        @Override
        public boolean canHandleRequest(Request request) {
            return request.getRequestType() == RequestType.TORTURE_PRISONER;
        }
    
        @Override
        public int getPriority() {
            return 4;
        }
    
        @Override
        public void handle(Request request) {
            request.markHandled();
            System.out.println("Orc Soldier torturing prisoner : " + name() + " : " + request);
        }
    
        @Override
        public String name() {
            return "Orc Soldier";
        }
    }
    
    //兽人王
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.List;
    
    public class OrcKing{
        private List<RequestHandler> handlers;
    
        public OrcKing() {
            buildChain();
        }
    
        private void buildChain(){
            handlers = Arrays.asList(new OrcCommander(), new OrcOfficer(), new OrcSoldier());
        }
    
        public void makeRequest(Request request){
            handlers
                    .stream()
                    .sorted(Comparator.comparing(RequestHandler::getPriority))
                    .filter(handlers -> handlers.canHandleRequest(request))
                    .findFirst()
                    .ifPresent(handler -> handler.handle(request));
        }
    }
    
    //测试
    public class Main {
        public static void main(String[] args) {
            var king = new OrcKing();
            king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
            king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
            king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
        }
    }
    

    责任链模式_第1张图片

  4. 结构图

责任链模式_第2张图片

  1. 适用性
  • 不止一个对象会处理请求,处理程序是位置的。应自动确定处理程序。
  • 你希望在不显示指定接受者的情况下向多个对象中的一个发送请求。
  • 应该动态指定可以处理请求的对象集。

你可能感兴趣的:(责任链模式,设计模式,java)