Presentation is loading. Please wait.

Presentation is loading. Please wait.

Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.

Similar presentations


Presentation on theme: "Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization."— Presentation transcript:

1 Singleton Pattern

2 Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization versus usage ordering Solution Provide a global access method (static in C++) First use of the access method instantiates the class Constructors for instance are made private

3 Structure

4 Singleton Model

5 Code C++ Java

6 Here's a declaration of such a class: class Singleton { public: static Singleton* Instance(); protected: Singleton(); Singleton(const Singleton&); Singleton& operator= (const Singleton&); private: static Singleton* pinstance; };

7 The class's implementation looks like this: Singleton* Singleton::pinstance = 0; // initialize pointer Singleton* Singleton::Instance () { if (pinstance == 0) // is it the first call? { pinstance = new Singleton; // create sole instance } return pinstance; // address of sole instance } Singleton::Singleton() { //... perform necessary instance initializations }

8 The class's implementation looks like this: Note that this design is bullet-proof—all the following Instance() calls return a pointer to the same instance: Singleton *p1 = Singleton::Instance(); Singleton *p2 = p1->Instance(); Singleton & ref = * Singleton::Instance();

9 Singleton Class Template template class Singleton { public: // Global access point static TYPE *instance(); private: // Default constructor. Singleton(); // Contained instance. TYPE s_instance; }; Good example of when to use C++ templates When you would have “cut and paste” classes Parameterized by a concrete type That we want to make a singleton instance Allows many singletons to be declared & defined Using a single template Notice constructor and variable are private

10 Singleton Template Definition // Initialize the static // instance pointer template TYPE *Singleton::s_instance = 0; // Global access point template TYPE * Singleton::instance (void) { // check for existing instance if (Singleton ::s_instance == 0) { // may want a try/catch here Singleton ::s_instance = new Singleton ; } return Singleton ::s_instance; }; Initialization of static s_instance variable Outside class declaration Outside method definition Done before any method in compilation unit is called Instance accessor method can then check For a 0 instance pointer And create a new instance if so Same object is always returned by accessor

11 Using the Singleton Template Foo *f1 = Singleton ::instance(); Foo *f2 = Singleton ::instance(); Need a single instance E.g., a common buffer of text tokens from a file Shared across multiple points in the code Need to share buffer Copying is wasteful Need to refer to same object instance What about deleting these instances?

12 JAVA Singleton Defines an Instance operation (class operation) that lets clients access its unique instance May be responsible for creating its own unique instance public class Singleton { private static Singleton instance = null; public static Singleton getSingleton() { if( instance == null ) { instance = new Singleton(); } return instance; }

13 Collaborations Clients access a Singleton instance solely through Singleton's interface public class Client { public static void main(String[] args) { Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); if (s1 == s2 ) { System.out.println( “ Single object ” ); } }

14 Consequences Controlled access to sole instance Reduced name space Avoids global variables Permits refinement of operations & representation Subclassing allows selection at run-time Permits variable number of instances Change operation that grants access to the Singleton instance More flexible than class operations Static functions in C++ - cannot be virtual

15 Implementation Ensuring a unique instance Can hide operation that creates the instance behind a class operation Can guarantee that creation & initialization occur correctly Since _instance is a pointer to a Singleton object - change can be made at run-time to a different Singleton object (subtyping) Allows instantiation of object at time client specifies Static initialization time is not under control of client

16 Subclassing the Singleton class Question: which subclass instance Put decision in Singleton's Instance operation Put implementation of Instance in subclass  programmer needs to select correct subclass Registry of singletons Singleton classes - register their singleton instance in a known location Registry - maps between string names & singletons When singleton is needed - registry is searched  common interface required for all Singleton classes registration - cannot occur in constructor – circular Workaround static implementation of each subclass & have constructor register subclass singleton

17


Download ppt "Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization."

Similar presentations


Ads by Google