Design Patterns in Java
Posted By : Ankur Bansala | 24-Apr-2019
In Java, For best practices, we should use Design patterns which can be used to resolve some issues which are known to the developer.
There are multiple types of design patterns.
In Java, We have listed some of the widely used design patterns.
Singleton Design Pattern:
Singleton Design Pattern in Java. One of the most familiar question in Interviews as well. So I will explain the Singleton pattern and try to clarify all your queries.
If you are in Java Development then you must aware of the new keyword. The new keyword creates an Object of class.
But in most of the cases where you don’t want to create each Object for a different purpose. Singleton Pattern assures that one and only one Object is created for a class.
When an object of a class is required, only one Object get returned.
Factory Design Pattern:
The basic book of Design Patterns (written by the so-called Gang of Four or GoF) states: “Make an interface for instantiating an object, but lets subclasses choose which class to instantiate.”
In Other words: a factory facilitate deferring the instantiation of subclasses.
Decorator Design Pattern:
This is a very handy pattern at runtime if you want to extend class behaviour.
The concept of the decorator pattern is that it dynamically adds additional attributes to objects.
This is good because by extending you change the action of all instances of the given class but there is requirement some objects (instances of a given class) only to change their action.
The Gang of Four (GoF) book states:
Allows for the dynamic binding of objects while modifying their existing responsibilities and behaviours.
Composite Design Pattern:
Let’s see what the Gang of Four (GoF) states:
“Build objects into a tree structure to perform part-whole hierarchies. Composite lets the client treat each object and compositions of objects evenly.”
In this pattern, the client uses the component interface to collaborate with objects which are a segment of the composition.
You can devise the composite hierarchy as a tree where there are stalk and composites, and the requests are directed through this tree.
If the beneficiary of the call is a front then the request is managed directly in this leaf.
If the recipient is a composite then this composite progressing the requests to its children, alternately this composite can achieve further operations before and after forwarding.
Adapter Design Pattern
This pattern is particularly used to permit loosely-coupling of plugins into applications (for example the way Eclipse does this).
“Transform the interface of a class into another interface clients predict.
An adapter lets classes do the job in sync that couldn’t otherwise because of conflicting interfaces.”
So as you can notice the adapter pattern comes reachable if you want to adapt the process to work between conflicting types.
In this case, the adapter must be a “man in the middle” which disciple the request of the source to the request accepted by the target.
commonly you can have bidirectional communication too, in this case, the adapter converts the result from the target service to the one which is accepted by the source.
Implementation of Singleton Design Pattern:
Implementation One
1) We will create a private Constructor.
2) Static method to create an object of the same class.
class JBT {
private static JBT instance = null;
int i;
private JBT() {
}
static JBT createInstance() {
if (instance == null){
instance = new JBT();
return instance;
}
else
return instance;
}
}
Problem:
If 2 different thread enters at the same time into the createInstance method when the instance is null.
In that case, 2 different objects will be created by Threads. Which is against the Singleton pattern. In the next implementation, it can be solved.
Implementation Two:
In this approach:
1) We will make createInstance method synchronized and only one object will be created instead of Two.
class JBT {
private static JBT instance = null;
int i;
private JBT() {
}
static synchronized JBT createInstance() {
if (instance == null){
instance = new JBT();
return instance;
}
else
return instance;
}
}
Problem:
We have used synchronized keyword it will create a problem in terms of performance.
We are resolving the problem on another side we are creating one more problem. In the next implementation, we will solve both this problem.
Implementation Three:
class JBT {
private static JBT instance = new JBT();
int i;
private JBT() {
}
static JBT createInstance() {
return instance;
}
}
Conclusion:
So, We have discussed the Java Design Patterns and also found the limitations of each. We have also provided the multiple implementations of Singleton Design Pattern with pros and cons.
I hope this will help you to explore further.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ankur Bansala
Ankur is an experienced Java back-end Developer and having capabilities to build web application.