Name : Prasoon Dadhich
USN : 1MS09IS069
CODE
#include <iostream>
using namespace std;
class Singleton
{
private:
static bool instanceFlag;
static Singleton *single;
Singleton()
{
//private constructor
}
public:
static Singleton* getInstance();
void method();
~Singleton()
{
instanceFlag = false;
}
};
bool Singleton::instanceFlag = false;
Singleton* Singleton::single = NULL;
Singleton* Singleton::getInstance()
{
if(! instanceFlag)
{
single = new Singleton();
instanceFlag = true;
return single;
}
else
{
return single;
}
}
void Singleton::method()
{
cout << "Method of the singleton class" << endl;
}
int main()
{
Singleton *sc1,*sc2;
sc1 = Singleton::getInstance();
sc1->method();
sc2 = Singleton::getInstance();
sc2->method();
return 0;
}
Explanation
If we create any object of this class then the static object (player) will be returned and assigned to the new object.
Finally only one object will be there. The point is not how many times something gets executed, the point is, that the work is done by a single instance of the the class. This is ensured by the class method Singleton::getInstance().
As you can see in the above code,Client can access any instance of the singleton only through the getInstance() method.Once an instance is created,the instanceFlag value becomes true and then any further objects pointers created will be given the reference to the same object that has been created.
Also,make sure you notice the presence of a private constructor.This ensures that no new objects of the class are created and that all the class pointers get the same reference to work on.
2. Index
Intent
Structure
Example program
Explanation
Participants
Consequences
Limitations
Practical examples
3. Intent
Ensure a class only has one instance, and
provide a global point of access to it.
Motivation
It’s important for some classes to have
exactly one instance.
Although there can be many printers in a
system, there should be only one printer
spooler.
There should be only one file system (or file
system manager) and one window manager.
5. Example program:
public class TestSingleton {
public static void main(String args[]) {
// Get a reference to the single instance of Singleton.
Singleton s = Singleton.instance();
s.setData(34);
System.out.println("First reference: " + s);
System.out.println("Singleton data value is: " +
s.getData());
6. // Get another reference to the Singleton.
// Is it the same object?
s = null;
s = Singleton.instance();
System.out.println("nSecond reference: " + s);
System.out.println("Singleton data value is: " +
s.getData());
} }
7. The test program output is as shown:
First reference:
Singleton data value is: 34
Second reference:
Singleton data value is: 34
8. Note that the singleton instance is only created when
needed. This is called lazy instantiation.
What if two threads concurrently invoke the instance()
method? Any problems?
If invoked concurrently, two instances of the singleton
class could be created. However, this could be
prevented using the following 2 techniques:
i) Make the instance() synchronized. Synchronization
is expensive, however, and it may have a negative
impact on the performance. Moreover, it is really
only needed the first time the unique instance is
created.
9. ii) Do an eager instantiation of the instance rather
than a lazy instantiation. An eager class loader
loads all the classes comprising the application
code at startup. Such an implementation requires
a static initializer and is thread safe.
10. Singleton Participants
Singleton - Defines an instance operation that
lets clients access its unique instance. Instance
is a class operation (static method). May be
responsible for creating its own unique
instance
Client - Accesses a singleton instance solely
through the singleton’s instance() method.
11. Singleton Consequences
Controlled access to sole instance facilitates
strict control over when and how the clients
access it.
The singleton pattern is an improvement over
global variables.
It may be subclassed and it is easy to configure
an instance of an application that extends the
functionality of singleton at run-time.
More flexible than class operations.
12. Singleton Limitations
The main limitation of the singleton pattern is
that it permits the creation of only one instance
of the class, while most practical applications
require multiple instances to be initialized.
Furthermore, in case of singleton, the system
threads fight to access the single instance
thereby degrading the performance of the
application.
13. Examples
Cache – The cache can be used as a singleton
object, having a global point of reference for
all future references to the cache object and the
client application uses this in-memory object.
Configuration File – Any application has a
single point of reference through its
configuration files. Any changes to be made to
the application will have to be made via this
single configuration file.