activiti command mode and chain of responsibility mode

Source:Activiti Learning (7) - Application of Command Mode and Responsibility Chain Mode in Activiti

Many api calls in activiti will eventually be encapsulated into a command and called using command mode. In addition, the command will be placed on the call chain. When the command is called, each interceptor (Interceptor) on the responsibility chain will be called in turn, such as log and transaction-related interceptors, and then the specified command will be called. This chapter first introduces these two design patterns.

Design Patterns

command mode

The command mode is used to perform on both “行为请求者” and “行为实现者”. The figure below is a UML diagram of the command pattern. 解耦
Insert image description here

in

  • HelloCommand and ByeCommand are specific commands,
  • Receiver is the actual executor of the command.
  • Invoker is a class provided to the client for invocation.

Command

public interface Command {
    
    
	void execute();
}
HelloCommand
public class HelloCommand implements Command {
    
    
 
	private Receiver receiver = null;
	
	public HelloCommand(Receiver receiver) {
    
    
		super();
		this.receiver = receiver;
	}
 
	public void execute() {
    
    
		receiver.helloAction();
	}
}
ByeCommand
public class ByeCommand implements Command {
    
    
 
	private Receiver receiver = null;
	
	public ByeCommand(Receiver receiver) {
    
    
		super();
		this.receiver = receiver;
	}
 
	public void execute() {
    
    
		receiver.byeAction();
	}
}

Receiver

public class Receiver {
    
    
 
	public void helloAction() {
    
    
		System.out.println("hello");
	}
	
	public void byeAction() {
    
    
		System.out.println("good bye");
	}
}

Invoker

public class Invoker {
    
    
 
	private Command command = null;
	
	public Invoker(Command command) {
    
    
		this.command = command;
	}
	
	public void action() {
    
    
		command.execute();
	}
 
	public Command getCommand() {
    
    
		return command;
	}
 
	public void setCommand(Command command) {
    
    
		this.command = command;
	}
}

Client

public class Client {
    
    
 
	public static void main(String[] args) {
    
    
	
		// 每个command都持有1个receiver
		Receiver receiver = new Receiver();
		
		// 不同的command 都会调用receiver中的不同方法
		HelloCommand helloCommand = new HelloCommand(receiver);
		ByeCommand byeCommand = new ByeCommand(receiver);
		
		// 调用 invoker#action方法, 去触发Command#execute方法, 
		//     这样的话, 不同的command就有不同的行为,
		//     并且具体的command可以把调用交给receiver去完成, 也可以在自己内部完成(activiti属于这种)
		Invoker invoker = new Invoker(helloCommand);		
		invoker.action();
		
		invoker.setCommand(byeCommand);		
		invoker.action();
	}
}

chain of responsibility model

The Chain of Responsibility Pattern creates a chain of recipient objects for a request. Avoid coupling request senders and receivers, make it possible for multiple objects to receive requests, connect these objects into a chain, and pass the request along this chain until an object handles it.

The following chain of responsibility model is different from the previous chain of responsibility model. Examples of previous chain of responsibility include tomcat's FilterChain.doFilter and ReflectiveMethodInvocation#proceed. They pass objects that can push the chain down as method parameters. Given to each node in the chain, and each node cannot sense the next node, but each node in the following can know the next node
Insert image description here

AbstractHandler

AbstractHandler indicates that each node maintains the next node, and after calling itself, triggers the call to the next node, similar to the existence of a one-way linked list

public abstract class AbstractHandler {
    
    
 
	AbstractHandler next = null;
 
	public void setNext(AbstractHandler next) {
    
    
		this.next = next;
	}
	
	public void handle() {
    
    
		action();
		if(next != null) {
    
    
			next.handle();
		}
	}
 
	public abstract void action();
}
ConcreteHandlerA
public class ConcreteHandlerA extends AbstractHandler{
    
    
 
	public void action() {
    
    
		System.out.println("handle A");
	}
}
ConcreateHandlerB
public class ConcreteHandlerB extends AbstractHandler{
    
    
 
	public void action() {
    
    
		System.out.println("handle B");
	}
}

Client

public class Client {
    
    
	
	public static AbstractHandler initChain() {
    
    
	
		// 创建2个节点
		ConcreteHandlerA concreteHandlerA = new ConcreteHandlerA();
		ConcreteHandlerB concreteHandlerB = new ConcreteHandlerB();
		
		// A节点的下一个节点是B节点
		concreteHandlerA.setNext(concreteHandlerB);
		
		return concreteHandlerA;
	}
 
	public static void main(String[] args) {
    
    
	
		AbstractHandler handlerChain = initChain();

		// 触发A节点的调用, A调用完成之后, 触发对B节点的调用
		handlerChain.handle();
	}
}

ProcessEngines、
ProcessEngineConfiguration、
ProcessEngineConfigurationImpl、
ProcessEngineConfigurationImpl#init、
ProcessEngineConfigurationImpl#build、
SpringProcessEngineConfiguration、
CommandInterceptor、
CommandContextInterceptor、
CommandInvoker、
CommandExecutor、
CommandConfig、
CommandContext、
DbSqlSession、

ManagementService#executeCommand
commandContext#getExecutionEntityManager
ExecutionEntityManager#findChildExecutionsByProcessInstanceId
ProcessDefinitionUtil#getBpmnModel(processDefinitionId)
BpmnModel#getFlowElement(activityId)

Insert image description here

Guess you like

Origin blog.csdn.net/qq_16992475/article/details/134342737