Saturday, 18 June 2011

Event flow in AS3.0

Flash Player or AIR dispatches event objects whenever an event occurs. If the event target is not on the display list, Flash Player or AIR dispatches the event object directly to the event target. For example, Flash Player dispatches the progress event object directly to a URLStream object. If the event target is on the display list, however, Flash Player dispatches the event object into the display list, and the event object travels through the display list to the event target.
The event flow describes how an event object moves through the display list. The display list is organized in a hierarchy that can be described as a tree. At the top of the display list hierarchy is the Stage, which is a special display object container that serves as the root of the display list. The Stage is represented by the flash.display.Stage class and can only be accessed through a display object. Every display object has a property named stage that refers to the Stage for that application.
When Flash Player or AIR dispatches an event object for a display list-related event, that event object makes a roundtrip journey from the Stage to the target node. The DOM Events Specification defines the target nodeas the node representing the event target. In other words, the target node is the display list object where the event occurred. For example, if a user clicks on a display list object named child1, Flash Player or AIR will dispatch an event object using child1 as the target node.
The event flow is conceptually divided into three parts. The first part is called the capture phase; this phase comprises all of the nodes from the Stage to the parent of the target node. The second part is called the target phase, which consists solely of the target node. The third part is called the bubbling phase. The bubbling phase comprises the nodes encountered on the return trip from the parent of the target node back to the Stage.


Example  of event flow download and see it.


Click




Friday, 10 June 2011

Singleton in AS3

There are some situations where we need to allow only one object to be created for a class. This can be achieved by “Singleton” pattern in as3. We can use the singleton pattern to access a class object globally.

How to prepare a singleton class:-
whenever we want to create a singleton pattern, we should keep two important steps in our mind.
  • We have to block the constructor to restrict instance creations of the class.
  • In order to access that class we should provide one static property

In the following example (SingletonExample.as ), First I have created a singleton class where the constructor has been blocked using throw statement. The throw statement throws an error whenever we tied to create an instance with syntax like var instace:SingletonExample = new SingletonExample();

Example(SingletonExample.as ):-

package
{
public class SingletonExample
{
public function SingletonExample()
{
throw(new Error(“Error: Instantiation failed: You can't instantiate a SingletonExample Class with new keyword”))
}
}
}


var instace:SingletonExample = new SingletonExample();//output: Error: Instantiation failed: You can't instantiate a SingletonExample Class with new keyword


Now we are providing a static property to access SingletonExample class.

Example:-

package
{
private static var _instance:SingletonExample=null
private static var lock:Boolean=true;


public class SingletonExample
{

public static function get instance(): SingletonExample
{
if( _instance==null)
{
lock=false;
_instance=new SingletonExample();
lock=true;
}
return _instance;
}


public function SingletonExample()
{
if(lock)
{throw(new Error(“Error: Instantiation failed: You can't instantiate a SingletonExample Class with new keyword”))}
}
public function message():void
{
trace(“Well Come to Singleton Class”)
}

}// Class closed
} // package closed


By using instance property of the above class we can access the message method as following.

SingletonExample.instance.message()// output panel:- Well Come to Singleton Class



Singleton class vs Static class?
  • The Singleton class object is created only when we make the first call through instance property. Since it uses lazy instantiation it will save memory consumption. Where as in static classes, all static members will consume more system resources and may slow down the startup process of the application.
  • it's easy to re factor . When made the changes from singleton to multiton ('n' of instance) is too easy
  • Static methods and properties cannot be inherited.
You can download the source files from the following link. Feel free to share your thoughts on this.

singleton.rar

Saturday, 4 June 2011

Get & Set (getter & setter) in As3.0

Using Get and Set keywords

Getter and setter methods are useful for class encapsulation. In an encapsulated class, the instance variables should not have a public access modifier. Because the public variables can be easily modified by another class without knowing a source class object. So we have to use private variables in encapsulation. If we declare private variables then no one can not access it from outside of the class
We can access this private data through both Getter and Setter methods. Using Getter and Setter is very easy in as3. Its a simple public method for accessing the private variables without calling the parenthesis ()

syntax:-

private var _instance:datatype;

public function get functionName():datatype
{
// send the private variable values
}

public function set functionName(value:datatype): void
{
// assn value into the private variables
}


get and set.rar