190 likes | 424 Views
Singleton. …there can be only one…. The Singleton pattern. In some cases, we only want one instance of a class to be created…ever Could be a class which manages access to some critical resource A database manager A device driver A manager of application preferences ….
E N D
Singleton …there can be only one…
The Singleton pattern • In some cases, we only want one instance of a class to be created…ever • Could be a class which manages access to some critical resource • A database manager • A device driver • A manager of application preferences • … DCS – SWC
The Singleton pattern • How can we implement such a restriction? • Should we throw an exception, if someone tries to create a second instance…? DBManager dbm1 = new DBManager(); // OK ... DBManager dbm2 = new DBManager(); // Error! • Not really what we want… DCS – SWC
The Singleton pattern Clients Creator Please give me a reference to the (only) DBManager object OK OK (hey, I already have it! DBManager Please give me a reference to the (only) DBManager object DCS – SWC
The Singleton pattern • A client should not be able to create an object directly (new …) – he must ask a creator for it • Creator creates an object (unless it has already been created), and hands a reference back to the client • How do we prevent clients from creating objects themselves…? DCS – SWC
The Singleton pattern public class DBManager { public DBManager() {} … } DCS – SWC
The Singleton pattern public class DBManager { private DBManager() {} … } DCS – SWC
The Singleton pattern • Private methods can only be invoked by other methods in the same class • The class itself becomes respon-sible for creating objects of itself… DCS – SWC
The Singleton pattern public class DBManager { private DBManager() {} public DBManager createInstance() { return new DBManager(); } } DCS – SWC
The Singleton pattern public class DBManager { private static DBManager theInstance = null; private DBManager() {} publicstatic DBManager getInstance() { if (theInstance == null) theInstance = new DBManager(); return theInstance; } } DCS – SWC
The Singleton pattern • Turning a class T into a Singleton: • Make the constructor for T private • Add a static variable to T, storing a reference to an object of class T • Add a static method to T, which returns a reference to the object pointed to by the static variable. If the reference is null, the method should create an object of type T first, and set the static variable to refer to this object DCS – SWC
The Singleton pattern public class T // T is a Singleton { private static T theInstance = null; private T() { // Whatever we need here… } publicstatic T getInstance() { if (theInstance == null) theInstance = new T(); return theInstance; } } DCS – SWC
Singleton vs global variables • But wait – haven’t we just made a nice wrapping for a global variable? • Why not just do like this: public class T { public static T theInstance = new T(); private T() {} ... } DCS – SWC
Singleton vs global variables DCS – SWC
Singleton vs global variables • A global variable is created when the program starts (eager initialisation) • A Singleton is created when somebody asks for it (lazy initialisation) • If we only need the object sometimes, it is wasteful to create it unconditionally – in particular if it is resource-intensive DCS – SWC
Singleton destruction • We can control when a Singleton is created, but not when it is destroyed • Garbage collector should claim it when noone refers to it anymore • Order of destruction might matter! DCS – SWC
Singleton destruction • Suppose two singletons DataManager and DBManager are dependent: DCS – SWC
Singleton destruction • If the DBManager is destroyed before the DataManager, there might be a problem! • Somewhat difficult to manage the dependen-cies between singletons, but it can be done… DCS – SWC
Singleton– final remarks • Singleton is a useful – but also debated design pattern (see the Net) • Using Singletons in multi-threaded scenarios requires some enhancements • You cannot make a ”generic” Singleton class, and let your class inherit from it – who ”owns” the static object reference…? DCS – SWC