Design Patterns in Java

Arriving at problems in various aspects of software design is inevitable. Stating every problem each time is rigorous. It becomes even more hectic if those problems are frequent.
Java has come up with an ultimate solution to this. It consists of a vast concept called “Design patterns”, which consist of various predefined strategies in the form of patterns.

What is a design pattern?

The process of software design deals with a lot many problems. Analysts have come up with a lot of common issues and solutions for it. The framing of these solutions for common problems led to the concept of design patterns in the software designing field.

These design patterns are easily reusable and avoid unwanted software development delays. Moreover, they are programming language-independent strategies to solve a common problem. Implementing such patterns will make our code flexible and easy to maintain.

The role of these patterns is to display the relationships and interactions between the classes and the objects. Implementing such patterns in every project is not compulsory and is not part of project development. However, it serves as a problem solver in some instances. You should also know that not all patterns can bring the same set of results. Each pattern is meant to solve a different problem.

The Gang of Four (GOF):

The book ‘Design Patterns – Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides was published in 1994. It sowed the seed for a new concept of Design Patterns in Software Engineering. This concept in today’s world has a prominent role in the software industry.

The title ‘Gang of Four(GOF)’ denotes these authors. According to them, there are a couple of principles on which design patterns are based on:

  • Program to an interface, not an implementation.
  • Favour object composition over inheritance.

Why and where do we use design patterns?

According to experts, design patterns act as a common platform for developers and are best suited to solve problems.

Standard Platform for developers:

Fixing standardised formats minimises the time taken to solve the problems. In addition, the standard terminology that the design patterns contain helps to resolve particular issues. An example of such a scenario would be a single pattern that focuses on using a single object. Most developers aware of this can easily know if the program follows a singleton pattern.

Best practices:

The formation of design patterns has taken years to build. Testing takes place in various use cases and to solve certain problems during the software development phase. The developers can make the most of these patterns to achieve the expected result quickly.

When to apply the design patterns?

The presence of design patterns is vital during the analysis and requirement phase of SDLC(Software Development Life Cycle). Furthermore, it speeds up this SDLC phase as there are tonnes of information regarding it based on experience.

Types of design patterns in Java:

The core Java design patterns are divided into three major parts:
1. Creational Design Pattern
2. Structural Design Pattern
3. Behavioural Design Pattern

1. Creational Design Pattern:

The creational design patterns focus on object creation or instantiation. They are further categorized into Class-creational patterns and object-creational patterns. The class-creational patterns adapt the concept of inheritance during the instantiation process. In contrast, the object-creational pattern uses delegation to attain the desired result.

Common creational design patterns are Factory Pattern, Abstract factory Pattern, Singleton Pattern, Prototype Pattern, Builder Pattern, and so on.

Use case of Creational design pattern:

i. Consider a scenario where a developer wants to create a simple DBConnection class for a database connection. Now the developer would wish to access the database at various locations via code. In such cases, the most common method the developer uses is to create an instance of DBConnection class and use it for database operations. Therefore, multiple connections can be established where each instance of DBConnection class will contain a separate connection to the database.

In this case, we can create a DBConnection class as a Singleton class where only one instance for DBConnection is created, and a connection is made. This paves the way to efficiently manage the DBConnection using one instance to control the load balance, unimportant connections, etc.

ii. Let’s look at another scenario:

Consider that you need multiple instances of the same kind to attain loose coupling. In such cases, we would pick the Factory pattern. A class with this pattern acts as a bridge between various classes.

During application development that uses several databases like SQL Server and Oracle, the Factory Pattern lends a helping hand. However, if the application is built using SQL Server base at the back end and requires changing it to Oracle database in the future, modification of the code would be necessary.

In such instances, applying the Factory design patterns to maintain loose coupling and easy implementation would be the right choice.

2. Structural Design Pattern:

It lets us format multiple classes with objects to create larger structures and offers new functionality.
Some structural design patterns are adapter patterns, bridge patterns, composite patterns, decorator patterns, Façade patterns, Flyweight patterns, and Proxy patterns.

Use case of Structural design pattern:

It is known as an adapter design pattern when two interfaces are incompatible and want to create a relationship via an adapter. This pattern converts the interface of a class into a different class or interface that the client expects. Therefore, the adapter pattern is preferable in such situations.

3. Behavioural Design Pattern:

Behavioural patterns include recognising and realising the common communication patterns between objects. These patterns include the Chain of Responsibility Pattern, Command Pattern, Interpreter Pattern, iterator Pattern, Mediator Pattern, Memento Pattern, State Pattern, Observer Pattern, Template Pattern, Visitor Pattern, and Strategy Pattern.

Use case for Behavioural design pattern:

The skeleton of an algorithm is based on the template pattern conceding some steps to its sub-classes of it. We can reconstruct certain algorithm steps using this method without altering the module’s behaviour. This significantly changes the project when the module’s behaviour is to be extended. In such situations where you need to modify but not restructure the code as a developer, you can use the template design pattern.

Advantages of Design Pattern in java:

1. It helps us in framing solutions for the system architecture.

2. It enhances understanding of the architecture of the system.

3. It lays out transparency to the design of the application.

4. The reusability of software architecture can take place on a large scale.

5. The main aim of design patterns is to offer a clear idea of the system architecture.

6. It paves the way for the rise of common language and jargon for programmers.

7. Expert developers testify design patterns.

Conclusion:

Design patterns have been indispensable in software development in recent years. I hope you now clearly understand various design patterns you can find to fix your software design problems.

Leave a Reply

Your email address will not be published. Required fields are marked *