Design Patterns in Java - Part 01
“Finding patterns is the essence of wisdom.” — Dennis Prager
What are Design Patterns?
Software engineering is a complex set of processes and when it comes to designing and implementing the solution for a specific problem it should be maintainable and reusable, these are some of the most important factors in modern software.
In software engineering, there is the same problem occurs multiple times. overtime some solutions are preferred over the others because of their reusability, maintainability, and flexibility.
To these solutions, we call Desing Patterns, it’s a practical proven solution for reoccurring design problems. By using design patterns developers do not have to implement by using programming paradigms from the scratch. These design patterns are previously used in large systems and proven their capability of good design.
How Design Pattern Helps
Design patterns have proven and tested their usages in real-world software, the developers working on designing, building, and documenting the software does not have to build patterns from the scratch, and during the communication and documenting, it provides common names and structures for developers this saves lot’s of time and ensure the quality of the code.
Type of Design Patterns
There are 23 design patterns mentioned in the book “Design Patterns: Elements of Reusable Object-Oriented Software”. written by The Gang of Four. Design patterns are different from one system to another it’s based on the problem. All these design patterns are divided into three main categories. and few subcategories.
Creational Patterns - Creational design patterns are engaged with how to create, clone, and initialize design objects.
Structural Patterns - Structural patterns are described how the objects are connected. this pattern uses Decomposition and Generalization concepts to identify the relationship between the objects and how they are connected.
Behavioral Patterns - Behavioral patterns focused on how the objects have distributed the works. it defines how set objects work towards a common goal and how a single object carries out each of the purposes towards that common goal.
Now Let’s look at each of the sub-patterns inside the three main patterns.
1. Creational Patterns
Singleton Pattern
Singleton is the simplest form of the design patterns, singleton means something singular which means having only one. in a Java program, a singleton object means creating only one object for the entire application. (per JVM). and this object is globally accessible within the application.
Here’s a Sample Implementation for Singleton Pattern.
In Singleton, it is important not to pass parameters to create the objects.
Factory Method Pattern
In the Singleton pattern, we did not pass any parameters and we are aware of the output/return object. But in the factory design pattern developer can pass parameters and depend on the parameters that are using at the creation the object returned will change.
In factory design patterns, for the creation of objects handled by the subclasses.
Here’s a Sample Implementation for Factory Method Pattern.
Prototype Design Pattern
There are situations where lot’s of object creations to be done. Object creation is a memory expensive process, in enterprise application performance is a critical factor having to create lots of objects create performance bottlenecks.
Prototype design pattern solves this issue by cloning objects rather can creating them. before cloning should be implemented “Cloneable” interface.
There are two ways of Object cloning.
- Shallow Copy - Where all the values of the object are copied to a new object. and it always copies the original values from primitive variables and from the non-primitive (objects) it copies the reference of the object, because of this if the original object’s attributes are changed then it will affect the cloned objects.
- Deep Copy - Deep copy makes separately allocated objects, in here the original objects and the copied objects are independent of each other.
Here’s a Sample Implementation for Prototype Pattern
Builder Pattern
When developers creating Java objects the construction of the Object can have a couple of states, and depend on the inputs and variables at that time there are two simple solutions one is to create multiple constructors or pass “null” values for non-applicable variables, and also telescopic constructor where one constructor calls to related another constructor to pass variable and creates objects.
//Telescopic Constructorspublic PremiumUser(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}public PremiumUser(String id, String firstName, String lastName){
this(firstName, lastName);
this.id = id;
}public PremiumUser(String id, String firstName, String lastName, boolean isActive){
this(id, firstName, lastName);
this.isActive = isActive;
}
Though this solves the problem which is not a good programming practice.
The Builder pattern solves this issue by providing Builder class and does not have to worry about the constructor of using at the object initialization.
Here’s a Sample Implementation for the Builder Pattern.
Abstract Factory Pattern
Abstract Factory pattern is similar to the Factory Method design pattern which follows the principle of “Program to an interface not to an implementation”. But the difference between the Factory Method and Abstract Factory is that the Factory Method provides a certain method to create the relevant object, but Abstract Factory Pattern provides an interface to create families of objects by implementing the interface.
Here’s a Sample Implementation for the Abstract Factory Pattern.
In the next article, Let’s find out about Structural Patterns. And find the complete repository of the design patterns in Java.
For more information find the tutorial set below.