Archive for November, 2007

Cairngorm – Commands and Responders

Recently, I’ve seen a few questions around Cairngorm and how commands and responders fit together when using asynchronous services with Flex. I’d like to clarify a couple of points.

A Command need only implement the Flex IResponder interface if it is to be the responder for an asynchronous service call.

This is probably obvious to many, but I’ve seen Command classes which implement IResponder, but don’t do any ansychrnous service calls and have empty result() and fault() methods. In this cases, the Command should implement the ICommand interface only and not the IResponder interface.

The Command class does not have to be the responder for asychronous service calls.

I’ve not seen many implementations like this, but the Cairngorm command pattern is designed in such a way that your responder can be an instance of another class, as in this example:

public class AddTaskCommand implements Command
{
public function execute( event : CairngormEvent ):void
{
var addTaskEvent : AddTaskEvent = event as AddTasksEvent;
var task : Task = addTaskEvent.task;
var addTaskResponder : AddTaskResponder = new AddTaskResponder( task );
var delegate : TaskDelegate = new TaskDelegate( addTaskResponder );
delegate.addTask( task );
}
}

public class AddTaskResponder implements IResponder
{
private var task : Task;
public function AddTaskResponder( task : Task )
{
this.task = task;
}
public function result( event : Object ):void
{
//handle the result here
}
public function fault( event : Object ):void
{
//handle the fault here
}
}

This implementation allows better separation of logic and potential reuse of responders, and also makes things easier to test.