Design Patterns (Singleton design pattern)
A lot of the time, programmers know about how to do programming but they do not know the design pattern patterns. They think programming is about the OOP, simple SQL and simple API knowledge. Because I also had the same kind of knowledge. After that when I was working I had to learn a lot of things about programming and design patterns.
Because the industry lot of time use these kinds of stuff but we have to know about the design pattern also. With the knowledge of the design patterns, we can simplify the code.
In this post serious I am going to talk about the below design patterns.
- Singleton Design Pattern
- Factory Method Design Pattern
- Prototype Design Pattern
- Avoid Telescoping Constructors With Builder Pattern
- Implement Loose coupling with Chain Of Responsibility pattern
- Undo Changes With Memento Pattern
Design patterns can be dived into three parts.
- Creational design patterns
- Structural Design patterns
- Behavioural Design patterns
- Creational design patterns:- These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using a new operator. This gives the program more flexibility in deciding which objects need to be created for a given use case.
- Structural Design patterns:- These design patterns concern class and object composition. The concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.
- Behavioural Design patterns:-These design patterns are specifically concerned with communication between objects.
First, we talked about the Singleton design pattern
It is the most fundamental design pattern among the design patterns. The Singleton design pattern is under the Creational design patterns. In the singleton design pattern, we have only one object for the whole class. And we can create only one instance of that class. Restrict the instantiation of a class and ensure that only one instance of the class exists in the java virtual machine.
If you used arguments don’t use the Singleton Design pattern use the Factory design patterns.
Uses of Singleton Design pattern
- Ensure that we have only one instance for the existing class.
- Why do we use these one instance for class? Think you have to access external resources or Hardware interface (external DB, external Printer). To manage that kind of external resources we can use the singleton design patterns to cartelize the users.
- When we used Logger to the programmer (Logger has used show errors, info, and debug messages in the application)
- To read the Configuration
- To provide caching
- Using the Singleton Design pattern we can build another design pattern also like Abstract Factory, Builder, Prototype and Façade.
Singleton Design pattern structure
Implement the Singleton Design pattern
- To prevent multiple instances we can use the private constructor.
- The private static variable of the same class is the only instance of the class.
- The public static method that returns the instance of the class is the global access point outer world to get the instance of the singleton class.
Write the other three code bases here.
- An early instance of Singleton Pattern:-In such a case, we create the instance of the class at the time of declaring the static data member, so an instance of the class is created at the time of class loading.
- Lazy Instantiation of Singleton Pattern
Consider a scenario if two threads try to create an instance of a singleton class at the same time. In a multi-threaded environment, there is a possibility that separate objects get created, due to different times of accessing the (instance == null) check. This will break the singleton principle. The simplest way of achieving thread safety in the singleton design pattern is to make the SingletonLazyInstance () method synchronized.
At this location using the synchronized keyword will ensure thread safety but the application performance will be degraded. So on one side, we are resolving the problem on another side we are creating one more. To solve this, the Double Check Lock principle is used. Do not use the Synchronized keyword it will reduce the performance of your application. Instead of this used the (Double Check Lock principle)
- Double-Checked locking Singleton Pattern
This is used if we have a multithread environment.
Real-world example
Now you have an idea about the Singleton Design pattern. Try your example then you can get the maximum.