Java Design Patterns?

In this article, we are going to understand what is a design pattern and popular Java Design Patterns, the GoF Design Patterns, what makes them so important to software components design, and what are the different categories of the design patterns in terms of Creational, Structural, and Behavioral design patterns.

1. Java Design Patterns?

The Design Patterns are solutions to software’s most commonly and frequently occurred problems. We apply these patterns when we are designing software. We can think of them as blueprints that we can customize to solve a design problem in our software. These patterns came to existence after great work by lots of software professionals who summarized their learning of how to solve software design challenges.

The Design Patterns are not code or library, they are guidelines to solve a particular problem in a particular way and a particular context. They are not forced and one can choose not to use them. We have to analyze our design problems and see if the list of patterns can solve them. Many programmers use design patterns without even noticing them.

Important to note that the Design Patterns and the Algorithms are two different concepts. While an algorithm defines a clear set of instructions to solve a problem, a design pattern is a description of a blueprint that can enhance your software. The Design Patterns usually are not associated with a particular language and can be applied to any software language.

2. What does the Design Pattern consist of?

The Design Patterns have comprised a problem statement and a solution to it, it also explains how the solution can effectively solve the problem. Each of the upcoming design patterns will explain the problem at hand and how the pattern can help find the solution to it. The design pattern will also show the class/sequence diagrams to show how these interfaces and classes are related to each other and how can they work together to find a solution.

Last, however not least, it will also have a code example to make the entire understanding easier for the readers. In some places, the design patterns will also give the example of a particular language and their libraries/APIs where these patterns have been implemented. For example, the Builder (creational) design pattern has been widely used in Java and we can take StringBuilder as an example.

3. Why Should I Learn Design Patterns?

As we have mentioned earlier in this article, design patterns are optional and many programmers work just fine knowing no patterns. They may accidentally use the patterns, however they are not aware. So why would any programmer spend his/her time learning the design patterns? Foremost, the design patterns solutions are well-tried, well-established, and well-tested to a variety of common software design problems. Knowing these principles in object-oriented design development will only help you create a design that is futuristic and future changes to your design are simple. (Read open-close principle). So, it will only help you design.

Second, software designs, which are based on design patterns, support re-usability, are more robust and easily maintainable. So, you save cost here. Thirdly, when we design patterns, our code becomes easy to understand and debug, this helps in faster development, and on-boarding a new team member becomes very easy.

Last, it is a common language that all the programmers understand, say, you can easily tell your team to implement a “Factory Design Pattern” for the problem statement and they will understand.

4. Classification of Design Patterns

There are three major categories in Java design patterns. We are going to discuss the categories and actual design patterns belonging to them.

4.1. Creational Design Patterns

Creational patterns are used to create the objects for class. We use them instead of direct instantiation with class constructors. These patterns make creating objects very dynamic and creation can happen at run-time based on the input value. They provide the highest flexibility in how the objects would be created and initialized.

Design Pattern NamePurpose
SingletonWe use Singleton design pattern when an application wants to have a unique instance of a class per JVM.
FactoryWe used factory design pattern when we need to create objects requiring complex steps. It centralized the creation of objects.
Abstract FactoryAbstract Factory design pattern is used when we need another layer of abstraction on the top on factory design pattern, we also called it as a factory of factory pattern.
BuilderBuilder design pattern is like factory design pattern to create complex objects. We used it when we want to build different types of immutable objects the using same object building process.
PrototypePrototype design pattern is used when an application needs to create a huge number of objects which have the same state or they differ very little.
Creational Design Patterns

4.2. Structural Design Patterns

Structural design patterns help the software design to provide solutions on how to club the different pieces of an application together. It provides flexibility and extensibility. The major benefit of using these patterns is that the programmer can change the structure of one individual piece without affecting the structure of the entire application.

Design Pattern NamePurpose
AdapterAdapter design pattern is used to convert the interface of a class into another interface that the clients expect. It wouldn’t be possible because of incompatible interfaces.
CompositeComposite design pattern is used to compose the objects to represent a part-whole hierarchy.
ProxyProxy design pattern is used to provide a placeholder for another object to control access to it. It provides controlled access to functionality.
FlyweightWe use flyweight design pattern when we need to create multiple objects of a class. To save the memory footprint on low-memory devices, this pattern can be applied by sharing objects.
FacadeFacade design pattern is used to create a unified interface for the client interacting with the application with a set of interfaces.
BridgeBridge design pattern is used when we have interface hierarchies in both the interfaces and in the implementations. Then this pattern is used to decouple the interfaces from the implementation and hide the implementation details from the client programs.
DecoratorDecorator design pattern is used to change the functionality of an object at runtime.
Structural Design Patterns

4.3. Behavioral Design Patterns

Behavioral design patterns help the application by abstracting the action that we want to take on classes or objects. It also provides the solution on objects that can interact with each other in a much better way by providing loose coupling.

Design Pattern NamePurpose
Template MethodTemplate Method design pattern is used to define the steps to execute an algorithm and it can provide a default implementation that might be common for all or some of the sub-classes.
MediatorMediator design pattern is used to provide a centralized communication medium between different objects in an enterprise application. It is of extreme use if there are many objects and they are interacting with each other.
Chain of ResponsibilityChain of responsibility design pattern is used when a client request can be passed to a chain of objects to process them. Thus, providing loose coupling.
ObserverWe use observer design pattern when an object is interested in the state of another object and wants to get notified whenever the other object’s state changes. The object which observes the state of another object is called the Observer and the object that is being watched the Subject.
StrategyStrategy design pattern is used an application has multiple algorithms for a specific problem and the client application decides the actual algorithm be used at runtime.
CommandCommand design pattern is used to implement loose coupling between two classes where one class (invoke) shall call a method on the other class (receiver) to perform a business operation in a request-response model.
StateState design pattern is used when an Object changes its behavior based on its internal state and the application takes a decision based on this change of state of that object.
VisitorVisitor design pattern is used when an application has to operate on a group of similar kinds of Objects. This way application can move the operational logic to outside the objects.
IteratorIterator design pattern is used to provide a standard way to traverse through a group of Objects. It is widely used in Java Collection classes.
MementoMemento design pattern is used when we want to save the state of an object so that we can restore it later.
Behavioral Design Patterns

Summary

The design patterns make the software designing process more organized and future-ready. They also help the code be more robust and more reusable. If a team uses the design patterns properly, they will save a lot of time and money, as the entire development is very easy. It also helps new people joining the project to come to speed fast.