define

Avoid coupling between the sender of the request and the receiver of the request by giving multiple objects a chance to process the request, chain the objects together, and pass the request along the chain until one object processes it.

nature

Separate responsibilities, dynamic combination.

His role

  • Handler (Handler)

    The handler role knows who the next handler is, and if he can’t handle the request himself, he passes the request to the next handler.

  • ConcreteHandler ConcreteHandler ConcreteHandler

    The specific role that handles the request.

  • Client (requestor)

    The role that sends the request to the first handler.

The sample code

Public abstract class Leader {protected Leader nextLeader; * @return */ public abstract int limit(); Public abstract void handler(int money); /** * @param */ public abstract void handler(int money); Public final void handlerRequest(int money){if(money < limit()){// if(money < limit()); Processing handler (money); }else{// Pass to next handler if(null! = nextLeader){ nextLeader.handlerRequest(money); }}}} /** * handler */ public class GroupLeader extends Leader{@override public int limit() {return 1000; } @override public void handler(int money) {system.out.println (" + money + "); } } /** * */ public class Director extends Leader{ @Override public int limit() { return 5000; } @override public void handler(int money) {system.out.println (" + money + "); } } public class Manager extends Leader{ @Override public int limit() { return 10000; } @override public void handler(int money) {system.out.println (" + money + "); } } public class Boss extends Leader{ @Override public int limit() { return Integer.MAX_VALUE; } @override public void handler(int money) {system.out.println (" + money + "); } } public class Main { public static void main(String[] args){ GroupLeader groupLeader = new GroupLeader(); Director director = new Director(); Manager manager = new Manager(); Boss boss = new Boss(); NextLeader = director; // Set the upper leader object groupleader.nextleader = director; director.nextLeader = manager; manager.nextLeader = boss; / / a reimbursement request groupLeader. HandlerRequest (50000); }}Copy the code

The results

The boss approved reimbursement of 50,000 yuanCopy the code

function

The chain of responsibility pattern is used to handle situations where a client makes a request, multiple objects have a chance to process the request, but the client does not know who will handle the request. This means that the requester and receiver need to be decouple so that the receiver can be switched and combined dynamically.

advantages

  • The requester and receiver are loosely coupled.
  • Dynamic composition of responsibilities. Functional care can be divided into individual responsibility objects and then, when used, dynamically combine responsibilities to form a chain of responsibilities, allowing for flexible assignment of responsibilities to objects and flexible implementation or change of responsibilities to objects.

disadvantages

  • Produces a lot of fine-grained objects.
  • May not be able to be processed.

When to use

  • Multiple objects can handle the same request, but that object processing is handled dynamically at run time.
  • Submit a request to one of multiple objects without the request handler being clear.
  • You need to dynamically specify a set of objects to handle requests.