Sunday, December 26, 2010

Command Design Pattern using flash AS3 .

please like and post your questions : http://www.facebook.com/pages/FlashCS5-Tutorials/176320862386436

 in this Tutorial you will understand what is the fundimental of command patterns, definition, and how can you implement it in flash AS3.


Fundametntals : 

  • command patterns fall in to behavioral categories which help up to change the behavioral of project without altering the same structure of your project.
  • Command Patterns allows a request or action to exist as an object as we use it in manycases to save an action in a class .

let suppose that we have menu in flash with the folloing actions :


* File  * Execute  * Exit


so usually when we want to write a code to execute some actions  we usually writing a methode with if conditions:


private function Execute(string strAction)
{
if(strAction == "File")
{
trace("File")
}
       else if(strAction == "Execute")
       {
       trace("Execute")
       }
       else if(strAction == "Exit")
       {
       trace("Exit")
       }
}
and as you can notice we have a lot of if conditions, so in the future if we want to add more actions then we need  to add more if conditions and itis a miss up code ;)

now in command patterns what we simply do is housing these command or actions in simple classes and we need to make sure to implement them using a common interface :

for the File action class we create a ClsExecuteFile class and implement the IExecute interface :

IExecute.as

package {
  public interface IExecute {  
    function Execute():void;
    function get_strCommand():String;
    function set_strCommand(x:String):void ;
  }
}



ClsExecuteFile.as

package {
  public class ClsExecuteFile implements IExecute{
        public var sStrCommand:String = "File"
        public function Execute():void
{
trace("File")
}
public function get_strCommand():String
{
return sStrCommand
}
public function set_strCommand(x:String):void 
{
sStrCommand = x;
}
  }
}

first think you need to know about above code that we cant declare a public property in the interface this is why i declare manually getter and setter .

the other thing we declare this variable the strCommand to identify what object we need to pull when any action occur.

so  in order to invoke these methods we need to create an invoker class which will save all the action objects in a container list and then based on which action is trigger we will return the object that is related to match action class .

ClsInvoker.as

package {
  public class ClsInvoker {   
   private var oObjectsList:Array = new Array()
        public  function ClsInvoker()
{
oObjectsList = new Array()
oObjectsList.push(new ClsExecuteFile())
}
public function getCommand(strCommand:String)
{
for(var i=0;i<oObjectsList.length;i++)
{
var oIExtecute:IExecute = oObjectsList[i]  as IExecute
if(oIExtecute.get_strCommand() == strCommand )
{
return oIExtecute;
}
}
return null;
}
  }
}
finally in order to execute any action we we first create object of the invoker class and then depend on the action we get the object and then simply execute it :

main execution  code :


var oClsInvoker:ClsInvoker = new ClsInvoker()
var oIexecute:IExecute = oClsInvoker.getCommand("File")
oIexecute.Execute();

and as you can find there is no if condition only simple three line of code and depend on the action we simply pass the action string to the invoker object.

in the future if you want to add new action you need to create a new action class then host its object in the invoker class container list and change the strCommand string to identify this object.

u can use this command when you want to save the action behavior like for example when we want to create undo, redo actions .

I hope this was very simple and helpful, for any questions or request just post it or email me at abed_q@hotmail.com

Source Code

No comments:

Post a Comment

Create facebook Iframe Application Using flash AS3 API ( Updated Version ).

please like and post your questions :  http://www.facebook.com/pages/FlashCS5-Tutorials/176320862386436 After I have received a lot of requ...