Singleton Design Pattern From definition to implementation Mudasir Qazi - mudasirqazi00@gmail.com 116-Dec-14
Contents / Agenda • Definition and types • Advantages and usage • UML – Class diagram • UML – Sequence diagram • Singleton in Memory (Memory allocation) • Implementation – Lazy Singleton (C# and Java) • Implementation – Early Singleton (C#) • Thread-Safe Singleton in C# • Thread-Safe Singleton in Java • Double-check locking singleton (thread-safe) • Static Block Implementation (thread-safe) • Implementation – N-Singleton (C# and Java) Mudasir Qazi - mudasirqazi00@gmail.com 216-Dec-14
Definition • Ensures that a class has only one instance, and provide a global point to access this instance. • In other words, a class must ensure that only single instance should be created and single object can be used by all other classes. • There are two forms of singleton design pattern 1. Early Instantiation: Creation of instance at load time. 2. Lazy Instantiation: Creation of instance when required. 3. N-Singleton: Create specific number of objects. • It comes under “Creational Design Patterns” category. Mudasir Qazi - mudasirqazi00@gmail.com 316-Dec-14
Advantages and Usage • Advantages • Saves memory because object is not created at each request. Only single instance is reused again and again. • In cases when object creation is very costly (time taking), we don’t have to create new object each time we need it. We just access already created object. • Usage • Singleton pattern is mostly used in multi-threaded and database applications. • It is used in logging, caching, thread pools, configuration settings etc. • For database connection, because one connection is enough for most applications and too much connections can make application slow. Mudasir Qazi - mudasirqazi00@gmail.com 416-Dec-14
Usage Example Mudasir Qazi - mudasirqazi00@gmail.com 516-Dec-14
UML - Class Diagram Mudasir Qazi - mudasirqazi00@gmail.com 6 We need 1) Private Static Instance of Class 2) Private Constructor 3) Public Static method with return type of Class to access that instance. 16-Dec-14
UML - Sequence Diagram Mudasir Qazi - mudasirqazi00@gmail.com 716-Dec-14
Singleton in Heap Mudasir Qazi - mudasirqazi00@gmail.com 8 Only one object is created, all other threads call the same object without creating new one. 16-Dec-14
Lazy Instantiation (C# and Java) Mudasir Qazi - mudasirqazi00@gmail.com 9 This is very common implementation of Singleton but is not very good in Multithreaded applications. Because there is a possibility that multiple thread can call the method getInstance on same time due to Race Condition. If happens so, then it would create multiple instances. Means meaning of singleton can will not be achieved. 16-Dec-14
Lazy Instantiation (C# and Java) - Test Mudasir Qazi - mudasirqazi00@gmail.com 1016-Dec-14
Early / Eager Instantiation (C#) Mudasir Qazi - mudasirqazi00@gmail.com 11 This is possible in C# because of ‘readonly’ keyword. Java don’t have ‘readonly’ keyword (but it can be achieved using final keyword, see next slide). And also note that ‘getInstance’ is a property (getter/setter) for variable instance, it has no parenthesis after its name. Note that, is this not thread safe. If you are working in multi threaded system then you should not use this implementation. 16-Dec-14
Thread-Safe Singleton in C# Mudasir Qazi - mudasirqazi00@gmail.com 12 Implementation in this picture will be thread safe. In any condition there would be one and only one instance of the Singleton class in Multithreaded system. 16-Dec-14
Thread-Safe Singleton in Java Mudasir Qazi - mudasirqazi00@gmail.com 13 Advantages: 1) The instance is not constructed until the class is used. 2) There is no need to synchronize the getInstance() method meaning all threads will see the same instance and no (expensive) locking is required. 3) The final keyword means that the instance can not be redefined, ensuring that one (and only one) instance will ever exists. 4) This is thread safe. (This is best implementation for both Multithreaded and single threaded applications) 16-Dec-14
Double Check Locking Implementation (Lazy) Mudasir Qazi - mudasirqazi00@gmail.com 14 These implementations also comes under synchronized or thread safe implementations. Left one is better. 16-Dec-14
Static Block Initialization Mudasir Qazi - mudasirqazi00@gmail.com 15 This is not very common use of singleton. But it also exists so I mentioned it. 16-Dec-14
N-Singleton Implementation (N=3) Mudasir Qazi - mudasirqazi00@gmail.com 16 MAX = 3, Means that maximum 3 instances can created. 16-Dec-14
N-Singleton Implementation (N=3) - Output Mudasir Qazi - mudasirqazi00@gmail.com 17 Output shows that all 3 instances are different. If you do same test with Singleton (or give MAX = 0 in N-Singleton) the output would be “obj1=obj2=obj3” 16-Dec-14
N-Singleton as Singleton (N=0) Mudasir Qazi - mudasirqazi00@gmail.com 18 Here N=0 (we give count < MAX condition so N=0 actually means that 1 instance will be created. If we have given <= condition then we have given N=1) to create single instance. 16-Dec-14
N-Singleton as Singleton (N=0) - Output Mudasir Qazi - mudasirqazi00@gmail.com 19 Output proves that only one instance is created. 16-Dec-14

