Learning Employee Directory 2.1 Singleton pattern

41 sec read

Return to table of contents
While reading ED code, I frequently encounter singleton pattern. A singleton class is a class you can only instantiate once and it is useful to store application-wide data (say window position). ApplicationModel and all managers are singleton classes.

Here is ED’s approach to singleton class to avoid multiple instantiation:
[code=”js”]
public class ApplicationModel extends EventDispatcher
{

private static var instance : ApplicationModel;

/**
* Private constructor. Use getInstance() instead.
*/
public function ApplicationModel()
{
if ( instance != null )
{
throw new Error(“Private constructor. Use getIntance() instead.”);
}
}

/**
* Get an instance of the DataManager.
*/
public static function getInstance() : ApplicationModel
{
if ( instance == null )
{
instance = new ApplicationModel();
}
return instance;
}
}
[/code]

One will have to use ApplicationModel.getInstance(), instead of new ApplicationModel() to create an ApplicationModel object. Please note the use of “static” in the declaration of variable instance and function getInstance().

In ED, all managers are singleton classes — there is no need to have two managers of the same kind.

There are 4 managers: ConfigManager, DatabaseConnectionManager, DataSynchronizationManager and WindowPositionManager.

ConfigManager: Read employeedirectory_config.xml and get the value of configuration such as data location.
DatabaseConnectionManager: keep the SQLConnection instance.
DataSynchronizationManager: sychronization data
WindowPositionManager: keep the entire window inside the computer screen.


Don't want to miss new papers in your field? Check out Stork we developed:

How much money did I make from an app?

Undoubtedly some people are very successful in making money by developing a smartphone app. Back in 2012 I developed an app called “Handbook of...
Xu Cui
27 sec read

Handy programs to visualize NIRS data (2): plotTopoMap

Often you need to view the spatial pattern of activation as in the example below. plotTopoMap allows you to do that. It probably only...
Xu Cui
37 sec read

Flash 3D video demo

Racer Ostrova Zombie
Xu Cui
0 sec read

Leave a Reply

Your email address will not be published. Required fields are marked *