Corporate Trainer | Designer | Developer

Corporate Trainer | Designer | Developer

Friday, January 13, 2012

What is component life cycle in flex 4?


Component life cycle is the way in which in flex framework interacts with every component. It is set of methods the framework calls to instantiate, control and destroy components. They are method that takes the most of every frame.

Flex component life cycle: 3 stage

Birth: Construction

· Calls your constructor.

· Can access properties and methods of class but not its children as they are not created yet.

· Must be public and no return type.

· Call super()

· Not used often in flex.

Birth: Configuration

· The process of assigning values to properties on objects

· This results in the first call to commitProperties();

· Containers must not expect children to be added yet.

Birth: Attachement

· Adding a component to element list

· parent.addElement(yourComponent)

Birth: Initialization

· After attachment, components are initialized

· It involves several method calls and events dispatches.

Life: Deferment

· The invalidation process

o When a property is set, retain the value on a private variable

o Set a ‘dirty’ flag

o Invalidate the Component

· The validation process

o When the framework calls validation methods, updates your component accordingly.

o Ex . setting text on TextBase class.

Life: Invalidation

· Invalidation Methods plays role here, called to “invalidate” a component, but not do any work on it.

· invalidateProperties() – any property changes

· invalidateSize() –change to width or height

· invalidateDisplayList() –change to child elements

· invalidateSkinState() – sets skinChanged property to true and calls invalidateProperties()


· “do the work” that invalidation requires move things, add things and remove things etc.

· Methods called are

o commitProperties()- all changes based on property and data events go here

o getCurrentSkinState()- used if the current skin state need to be updated

o updateDisplayList() – used when elements on display list need to be drawn.

o measure()- caluculate the size of components


· It puts off the “real work” until the appropriate time.


· In this phase component assigns handlers to user events

· Process things and cause invalidation to occur


· This refers to the process of removing components from the element list.

· These component can be re-parented( come back to live) or abandoned to die.


· Any object in the element list with no active reference is eligible for garbage collection.

Thursday, January 5, 2012

How to implement Singleton pattern in ActionScript 3.0

Today I will discuss how to implement singleton design pattern using ActionScript 3.0.

Let start with the basics

The Singleton Pattern

This pattern ensures that a class has only one instance. It also ensure that global access is provided to this single instance.

Singletons are generally used in situations where data needs to be kept synchronized or for centralizing

shared resources.

Take for example the situation in which two views display and allow the user to update data, but hold

two separate instances of that data. When one view updates the data, the other view will be out of sync.

However, if both views are pointing to the same object, this problem no longer exists; all you have to do

is refresh the views (which you would probably do by implementing the observer pattern on the model

with the views as observers).

Another possibility is two views that need to access the same external data source via a service call.

If each view has its own instance of the service call, you now have to maintain that code in two different

locations and you will be creating duplicate resources (in the form of objects) that do essentially the same thing. Again, this is not an issue if both views are referencing the same object

A typical singleton class is made up of three major parts. These are:

  • A static variable that holds a reference to the single instance.
  • A public static method (most commonly called getInstance) that provides global access to the stored instance if one already exists and creates it if it does not.
  • Some means of limiting the class to a single instance.

A typical singleton class might look like the following

package com.somedomain.somepackage{

private static instance:SingletonExample;

private function SingletonExample():void{

//any code need to initialize the object


public static function getInstance():SingletonExample{

if (SingletonExample.instance == null){

SingletonExample.instance = new SingletonExample();


return SingletonExample.instance;



The single instance of the class is held in the private static instance property. The constructor of the class

is set to private so that it cannot be accessed from outside the class. The public static method getInstance

provides the only method for retrieving the instance, ensuring that all classes that make use of it are

referencing the same instance.

Singleton Pattern implementation in Action Script 3.0

The implementation of a singleton class in ActionScript is slightly different, since ActionScript does not

allow for private constructors. You can simulate a private constructor by using an internal class, as in the

following example:


public class Singleton


private static var instance:Singleton;

public function Singleton(enforcer:SingletonEnforcer){}

public static function getInstance():Singleton


if (Singleton.instance == null)


Singleton.instance = new Singleton(new SingletonEnforcer);


return Singleton.instance;




class SingletonEnforcer{}

The class SingletonEnforcer is defined in the same file as the singleton class, but outside the package

definition. When a class is defined in this manner in ActionScript, then only the main class defined in the

file (the one inside the package definition) may access it.

Since the constructor requires an instance of the SingletonEnforcer, any attempt to directly instantiate the

class using the constructor will fail because of an invalid parameter error. However, since the getInstance

function is internal to the class, it may freely access the SingletonEnforcer class and pass it to the


Hope this will help you to understand how to implement Singleton pattern in ActionScript 3.0

visit: to view my profile and other articles and post.