Design Pattern - Singleton Pattern

  • 1.
    Singleton Design Pattern Fromdefinition to implementation Mudasir Qazi - mudasirqazi00@gmail.com 116-Dec-14
  • 2.
    Contents / Agenda •Definition and types • Advantages and usage • UML – Class diagram • UML – Sequence diagram • Singleton in Memory (Memory allocation) • Implementation – Lazy Singleton (C# and Java) • Implementation – Early Singleton (C#) • Thread-Safe Singleton in C# • Thread-Safe Singleton in Java • Double-check locking singleton (thread-safe) • Static Block Implementation (thread-safe) • Implementation – N-Singleton (C# and Java) Mudasir Qazi - mudasirqazi00@gmail.com 216-Dec-14
  • 3.
    Definition • Ensures thata class has only one instance, and provide a global point to access this instance. • In other words, a class must ensure that only single instance should be created and single object can be used by all other classes. • There are two forms of singleton design pattern 1. Early Instantiation: Creation of instance at load time. 2. Lazy Instantiation: Creation of instance when required. 3. N-Singleton: Create specific number of objects. • It comes under “Creational Design Patterns” category. Mudasir Qazi - mudasirqazi00@gmail.com 316-Dec-14
  • 4.
    Advantages and Usage •Advantages • Saves memory because object is not created at each request. Only single instance is reused again and again. • In cases when object creation is very costly (time taking), we don’t have to create new object each time we need it. We just access already created object. • Usage • Singleton pattern is mostly used in multi-threaded and database applications. • It is used in logging, caching, thread pools, configuration settings etc. • For database connection, because one connection is enough for most applications and too much connections can make application slow. Mudasir Qazi - mudasirqazi00@gmail.com 416-Dec-14
  • 5.
    Usage Example Mudasir Qazi- mudasirqazi00@gmail.com 516-Dec-14
  • 6.
    UML - ClassDiagram Mudasir Qazi - mudasirqazi00@gmail.com 6 We need 1) Private Static Instance of Class 2) Private Constructor 3) Public Static method with return type of Class to access that instance. 16-Dec-14
  • 7.
    UML - SequenceDiagram Mudasir Qazi - mudasirqazi00@gmail.com 716-Dec-14
  • 8.
    Singleton in Heap MudasirQazi - mudasirqazi00@gmail.com 8 Only one object is created, all other threads call the same object without creating new one. 16-Dec-14
  • 9.
    Lazy Instantiation (C#and Java) Mudasir Qazi - mudasirqazi00@gmail.com 9 This is very common implementation of Singleton but is not very good in Multithreaded applications. Because there is a possibility that multiple thread can call the method getInstance on same time due to Race Condition. If happens so, then it would create multiple instances. Means meaning of singleton can will not be achieved. 16-Dec-14
  • 10.
    Lazy Instantiation (C#and Java) - Test Mudasir Qazi - mudasirqazi00@gmail.com 1016-Dec-14
  • 11.
    Early / EagerInstantiation (C#) Mudasir Qazi - mudasirqazi00@gmail.com 11 This is possible in C# because of ‘readonly’ keyword. Java don’t have ‘readonly’ keyword (but it can be achieved using final keyword, see next slide). And also note that ‘getInstance’ is a property (getter/setter) for variable instance, it has no parenthesis after its name. Note that, is this not thread safe. If you are working in multi threaded system then you should not use this implementation. 16-Dec-14
  • 12.
    Thread-Safe Singleton inC# Mudasir Qazi - mudasirqazi00@gmail.com 12 Implementation in this picture will be thread safe. In any condition there would be one and only one instance of the Singleton class in Multithreaded system. 16-Dec-14
  • 13.
    Thread-Safe Singleton inJava Mudasir Qazi - mudasirqazi00@gmail.com 13 Advantages: 1) The instance is not constructed until the class is used. 2) There is no need to synchronize the getInstance() method meaning all threads will see the same instance and no (expensive) locking is required. 3) The final keyword means that the instance can not be redefined, ensuring that one (and only one) instance will ever exists. 4) This is thread safe. (This is best implementation for both Multithreaded and single threaded applications) 16-Dec-14
  • 14.
    Double Check LockingImplementation (Lazy) Mudasir Qazi - mudasirqazi00@gmail.com 14 These implementations also comes under synchronized or thread safe implementations. Left one is better. 16-Dec-14
  • 15.
    Static Block Initialization MudasirQazi - mudasirqazi00@gmail.com 15 This is not very common use of singleton. But it also exists so I mentioned it. 16-Dec-14
  • 16.
    N-Singleton Implementation (N=3) MudasirQazi - mudasirqazi00@gmail.com 16 MAX = 3, Means that maximum 3 instances can created. 16-Dec-14
  • 17.
    N-Singleton Implementation (N=3)- Output Mudasir Qazi - mudasirqazi00@gmail.com 17 Output shows that all 3 instances are different. If you do same test with Singleton (or give MAX = 0 in N-Singleton) the output would be “obj1=obj2=obj3” 16-Dec-14
  • 18.
    N-Singleton as Singleton(N=0) Mudasir Qazi - mudasirqazi00@gmail.com 18 Here N=0 (we give count < MAX condition so N=0 actually means that 1 instance will be created. If we have given <= condition then we have given N=1) to create single instance. 16-Dec-14
  • 19.
    N-Singleton as Singleton(N=0) - Output Mudasir Qazi - mudasirqazi00@gmail.com 19 Output proves that only one instance is created. 16-Dec-14