Design Patterns in Java

Design pattern is a language independent strategy for solving common object-oriented design problem. It describes how to structure classes to meet a given requirement. Design patterns are generally set of standardized practices used in the software development industry to solve a specific problem.
Design Patterns represent the solutions given by the community to general problems faced in every-day tasks regarding software development. Design patterns are tried and tested way to solve particular design issues by various programmers in the world.

Design patterns are elegant solutions to the repeating problems.

Design patterns are mainly categorized in 3 different categories. (Refer below chart)

Design Patterns

Singleton

Singleton design pattern ensures that a class has only one instance available per JVM and provides global point of access to it.

Factory

Factory design pattern helps in creating the Object without exposing the creation logic to the client.

Abstract Factory

Abstract Factory Design Pattern is similar to Factory design pattern but it provides additional layer of abstraction over factory pattern.

Builder

Builder pattern helps in Separating the construction of a complex object from its representation so that the same construction process can be used to create different representations.

Prototype

When Object creation is a heavy process, Prototype design pattern helps in creating the Object by copying the existing object and modify properties after Object creation if needed.

Adapter

Adapter design pattern helps in making two incompatible interfaces work together.

Composite

Composite design pattern modifies the structure of an object. It is most suitable in cases where we need to work with objects which form a tree like hierarchy.

Proxy

Proxy design pattern helps in controlling and managing access to the object they are protecting.

Fly Weight

Flyweight design pattern is used when we need to create lot of Objects of a same class. It’s a bad idea to create lot many objects of a same class when the required object count is huge, flyweight design pattern can be applied to reduce the load on memory by sharing objects.

Facade

Facade design pattern helps in providing unified interface from a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Bridge

The Bridge design pattern allows us to separate the abstraction from its implementation.

Decorator

Decorator design pattern helps in modifying the functionality of an object at runtime without affecting other Objects of the same class.

Template Method

Template method design pattern helps in defining the steps to execute an algorithm and it can provide default implementation that is treated as template and might be common for all or some of the subclasses.

Mediator

Mediator desing pattern helps in establishing loosely coupled communication between objects and reduces the direct references to each other.

Chain of Responsibility

Chain of responsibility pattern helps in achieving loose coupling where a request from the client is passed to a chain of objects to process them.

Observer

Observer design pattern defines a one-to-many dependency between objects so that when one object state changes, all its dependents are notified and updated automatically.

Strategy

Strategy Design Pattern helps us when we have multiple algorithms to accomplish a specific task and client decides the actual implementation to be used at runtime.

Command

Command design pattern is used to separate a request for an action from the object which actually performs the action.

State

State design pattern helps in changing the Object’s behavior based on its internal state.

Visitor

Visitor pattern helps us when when we have to perform an operation on a group of similar type of Objects. Operational logic can be moved from one type of Object to another types of Objects.

Iterator

Iterator design pattern helps in traversing the collection of objects in a defined manner

Interpreter

Interpreter pattern helps us to define a grammatical representation for a language and provides an interpreter to deal with this grammar.

Momento

Memento design pattern helps us to save the state of an object for later reuse.

Author: Mahesh

Technical Lead with 10 plus years of experience in developing web applications using Java/J2EE and web technologies. Strong in design and integration problem solving skills. Ability to learn, unlearn and relearn with strong written and verbal communications.