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:

 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;
  }
 }

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.



写作助手,把中式英语变成专业英文


Want to receive new post notification? 有新文章通知我

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 Brain” which is a collected resources of brain anatomy, function and diseases. I put the app i
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 works for Hitachi devices where the spatial relationship between channels are known. In the above example, the activ
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 *