🏠JavaDesign Patterns in Java

Design Patterns in Java

Java

In software engineering, Design patterns in java are the solution to write better applications. They are the best practices to solve certain problems in various situations. And These patterns are not algorithms. And they are definitely not a programming paradigm. But, it is just an approach to building concrete programs.

In 1994 Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published the book Design Patterns: Elements of Reusable Object-Oriented Software. We now know these authors as the Gang of Four (GoF).

This book goes on to explain various approaches to object-oriented programming. In those approaches, two instructions stand out. They are,

  1. Always program to interfaces, not implementation.
  2. Do not reuse code through inheritance (object composition over inheritance).

In this series of tutorials, We will go through each java design pattern in detail.

Types of Design patterns in Java

As per the Gang of four(GOF) there are 23 patterns. And we can classify them in to the following 3 types.

Creational Patterns

To begin with, The Creational patterns deal with the object creation based on the situation. When basic object creation causes added complexity to the design, these patterns deal with it by controlling the object creation.

Some well known creational patterns are,

  1. Factory method pattern
  2. Abstract factory pattern
  3. Singleton pattern
  4. Builder pattern
  5. Prototype pattern

Structural Design Patterns

Further, We have the Structural patterns take care of class and object composition. These patterns use inheritance to create proxies and adapters that allow one class to used as another interface. Some important patterns in this category are,

  1. Adapter pattern
  2. Bridge pattern
  3. Composite pattern
  4. Decorator pattern
  5. Facade pattern
  6. Proxy Pattern
  7. Flyweight pattern

Behavioral Patterns

At last, We have the Behavioral Patterns that deal with communication between objects and their behaviour in the system. They provide approach to adding new features.

  1. Command pattern
  2. Interpreter pattern
  3. Chain of Responsibility pattern
  4. Iterator pattern
  5. Mediator pattern
  6. Strategy pattern
  7. Memento pattern
  8. Observer pattern
  9. State pattern
  10. Visitor pattern
  11. Template method Pattern

Why use design patterns?

Let’s go through the reasons why you should use design patterns in your java applications.

Don’t reinvent the wheel

Design patterns are hard earned experiences. That is, There is almost always a design pattern to solve a design problem. So it is easier for developers to find the right solution for a problem

Fewer Changes

All software development projects are susceptible to change. That is, As changes directly correlate with amount of effort and money, It makes sense to design the system that changes very little. Most of the behavioural patterns help deal with changes in the project. These patterns can introduce new behaviours to the project without huge effort from the developers.

Recognizable code

Once you master the design patterns, you can easily understand anyone else’s code(if they use design patterns). This advantage is crucial when the developers need to work with third party libraries. After all, the best documentation for the code is the code itself. To conclude, the patterns help you understand and write better code.

Similar Posts

Leave a Reply

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