Web Application Component Toolkit

Command Pattern

A GoF Design Pattern with the intent: “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”

In WACT, this is seen with the FormController, where users can define a class which must contain the method performAction();

class AddressForm extends PostFormController {
 
    // Code omitted
 
    function InitializeActions() {
        $this->registerSubmitAction("register", "RegisterUser", PRE_VALID);
        $this->registerDefaultSubmitAction("RegisterUser", PRE_VALID);
    }
 
    // Code omitted
}

The registerSubmitAction call above points the controller at the class RegisterUser, which is where the Command pattern resides.

class RegisterUser {
 
    function performAction(&$context) {
        $DataSpace =& $context->getDataSpace();
        $Page =& new Template('/input/thanks.html');
        $Page->import($DataSpace->export());
        $Page->display();
        return FORM_COMPLETE;
    }
 
}

The FormController expects to find the performAction method in the Action Class RegisterUser, calling it when the form is submitted (and successfully validated).

The Command pattern is a Gang of Four Design Pattern, used to allow a function name to be wrapped up in a class namespace, so that some code, which will call the function, can be given an instance of one of many classes without caring which which instance it's dealing with.

A possible (somewhat stupid) PHP example might be;

<?php
/* interface */ class Tag {
   // The abstract command method
   function render($content);
}
 
class Bold {
   function render($content) {
       return "<b>$content</b>";
   }
}
 
class Italic {
   function render($content) {
       return "<i>$content</i>";
   }
}
 
$commands = array (
    new Bold(),new Italic(), new Italic(), new Bold()
);
 
// Iterate over the command objects
foreach ( $commands as $command ) {
    echo ( $command->render('Hello World!') );
}
?>

The Command pattern is used widely on in web based application frameworks like Java Struts from a PHP Perspective, usually in Action Classes, to allow framework users to define logic which responds to “events” in the application. Action Classes can be regarded as the “verbs” of the framework - they act on things - they do things.

The Command Pattern in Modern Web Applications

The idea behind the Command pattern remains useful in modern web applications. Applications still need a way to represent actions and connect user input with the code that performs a particular task. A form submission, button click, API request, or other event can all result in an action being handled by a specific part of an application.

Modern frameworks may use different terminology, but the underlying idea is often similar. Requests may be routed to controllers, handlers, actions, commands, or other application components responsible for carrying out a particular operation.

This is one of the architectural ideas that continues to shape web applications. Separating requests and actions into well-defined parts can make an application easier to understand, test, and extend as it grows.

The same principle can also be found in interactive browser-based applications. An online tool may respond to actions such as selecting a file, changing an image size, starting a conversion, or downloading a processed result. The technologies have changed, but applications still need a clear way to connect user actions with the work performed by the application.