责任链模式

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

责任链模式的通用类图

责任链模式_第1张图片

责任链模式的通用源码

  • 抽象处理者

    public abstract class Handler {
        //责任传递,下一个责任人是谁
        private Handler nextHandler;
    
        //每个处理者都必须对请求做出请求
        public final Response handlerMessage(Request request) {
            Response response = null;
            if (this.getHanderLevel().equals(request.getRequestLevel())) {
                response = this.echo(request);
            }
            else {
                if (null != this.nextHandler) {
                    this.nextHandler.handlerMessage(request);
                }
                else {
                    //没有适当的处理者,业务自行处理
                }
            }
            return response;
        }
    
        //设置下一个处理者
        public void setNext(Handler _handler) {
            this.nextHandler = _handler;
        }
    
        //每一个处理者都有一个处理等级
        protected abstract Level getHanderLevel();
        //每一个处理者都必须实现处理请求任务
        protected abstract Response echo(Request request);
    }
    
  • 具体处理者类(0)

    public class ConcreteHandler extends Handler {
    
        //定义自己处理的级别
        @Override
        protected Level getHanderLevel() {
            return null;
        }
    
        //顶一一自己处理的逻辑
        @Override
        protected Response echo(Request request) {
            return null;
        }
    }
    
  • 具体处理者类(1)

    public class ConcreteHandler1 extends Handler {
    
        //定义自己处理的级别
        @Override
        protected Level getHanderLevel() {
            return null;
        }
    
        //定义自己处理的逻辑
        @Override
        protected Response echo(Request request) {
            return null;
        }
    }
    
  • 具体处理者类(2)

    public class ConcreteHandler2 extends Handler {
    
        //定义自己处理的级别
        @Override
        protected Level getHanderLevel() {
            return null;
        }
    
        //定义自己处理的逻辑
        @Override
        protected Response echo(Request request) {
            return null;
        }
    }
    
  • 请求类

    public class Request {
        //请求的等级
        public Level getRequestLevel() {
            return null;
        }
    }
    
  • 级别类

    public class Level {
        //定义一个请求和处理等级
    }
    
  • 回应类

    public class Response {
        //处理者返回的数据
    }
    
  • 场景类

    public class Client {
    
        public static void main(String[] args) {
            Handler handler = new ConcreteHandler();
            Handler handler1 = new ConcreteHandler1();
            Handler handler2 = new ConcreteHandler2();
    
            //1->2->3
            handler.setNext(handler1);
            handler1.setNext(handler2);
    
            Response response = handler.handlerMessage(new Request());
        }
    }
    

责任链模式的优点

  • 将请求和处理分开。请求者可以不用知道是谁处理的,处理者可以不用知道请求的全貌。

责任链模式的缺点

  • 性能问题。每个请求都是从链头遍历到链尾,特别是在链长的时候,性能一定是非常大的问题。
  • 调试不便。链长,由于采用了递归的方式,调试的时候逻辑可能比较复杂。

责任链模式的应用场景

  • 用户注册时。

注:使用责任链的时候,要控制链的长度。

责任链模式的简单应用的类图及源码

责任链模式_第2张图片

  • 女性接口

    public interface IWomen {
    
        //说明女性个人的情况
        public int getType();
        //说明女性发出的请求
        public String getRequest();
    }
    
  • 具体女性类

    public class Women implements IWomen {
    
        /**
         * 说明女性的个人情况
         * 1 -- 女儿
         * 2 -- 妻子
         * 3 -- 母亲
         */
        private int type;
        //说明女性个人发出的请求
        private String request;
    
        public Women(int _type, String _request) {
            this.type = _type;
            switch (this.type) {
            case 1:
                this.request = "女儿向父亲请求是" + _request;
                break;
            case 2:
                this.request = "妻子向丈夫请求是" + _request;
                break;
            case 3:
                this.request = "母亲向儿子请求是" + _request;
                break;
            }
        }
    
        @Override
        public int getType() {
            return this.type;
        }
    
        @Override
        public String getRequest() {
            return this.request;
        }
    }
    
  • 抽象处理类

    public abstract class IHandler {
    
        public final static int FATHER_LEVEL_REQUEST = 1;
        public final static int HUSBAND_LEVEL_REQUEST = 2;
        public final static int SON_LEVEL_REQUEST = 3;
        //能处理的级别
        private int level = 0;
        //责任传递,下一个责任人是谁
        private IHandler nextHandler;
        //每一类都要说明自己能处理的哪些请求
        public IHandler(int _level) {
            this.level = _level;
        }
    
        //一个女性要求逛街,你需要处理这个请求
        public final void handlerMessage(IWomen women) {
            if (women.getType() == this.level) {
                this.response(women);
            }
            else {
                if (null != this.nextHandler) {
                    this.nextHandler.handlerMessage(women);
                }
                else {
                    System.out.println("--   不同意");
                }
            }
        }
    
        // 如果不是你的处理请求,你应该让他找下一个环节的人
        public void setNext(IHandler _handler) {
            this.nextHandler = _handler;
        }
    
        // 有请求自然要回应
        protected abstract void response(IWomen women);
    }
    
  • 具体处理类(父亲)

    public class Father extends IHandler {
    
        public Father() {
            super(IHandler.FATHER_LEVEL_REQUEST);
        }
    
        @Override
        protected void response(IWomen women) {
            System.out.println("女儿向父亲发出请求");
            System.out.println(women.getRequest());
            System.out.println("父亲 -----------> 同意");
        }
    }
    
  • 具体处理类(丈夫)

    public class Husband extends IHandler {
    
        public Husband() {
            super(HUSBAND_LEVEL_REQUEST);
        }
    
        @Override
        protected void response(IWomen women) {
            System.out.println("妻子向丈夫发出请求");
            System.out.println(women.getRequest());
            System.out.println("丈夫 -----------> 同意");
        }
    }
    
  • 具体处理类(儿子)

    public class Son extends IHandler {
    
        public Son() {
            super(SON_LEVEL_REQUEST);
        }
    
        @Override
        protected void response(IWomen women) {
            System.out.println("母亲向儿子发出请求");
            System.out.println(women.getRequest());
            System.out.println("儿子 -----------> 同意");
        }
    }
    
  • 场景类

    public class Client {
    
        public static void main(String[] args) {
    
            Random random = new Random();
            ArrayList<IWomen> list = new ArrayList<IWomen>();
            for (int i = 0; i < 5; i++) {
                list.add(new Women(random.nextInt(3) + 1, "我要逛街"));
            }
    
            IHandler father = new Father();
            IHandler husband = new Husband();
            IHandler son = new Son();
    
            father.setNext(husband);
            husband.setNext(son);
    
            for(IWomen women : list) {
                father.handlerMessage(women);
            }
        }
    
    }
    

你可能感兴趣的:(责任链模式)