Activiti Source Code Analysis (i) design pattern

Source: Internet
Author: User

Activiti has a basic understanding of the friends know, Activiti exposed seven interfaces to provide workflow related services, how specifically these interfaces are implemented? View the source code found its implementation in the following form:

public class Runtimeserviceimpl extends Serviceimpl implements Runtimeservice {public    processinstance Startprocessinstancebykey (String processdefinitionkey) {    return Commandexecutor.execute (new Startprocessinstancecmd<processinstance> (processdefinitionkey, NULL, NULL, NULL));  }  Public ProcessInstance Startprocessinstancebykey (string processdefinitionkey, String businesskey) {    return Commandexecutor.execute (New startprocessinstancecmd<processinstance> (Processdefinitionkey, NULL, Businesskey, null));  }  ...}

Most of the methods in the service are done by calling Commandexecutor.execute (), but when you click inside, you will find nothing at all:

public class Commandexecutorimpl implements Commandexecutor {  private final commandconfig defaultconfig;  Private final commandinterceptor first;    Public Commandexecutorimpl (Commandconfig Defaultconfig, Commandinterceptor first) {    this.defaultconfig = Defaultconfig;    This.first = first;  }    Public Commandinterceptor GetFirst () {    return first;  }  @Override public  commandconfig getdefaultconfig () {    return defaultconfig;  }    @Override public  <T> T execute (command<t> command) {    return execute (defaultconfig, Command);  }  @Override public  <T> T execute (commandconfig config, command<t> Command) {    return First.execute (config, command);}  }

When you see this, you can't see what the statement actually does, so how does it work? In fact, most of the operations in Activiti are based on the command pattern in design mode (this also uses the chain of responsibility model, constructs the command interceptor chain, Used to do a series of operations before the command is actually executed). The following combined with the source code details about these design ideas:

Activiti's command pattern may be different from the command pattern I saw earlier, the essence of the command pattern is to encapsulate the command, issue commands, and perform command separation. The responsibility chain model only needs to put the request into the responsibility chain, its processing details and delivery are not to be considered. Activiti these two patterns together, forming the main way of its service implementation. Its core has only three parts: Commandexecutor (Command executor, for executing commands), Commandinterceptor (command Interceptor, used to build the Interceptor chain), command (commands themselves). These three interfaces are part of the core, and other key classes are involved, which are then explained These three classes are in Activiti-engine.jar, the Activiti implementation of the core package, the specific location is: Org.activiti.engine.impl.interceptor. The following three interfaces step through the related classes and implementation:

Three interface source code:

Public interface Command <T> {  T execute (commandcontext commandcontext);  }

  

/** * The command executor for internal usage. */public interface Commandexecutor {    /**   * @return The default {@link Commandconfig}, used if none is Provided.
   */  commandconfig getdefaultconfig ();  /**   * Execute a command with the specified {@link commandconfig}.   *  /<T> T execute (commandconfig config, command<t> Command);  /**   * Execute a command with the default {@link commandconfig}.   *  /<T> T execute (command<t> Command);  }

  

Public interface Commandinterceptor {  <T> T execute (commandconfig config, command<t> Command);   Commandinterceptor GetNext ();  void Setnext (Commandinterceptor next);}

Command interface only one Execute method, here is the specific implementation of the write command, and the Commandexecutor implementation class is given above, It contains a commandconfig and a command interceptor Commandinterceptor, and executes the Execute command method, which is actually called Commandinterceptor.execute ( Commandconfig,command). In Commandinterceptor, a set and get method is included to set next (actually the next commandinterceptor) variable. Imagine this way to find the next interceptor chain in this form, and you can pass the command down.

A simple comb: one of the standard methods of service implementation services is to invoke Commandexecutor.execute (New command ()) in a specific service (commands here are specific). Its execution step is that the command executor Commandexecutor.execute invokes the Execute method of its internal variable, commandinterceptor first (a command Interceptor) (plus the parameter commandconfig). The commandinterceptor contains a Commandinterceptor object next, which points to the next commandinterceptor, and in the Execute method of the interceptor, it only needs to complete its corresponding operation. Then execute the Next.execute (Commandconfig,command) and you can simply tell the command to the next command interceptor, Then execute Command.Execute () in the last interceptor, and invoke the final content of the command to be implemented.

It's not too sleepy to be continued.

  

Activiti Source Code Analysis (i) design pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.