How to use commands


The use of Command objects is the recommended way to control the user interface and trigger business logic methods. It avoids tight coupling of ui and business objects, and lets you call one and the same functionality from different places without code redundancy or referential dependencies between ui objects. Also, commands support queueing and undoable operations.


There are three steps to successfully commanding:

  1. Implement a Command subclass
  2. Instantiate, initialize and register a Command object
  3. Process the command

Let's look at it in full detail:

  1. A command extends the abstract class com.w4t.Command. A simple example implementation would look like this:

          public class MyCommand extends Command { 
            public void process() {} 
            public void process( Object[] params ) {
              String msg =   "Hi, I got "
                           + String.valueOf( params.length )
                           + " parameters.";
              W4TContext.addMessage( new Message( msg ) );
            public void undo() {} 

    Commands have a parameterless process() method and one which takes an array of Objects as parameter. It is up to you which of them you implement (and call).

  2. Commands must be registered in the command repository, which is a singleton that has exactly one instance in every user session.

    The command repository is available from every piece of code within the application. It can be accessed for adding and removing commands by use of the static methods of the class com.w4t.W4TContext. For the command repository, a unique identifier is used as name, under which a command instance is known within the repository.

          MyCommand myCommand = new MyCommand();
          W4TContext.addCommand( "myCommand", myCommand );

    Command names should be managed centrally as public String constants.

    If commands need references to objects for their processing, a constructor can be implemented and the references may be passed at construction time.

  3. Once a command instance has been added to the repository, it can be requested from any other part of the application with the W4TContext's findCommand() method.

          webButton1.addWebActionListener( new WebActionListener() { 
            public void webActionPerformed( WebActionEvent e ) {
              Command command = W4TContext.findCommand( "myCommand" );
          } );


To learn more about Design patterns (like Singleton and Command) see:

An excursus

A word about static access to the command repository. You may have rightly noted that you can to the repository by calling the methods W4TContext.addCommand() and W4TContext.findCommand(), which are static methods. This means they are surely accessing some static data structure. If you are familiar with some of the deeper aspects of distributed applications (and a web application usually is one), you are probably aware of a problem that must be taken into account with that kind of applications: the possible interference between different user sessions which means usually that you should not use static variables in a web application.

This restriction does not apply to W4 Toolkit applications, however. The library guarantees that every user session is in its own namespace, with its own static values. Therefore, the repository singletons of each session are different instances, and thus it is safe to use static variables and methods. This means also that you are free to implement your own singleton objects, which will also be singletons-per-session.

Copyright (c) 2003 by Innoopract Informationssysteme GmbH. All rights reserved.