design pattern - singleton pattern

19
Singleton Design Pattern From definition to implementation Mudasir Qazi - [email protected] 1 16-Dec-14

Upload: mudasir-qazi

Post on 08-Aug-2015

210 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 1

Singleton Design Pattern

From definition to implementation

16-Dec-14

Page 2: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 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)

16-Dec-14

Page 3: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 3

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.

16-Dec-14

Page 4: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 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.

16-Dec-14

Page 5: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 5

Usage Example

16-Dec-14

Page 6: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 6

UML - Class Diagram

We need1) Private Static Instance of Class2) Private Constructor3) Public Static method with return type of

Class to access that instance.

16-Dec-14

Page 7: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 7

UML - Sequence Diagram

16-Dec-14

Page 8: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 8

Singleton in Heap

Only one object is created, all other threads call the same object without creating new one.

16-Dec-14

Page 9: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 9

Lazy Instantiation (C# and Java)

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

Page 10: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 10

Lazy Instantiation (C# and Java) - Test

16-Dec-14

Page 11: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 11

Early / Eager Instantiation (C#)

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

Page 12: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 12

Thread-Safe Singleton in C#

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

Page 13: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 13

Thread-Safe Singleton in Java

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

Page 14: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 14

Double Check Locking Implementation (Lazy)

These implementations also comes under synchronized or thread safe implementations. Left one is better.

16-Dec-14

Page 15: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 15

Static Block Initialization

This is not very common use of singleton. But it also exists so I mentioned it.

16-Dec-14

Page 16: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 16

N-Singleton Implementation (N=3)

MAX = 3,Means that maximum 3 instances can created.

16-Dec-14

Page 17: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 17

N-Singleton Implementation (N=3) - Output

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

Page 18: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 18

N-Singleton as Singleton (N=0)

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

Page 19: Design Pattern - Singleton Pattern

Mudasir Qazi - [email protected] 19

N-Singleton as Singleton (N=0) - Output

Output proves that only one instance is created.

16-Dec-14