Applications that require undo/redo functionality, can be constructed using the Command Pattern. Other uses of command pattern can be to construct reusable components, process queued requests, and develop wizard or transactional applications. As the name goes, Command pattern encapsulates functionality into a class and consist of five elements:
1. Interface
2. Concrete Command
3. Receiver
4. Client
5. Invoker
In this post I will touch upon Interface element of the command pattern. The command interface defines the execute() method, which is responsible for executing the requested operation. Also multiple command types can be implemented using the same interface. The command interface provides a programmatic approach to manage the objects which have disparate modes of operation.
//General interface implementation
package com.thepixelcode.sampleCPExample.commands {
public interface ICommand {
function execute():void;
}
}
The above snippet can be further extended to support the redo/undo functionality, and in order to implement, we would need to create two command interfaces, one for redo and another for undo which shall extend the ICommand.
//Interface for undo
package com.thepixelcode.sampleCPExample.commands {
public interface IUndoCommand extends ICommand {
function undo():void;
}
}
//Interface for redo
package com.thepixelcode.sampleCPExample.commands {
public interface IRedoCommand extends ICommand {
function redo():void;
}
}
I didn’t want to write a long post over the weekend, also neither i wanted to loose on my writing spirit. Shall continue on other elements and a demo app in next post. Till then have a great weekend.


No Comments on “Working with Command Pattern – Interface”
You can track this conversation through its atom feed.