Follow Me

Archives

Blog Statistics

Total Visitors: 15863
Currently Online: 0

#Command design pattern in #actionscript(#as3)

The command pattern is a design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. 

This information includes the method name, the object that owns the method and values for the method parameters.

Three terms always associated with the command pattern are clientinvoker and receiver.

The client instantiates the command object and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is an instance of the class that contains the method's code.

Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters.

The example app uses one single command class to generate 3 canvas DisplayObjects to the stage.

 

In the DesignPatternCommand.mxml file im using the CreationComplete event to kick of the Command sequence.

 

  1. private function onCreationComplete():void
  2. {
  3.         var commandCanvas:Command=new Command();
  4.         commandCanvas.execute(Command.RED, this);
  5.         commandCanvas=new Command();
  6.         commandCanvas.execute(Command.GREEN, this);
  7.         commandCanvas=new Command();
  8.         commandCanvas.execute(Command.BLUE, this);
  9. }
  10.  

Since its good practice in OOP i also created a interface to program the Command class.

ICommand.as

  1. import flash.display.DisplayObjectContainer;
  2.  
  3. /**
  4.  * The interface of command implementation
  5.  * @author Geirr Winnem
  6.  */
  7. public interface ICommand
  8. {
  9.         /**
  10.          * Execute the <code>ICommand</code> logic.
  11.          * @param canvasColor the canvas color to generate
  12.          * @param stage DisplayObjectContainer which the canvas is added to.
  13.          */
  14.         function execute(canvasColor:String, stage: DisplayObjectContainer):void;
  15. }
  16.  

 

Finally we have the command class itself which implements the ICommand interface

  1. import flash.display.DisplayObjectContainer;
  2.  
  3. import mx.containers.Canvas;
  4.  
  5. /**
  6.  * Class implementing the Command pattern
  7.  * @author Geirr Winnem
  8.  */
  9. public class Command extends Canvas implements ICommand
  10. {
  11.         /**
  12.          * Constant value for a red canvas
  13.          */
  14.         public static const RED:String="redCanvas";
  15.         /**
  16.          * Constant value for a green canvas
  17.          */
  18.         public static const GREEN:String="greenCanvas";
  19.         /**
  20.          * Constant value for a blue canvas
  21.          */
  22.         public static const BLUE:String="blueCanvas";
  23.  
  24.         /**
  25.          * Constructor
  26.          */
  27.         public function Command()
  28.         {
  29.                 super();
  30.         }
  31.  
  32.         /**
  33.          * Execute the <code>ICommand</code> logic.
  34.          * @param canvasColor the canvas color to generate
  35.          * @param stage DisplayObjectContainer which the canvas is added to.
  36.          */
  37.         public function execute(canvasColor:String, mainStage:DisplayObjectContainer):void
  38.         {
  39.                 switch (canvasColor)
  40.                 {
  41.                         case RED:
  42.                                 super.setStyle("backgroundColor", 0xff0000);
  43.                                 break;
  44.                         case GREEN:
  45.                                 super.setStyle("backgroundColor", 0×00ff00);
  46.                                 break;
  47.                         case BLUE:
  48.                                 super.setStyle("backgroundColor", 0×0000ff);
  49.                                 break;
  50.                 }
  51.                 this.width=100;
  52.                 this.height=100;
  53.                 mainStage.addChild(this);
  54.         }
  55. }
  56.  

 

Example App..

 

DesignPatternCommand flex project

3 comments to #Actionscript(#as3) #Command #pattern with source code

  • Social comments and analytics for this post…

    This post was mentioned on Twitter by gwinnem: New blog posting, #Command design pattern in #actionscript(#as3) with source code – http://tinyurl.com/ycz954y…

  • I totally agree with you, and that is the correct implementation for the pattern. Just though i would keep it simple for the as3 example. I would never implement it without IUndoable in a real world scenario.

  • I find the command pattern more useful when the ICommand interface has an undo method (or extends IUndoable, for instance).   Then you can execute your commands and add them to an undo manager.  Each command just needs to know how to reverse the change it made.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>