As the name suggests, design patterns are solutions for the most common (and frequent) problems while designing software. These patterns are mostly “evolved” rather than “discovered“. A lot of learning by lots of professionals has been summarized into these design patterns. None of these patterns force you anything regarding implementation; they are just guidelines to solve a particular problem – in a particular way – in particular contexts. Code implementation is your responsibility.
In software engineering, a software design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations. [Wikipedia]
Given their importance, let’s learn these design patterns (in the context of Java) in more detail.
Creational Design Patterns | Structural Design Patterns | Behavioral Design Patterns |
---|---|---|
Simple Factory | Adapter | Chain of Responsibility |
Factory Method | Bridge | Command |
Abstract Factory | Composite | Iterator |
Builder | Decorator | Mediator |
Prototype | Facade | Memento |
Singleton | Flyweight | Observer |
Proxy | Visitor | |
Strategy | ||
State | ||
Template Method |
1. Creational Design Patterns
Creational patterns are often used in place of direct instantiation with constructors. They make the creation process more adaptable and dynamic. In particular, they can provide a great deal of flexibility about which objects are created, how those objects are created, and how they are initialized.
Creational patterns are focused towards how to instantiate an object or group of related objects.
Design Pattern Name | Purpose |
---|---|
Builder | The builder design pattern is an alternative way to construct complex objects. It should be used only when we want to build different types of immutable objects using the same object-building process. |
Prototype | Prototype design pattern is used in scenarios where application needs to create a large number of instances of a class, which have almost same state or differ very little. |
Factory | The factory design pattern is most suitable when complex object-creation steps are involved. It ensures that these steps are centralized and not exposed to composing classes. |
Abstract factory | The abstract factory pattern is used whenever we need another level of abstraction over a group of factories created using the factory pattern. |
Singleton | Singleton enables an application to have one and only one instance of a class per JVM. |
2. Structural Design Patterns
Structural design patterns show us how to glue different pieces of a system together in a flexible and extensible fashion. These patterns help us guarantee that the entire application structure does not need to change when one of the parts changes.
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize relationships between entities.
Structural patterns are mostly concerned with object composition or in other words how the entities can use each other.
Design Pattern Name | Purpose |
---|---|
Adapter | An adapter converts a class’s interface into another interface clients expect. It lets classes work together that couldn’t otherwise because of incompatible interfaces. |
Bridge | The bridge design pattern is used to decouple a class into two parts—abstraction and implementation—so that both can evolve in the future without affecting each other. It increases the loose coupling between class abstraction and implementation. |
Composite | The composite design pattern helps compose objects into tree structures to represent whole-part hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. |
Decorator | The decorator design pattern is used to add additional features or behaviors to a particular instance of a class without modifying the other instances of the same class. |
Facade | The facade design pattern provides a unified interface to a set of interfaces in a subsystem. It defines a higher-level interface that makes the subsystem easier to use. |
Flyweight | The flyweight design pattern enables the efficient use of shared objects to support large numbers of fine-grained objects. A flyweight is a shared object that can be used in multiple contexts simultaneously. It acts as an independent object in each context. |
Proxy | In the proxy design pattern, a proxy object provides a surrogate or placeholder for another object to control access to it. Proxy is heavily used to implement lazy loading use usecases where we do not want to create a complete object until it is needed. |
3. Behavioral Design Patterns
Behavioral patterns abstract an action we want to take on the object or class that takes the action. By changing the object or class, we can change the algorithm used, the objects affected, or the behavior while still retaining the same basic interface for client classes.
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns.
These patterns don’t just specify the structure but also outline the patterns for message passing/communication between them.
Design Pattern Name | Purpose |
---|---|
Chain of responsibility | The chain-of-responsibility design pattern gives more than one object an opportunity to handle a request by linking receiving objects together in a chain. |
Command | The command design pattern is helpful for abstracting the business logic into discrete actions, which we call commands. These command objects help in loose coupling between two classes, where one class (invoker) calls a method on the other class (receiver) to perform a business operation. |
Interpreter | The interpreter pattern specifies how to evaluate sentences programmatically in a language. It helps build the grammar for a simple language so that sentences can be interpreted. |
Iterator | The iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. |
Mediator | The mediator pattern defines an object that encapsulates how a set of objects interact. It promotes loose coupling by keeping objects from referring to each other explicitly and letting us vary their interaction independently. |
Memento | The memento pattern, also known as the snapshot pattern, is used to restore an object’s state to a previous state. |
Observer | The observer pattern defines a one-to-many dependency between objects so that all its dependents are notified and updated automatically when one object changes state. It is also referred to as the publish-subscribe pattern. |
State | In state pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class. There shall be a separate concrete class per possible state of an object. |
Strategy | A strategy pattern is used where we choose a specific implementation of an algorithm or task in run time out of multiple other implementations for the same task. |
Template method | The template method pattern defines the sequential steps to execute a multi-step algorithm and can optionally provide a default implementation as well (based on requirements). |
Visitor | The visitor pattern is used when we want to modify the behavior of a hierarchy of objects without modifying their source code. |
Happy Learning !!
Very good information really good starting for the learners.
And Facade, observer too.
Hi Lokesh ,
Awesome blog . But could you include brief description of Strategy pattern with examples .
Awesome Blog Lokesh. Thanks ! Simple and Best. Really helpful. Please update remaining TODO soon.
Hi,
Very clear information. Really helpful to understand the Design patterns with real time examples.
Please update the remaining TODO