DEV Community

Cover image for What Everyone Ought to Know About Singelton design pattern
Abhiram
Abhiram

Posted on

What Everyone Ought to Know About Singelton design pattern

Singleton Pattern

  • Singleton Pattern says that just "define a class that has only one instance and provides a global point of access to it".

UML of Singleton design pattern

  • The above image depicts the UML of Singleton design pattern

UML - Unified Modeling Language

  • Popular for diagrammatic notation.

  • For Visualization, to understand & remember.

  • In UML, first block is considered as the class, second block is considered as Attributes & third block is considered as Methods

  • Signs used such as +, -, # have certain meaning. They will tell which Access modifier is being used.

  1. (+) - Public

  2. (-) - Private

  3. (#) - Protected

Steps to create Singleton design pattern

  • Through constructor the client can create many objects of a class

  • So make the constructor as private.

  • Now, how do we get the instance of the class

  • Create getInstance method which checks if instance is null or not. If instance is not null, return the same instance or call the constructor to create new instance.

  • Now how the client can call this method? because methods are called by objects

  • Client doesn't have object, so no access to constructor. Use static keyword. We do not need instance of the class if we use static.

Code walkthrough of Singleton design pattern

What is Multithreading and how to handle Multiple threads

  • In main method, do mistake purposefully in System.out.println we will see error exception in thread main which means main is a thread & by running it we are getting the error.

  • Normally code runs line by line but if we want 2 things to run simultaneously, then it will be multithreading.

  • Do Ctrl + Shift + Escape, you will get Task manager go to performance tab in that We can view how many cores are there & in each core 2 threads can be run. So Logical processors = 2 * Cores.

  • At a time only (Logical processors = 2 * Cores) threads can be run.

  • In Task manager We will see thousands of threads working at a time. But the (Logical processors = 2 * Cores) threads will take turns & switch within microseconds. That is why we see thousand of threads in numbers.

Task Manager - Performance

Why we are handling Mutiple threads in Singleton design pattern

  • If 2 threads access the getInstance method, both threads will check if instance is null or not. To both of them instance will be null and each thread will create one obecjt.

  • But our objective in Singleton design pattern is to create one object and use it for all instances. So to avoid this we will use synchronized key word.

  • To handle multiple threads we will be using synchronized key word. What it does is it will lock the operation until one thread operation is completed. So that multiple threads can not use a method at a time.

Handling Multiple threads in Singleton design pattern

  • In above image we can view we made the getInstance method synchronized so that multiple threads can not access it at the same time

Cons of Singleton design pattern

  • Should be handled carefully in Multithreaded environment

                   Happy Reading
    

Top comments (0)