singleton pattern
TRANSCRIPT
Special Topics in Software Engineering
Creational PatternsSingleton Pattern
Rêber & Abdullah
Creational PatternsAbstract Factory √
Builder √
Factory Method √
Prototype √
Singleton
Contents• Introduction• Intent• Motivation• Applicability• Structure and Participants • Consequences/Benefits• Known Uses • Implementation• Code
Definition In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.
There are some who are critical of the singleton pattern and consider it to be an anti-pattern in that it is frequently used in scenarios where it is not beneficial, introduces unnecessary restrictions in situations where a sole instance of a class is not actually required.
-Courtesy: Wikipedia
Intent• Ensure a class only has one instance, and provide a
global point of access it.
How?• A better solution is to make the class itself responsible
for keeping track of its sole instance. The class can ensure that no other instance can be created (by intercepting requests to create new objects), and it can provide a way to access the instance. This is the Singleton pattern.
Motivation• It's important for some classes to have exactly one
instance.
For example:
• There can be many printers in a system, there should be only one printer spooler. There should be only one file system and one window manager. A digital filter will have one A/D converter system will be dedicated to serving one.
Applicability
• It has to be exactly one instance of a class, and must be accessible to clients from a known access point.
• When sole instance is extensible (become new) by sub-classing, therefore clients should be able to use an extended instance without code modification.
• Defines an Instance operation that lets clients access its unique instance. Instance is a class operation.
• May be responsible for creating its own unique instance.
Clients access a Singleton instance solely through Singleton's Instance operation.
Participants Structure
Consequences –Benefits • Controlled access to sole instance: Because its class
encapsulates (covers) its sole instance, it can control over how and when clients access it.• Reduced name space: It avoids polluting (spoiling) the
name space with global variables that is in sole instances, because of having improvement over these variables.• Permits a variable number of instances: It makes it
easy to change your mind and allow more than one instance of the class.Only the operation that grants access to the Singleton instance needs to change.
Cont.• Permits refinement of operations and representation: The class may be sub-classed, You can configure the application with an instance of the class you need at run-time.• More flexible than class operations: Another way
to package a singleton's functionality is to use class operations.
Well-known uses of Singleton Pattern• An example of the Singleton pattern in Smalltalk-80
(which is an object-oriented, dynamically typed, reflective programming language), here the Singleton does a set of changes to the code known as ChangeSet current.
• Another example is using it in The Interviews user interface toolkit which the Singleton here uses to access the unique instance of its Session and WidgetKit classes, among others.
Implementation• Ensuring a unique instance:
It makes the sole instance a normal instance of a class, but that class is written so that only one instance can ever be created.
To do this is we need to hide the operation that creates the instance behind a class operation that guarantees only one instance is created.
class Singleton {public:static Singleton* Instance();protected:Singleton();private:static Singleton* _instance;};
//The corresponding implementation isSingleton* Singleton::_instance = 0;
Singleton* Singleton::Instance () {if (_instance == 0) {_instance = new Singleton;}return _instance;}
Cont.• Sub-classing the Singleton class: The main issue is
not so much defining the subclass but installing its unique instance so that clients will be able to use it. In essence, the variable that refers to the singleton instance must get initialized with an instance of the subclass.
Thanks for your attention
Don’t be hesitate to ask any question
Code Time . . .