Updated June 9, 2023
Introduction to Design Patterns in Java
In any programming language, independent strategies are used to solve common object-oriented design problems, and these are called design patterns. This represents an idea and not an implementation process in OOPS. Codes are made more flexible, reusable, and manageable using design patterns. There are three design patterns- creational, structural, and behavioral used in the Java language. Design patterns are solutions to repeatedly occurring problems in the code. Design patterns are specific to a particular scenario and provide standard terminology for each problem. Visual clues are offered, and we can customize them to reach the desired outcome.
Types of Design Patterns Available in Java
Given below are the types of design patterns available in java:
1. Creational Design Pattern
This type of design pattern is associated with the creation of an object. The following are sub-categories of the creation design pattern:
- Singleton Pattern
- Builder Pattern
- Factory Pattern
- Abstract Factory Pattern
- Prototype
2. Structural Design Patterns
This type of design pattern is concerned with the code structure followed while developing an application. Using a structural design pattern ensures that changing one part of an application does not affect other parts of the application.
The following are sub-categories of structural design patterns:
- Adapter Pattern
- Bridge Pattern
- Composite Pattern
- Façade Pattern
- Proxy Pattern
- Flyweight Pattern
- Decorator Pattern
3. Behavioral Design Patterns
This pattern is associated with algorithms and patterns of communication used when two or more objects need to communicate. The following are different types of behavioral design patterns:
- Chain of Responsibility Pattern
- Command Pattern
- Interpreter Pattern
- Iterator Pattern
- Mediator Pattern
- Observer Pattern
- Strategy Pattern
- Template Pattern
- Visitor Pattern
- Null Object Pattern
Why Should We Use Design Patterns?
The following are the main reasons why we should go for design patterns in java:
- Standard Approach: Using design patterns is considered a standard approach in java understood by all developers. This leads to the development of consistent code by different developers.
- Code Reusability: Using Design patterns increases code reusability, which results in the development of robust, maintainable, and testable code.
- Easy to Understand and Debug: Design patterns are defined and accepted practices that make our code easy to understand and debug in case of errors. Design patterns make our code easy to understand, even for beginner-level developers.
Advantages of Design Patterns
- Design patterns provide code transparency while designing an application.
- Design patterns are a well-tested and universally accepted way of code development.
- Using Design patterns makes the analysis and requirement-gathering stage of SDLC easy.
Example of Design Patterns in Java
Now we will see java code examples of singleton design patterns. The singleton design pattern requires creating a singleton class that returns the same instance every time someone instantiates it. The below code example shows the creation and usage of a singleton class:
Code:
package com.edubca.singleton;
class SingletonClassDemo
{
// static variable instance of type SingletonClassDemo
private static SingletonClassDemo instance = null;
// variable of type String
public String text;
// private constructor restricted to this class itself
private SingletonClassDemo()
{
text = "THIS IS SINGLETON EXAMPLE FROM EDUBCA ";
}
// static method to create instance of SingletonClassDemo class
public static SingletonClassDemo getInstance()
{
if (instance == null)
instance = new SingletonClassDemo();
return instance;
}
}
// Main Class
class SingletonTest
{
public static void main(String args[])
{
// instantiating SingletonClassDemo class with variable first
SingletonClassDemo first = SingletonClassDemo.getInstance();
// instantiating SingletonClassDemo class with variable second
SingletonClassDemo second = SingletonClassDemo.getInstance();
// instantiating SingletonClassDemo class with variable third
SingletonClassDemo third = SingletonClassDemo.getInstance();
// Modifying variable of instance first
first.text = (first.text).toLowerCase();
System.out.println("Value from first instance is : " + first.text);
System.out.println("Value from second instance is : " + second.text);
System.out.println("Value from third instance is : " + third.text);
System.out.println("\n");
// Modifying variable of instance third
third.text = (third.text).toLowerCase();
System.out.println("Value from first instance is : " + first.text);
System.out.println("Value from second instance is : " + second.text);
System.out.println("Value from third instance is : " + third.text);
}
}
Output:
In the above example, SingletonClassDemo functions as a singleton class that returns the same instance every time someone calls the getInstance() method. In the Main class, we have called the getInstance () method three times and captured the result in three variables. As we can observe, modifying the first variable results in modifying the other two variables. Modifying the third variable further leads to modifying the other two variables as well. Hence, all variables first, second, and third point to the same object instance. Therefore a singleton design pattern results in the creation of a single object.
Conclusion
All developers have adopted design patterns for developing an application universally. Therefore it is essential to know design patterns. As described above, design patterns make our code robust, maintainable, testable, and fast.
Recommended Articles
This has been a guide to Design Patterns in Java. Here we discuss the introduction, types, uses, advantages, and examples of design patterns available in java. You may also have a look at the following articles to learn more –