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.