• activiti命令模式与责任链模式


    来源:activiti学习(七)——命令模式和职责链模式在activiti中的应用

    activiti中很多api的调用,最终会把这个调用封装成一个命令,使用命令模式去调用。另外还会把命令放在调用链上,当调用该命令时会依次调用职责链上的每一个拦截器(Interceptor),例如日志、事务相关拦截器,然后调用指定的命令。本章先对这两种设计模式进行介绍

    设计模式

    命令模式

    命令模式其作用是为了对 “行为请求者” 和 “行为实现者” 这两者进行解耦。下图是命令模式的UML图。
    在这里插入图片描述

    其中

    • HelloCommand和ByeCommand是具体命令,
    • Receiver是命令的实际执行者。
    • Invoker是提供给客户端进行调用的类。

    Command

    public interface Command {
    	void execute();
    }
    
    • 1
    • 2
    • 3
    HelloCommand
    public class HelloCommand implements Command {
     
    	private Receiver receiver = null;
    	
    	public HelloCommand(Receiver receiver) {
    		super();
    		this.receiver = receiver;
    	}
     
    	public void execute() {
    		receiver.helloAction();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    ByeCommand
    public class ByeCommand implements Command {
     
    	private Receiver receiver = null;
    	
    	public ByeCommand(Receiver receiver) {
    		super();
    		this.receiver = receiver;
    	}
     
    	public void execute() {
    		receiver.byeAction();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    Receiver

    public class Receiver {
     
    	public void helloAction() {
    		System.out.println("hello");
    	}
    	
    	public void byeAction() {
    		System.out.println("good bye");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    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();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    职责链模式

    责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,并且沿着这条链传递请求,直到有对象处理它为止。

    下面这个职责链模式与之前认识的责任链模式有区别,之前的责任链的示例有在tomcat的FilterChain.doFilter 和 ReflectiveMethodInvocation#proceed都有,它们是将可以推动链条往下走的对象作为方法参数传给了链条中的每个节点,并且每个节点是感知不到下一个节点的,而下面这种每个节点能知道下一个节点的
    在这里插入图片描述

    AbstractHandler

    AbstractHandler 预示着每个节点都维护了下1个节点,并且在自己调用完后,触发对下1个节点的调用,类似于单向链表的存在

    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();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    ConcreteHandlerA
    public class ConcreteHandlerA extends AbstractHandler{
     
    	public void action() {
    		System.out.println("handle A");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    ConcreateHandlerB
    public class ConcreteHandlerB extends AbstractHandler{
     
    	public void action() {
    		System.out.println("handle B");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    advanced installer 做包教程
    【无标题】
    【已解决】springboot整合swagger2文档
    c++入门99题31-40
    盘点十大免费低/无代码开发软件,数字化转型看这里
    如何进行各个终端的页面适配(react项目安装插件 postcss-px-to-viewport)
    docker--知识点提炼
    JVM学习笔记
    软件需求分析一般应确定的是用户对软件的(功能需求和非功能需求 ),结构化设计是一种面向(数据流 )的设计方法
    elasticsearch的索引库操作
  • 原文地址:https://blog.csdn.net/qq_16992475/article/details/134342737