Morning Joe: Using the command pattern in Spring

So, that ArrayList of Strings that you’ve been using to store activities and with their respective run commands. There is a design pattern that can better ground it. Spring has the ability to implement the Command Pattern extremely well.

This pattern offers guidance on the storage and use of commands at a later time. Utilizing this pattern in a stable and strong way will ensure the survival of long running code. It is always a good practice to develop around established patterns.

In this pattern, the client keeps a list of commands, I personally make a command default level class with a specific name and sequence number and place the list in another default class, which passes commands to an invoker that, in turn, runs them. The base UML is available below. You may need to click on it to see it better (that is a WordPress theme issue).


Obviously, the Object class and MainApp class needs to be flushed out and given an appropriate name. The Invoker class has been given an invoke command which will take in the command, access its sequence information and check to see if it is in order, run the command, and increment the counter. The counter integer and inOrder method are there to warn a user that something is amiss. Commands should be an ArrayList of Commands unless the number of Commands is set. A sort is included to make the pattern work better with sloppy users that accesses the ArrayList. Everything is under the same package in this design. The state in the object class helps in tracking an object if the programmer decides to reuse the class or create a static function that must be called again,especially in multithreaded environments.

Creating a series of commands to be run can be done through a configuration file or a user interface. Java interface classes would help to ensure that every class has a standard way of being accessed as needed. Personally, I like to include a run instance method which also allows for code to be more easily be reconfigured for a multi-threaded environment.

The interface may look like:

public interface CommandPattern{
       public void run();

In addition to storing commands, if a command is to be run directly after initialization of a class, it is probably better to use Java EE’s post construct annotation. The annotation signals that a class should be run directly after the program and beans initialize. Just write the annotation @PostConstruct on top of a method signature.

Finally, another potential benefit of Spring is that storing and using properties as needed is extremely useful. The configuration file already contains much of the needed code. Of course, it is possible to specify when these instances are created. The Spring framework contains eager instantiation for when an object may need to be called by a user or used in a concurrent environment. Both are controlled through the lazy-init attribute.

Simple,easy, hopefully starts getting some grounding for solid command creation in Spring programs. Cheers!