Introduction to Design Patterns in Java
In any programming language, independent strategies are used to solve common object-oriented design problems, and these is 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 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 have an effect on other parts of an application.
The following are subcategories of structural design pattern:
- 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 should we 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 patterns that make our code easy to understand and debug in case of errors. Using Design patterns makes our code easy to understand, even for beginner level developers.
Advantages of Desgin 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. A singleton design pattern involves creating a singleton class that returns the same instance every time it is instantiated. 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 is a singleton class that returns the same instance every time the getInstance () method is called. In the Main class, we have called the getInstance () method three times and captured the result in three variables. As we can see after modifying the variable first, the other two variables also get modified. Again after the variable third is modified, the other two variables also get modified. Hence, all variables first, second, and third are pointing to the same object instance. Therefore a singleton design pattern results in the creation of a single object.
Conclusion
Using design patterns for developing an application has been adopted universally by all developers. Therefore it is important to have knowledge about design patterns. As described above use of design patterns makes 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 –
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses