java design pattern ~ responsibility chain pattern

Responsibility chain, I feel that there are five steps for organizing work flow dynamically according to requirements, for example, to complete a thing, and the order of step 1, step 2 and step 3 can be not fixed at some time, which is in line with the scope of responsibility chain. We design these chains according to requirements, and specify their execution order by ourselves. Let's see my example below .

Objects that appear

  • Abstract responsibility
  • Specific responsibilities
  • Abstract chain
  • Concrete chain

Interpretation of objects

  • Abstract responsibility, also can be said to be an act, which stipulates the signature of such acts
  • Concrete responsibility is to realize abstract responsibility, which is the realization of each step mentioned above
  • Abstract chain, which will initialize your responsibility chain and provide the external access to call the chain, but does not specify the chain order
  • Specific chains, only responsible for specifying the order of these chains

code implementation

Image responsibility

public abstract class ChainHandler {
    private ChainHandler next;

    public abstract void execute(HandlerParameters parameters);

    public ChainHandler getNext() {
        return next;
    }

    public ChainHandler setNext(ChainHandler next) {
        this.next = next;
        return this.next;
    }

    /**
     * Chain processing method, one-way list traversal.
     *
     * @param handlerParameters
     */
    public void ProcessRequest(ChainHandler command, HandlerParameters handlerParameters) {
        if (command == null) {
            throw new IllegalArgumentException("Please use first. setCommand Method to register a command");
        }
        command.execute(handlerParameters);

        // Recursively handle the next level chain
        if (command.getNext() != null) {
            ProcessRequest(command.getNext(), handlerParameters);
        }
    }
}

Specific responsibilities

public class CreateService extends ChainHandler {
    @Override
    public void execute(HandlerParameters parameters) {
        System.out.println("establish");
    }
}

public class EditService extends ChainHandler {
    @Override
    public void execute(HandlerParameters parameters) {
        System.out.println("edit");
    }
}

public class RemoveService extends ChainHandler {
    @Override
    public void execute(HandlerParameters parameters) {
        System.out.println("delete");
    }
}

Abstract chain

/**
 * Responsibility chain process processor
 */
public abstract class WorkFlow {
  private ChainHandler command;

  public WorkFlow() {
    this.command = getChainHandler();
  }

  protected abstract  ChainHandler getChainHandler();
  /**
   * Chain processing method, one-way list traversal.
   *
   * @param handlerParameters
   */
  public void ProcessRequest(HandlerParameters handlerParameters) {
    if (command == null) {
      throw new IllegalArgumentException("Please use first. setCommand Method to register a command");
    }
    command.execute(handlerParameters);

    // Recursively handle the next level chain
    if (command.getNext() != null) {
      command = command.getNext();
      ProcessRequest(handlerParameters);
    }
  }
}

Concrete chain

/**
 * The first chain of responsibility
 */
public class WorkFlow1 extends WorkFlow {
  @Override
  protected ChainHandler getChainHandler() {
    ChainHandler chainHandler = new CreateService();
    chainHandler.setNext(new EditService())
        .setNext(new RemoveService())
        .setNext(new ReadService());
    return chainHandler;
  }

}

test

 @Test
    public void chainFlow1() {
        WorkFlow workFlow = new WorkFlow1();
        workFlow.ProcessRequest(new HandlerParameters("doing", "test"));
    }

Result

establish
 edit
 delete
 read

Tags: Java

Posted on Tue, 05 Nov 2019 15:40:40 -0500 by Distant_storm