State Design Pattern in Java

State pattern comes under behavior design pattern in JEE. The State pattern allows an object to alter its behavior when its internal state changes. State pattern is used to provide a systematic and lose-coupled way to achieve this through Context and State implementations. Context is the class that has a state reference to one of the concrete implementations of the state and forwards the request to the state object for processing. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements such as using switch-case or if-else statements.

The original definition of State Pattern in Gang of Four Book is given below

Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.

When should we use the State Pattern

We should use the State pattern when the behavior of an object should be influenced by it’s internal state.


A TV can be switched ON or OFF. So TV is either in ON or OFF state. A person can be in HAPPY or in EMOTIONAL state. Continue reading “State Design Pattern in Java”

Strategy Design Pattern in Java

The strategy pattern is a behavioral design pattern that enables an algorithm’s behavior to be selected at runtime without causing tight coupling. The strategy pattern defines a family of algorithms, encapsulates each algorithm, and makes algorithms interchangeable. Strategy pattern lets the algorithm vary independently from clients that use it.

For more information go to

The definition in original Gang of Four book is given below

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Continue reading “Strategy Design Pattern in Java”

Observer Design Pattern in Java

Observer Pattern is one of the behavioral patterns. Observer design pattern is useful when there is something need to be done with the state of an object and an object wants to get notified whenever there is any change in state. In observer pattern, the object that watches on the state of another object are called Observer and the object that is being watched is called Subject.

This pattern is useful when there is a one-to-many relationship between objects and when an object changes its state, all its dependent objects are notified and updated automatically.

Subject contains a list of observers to notify of any change in its state, so it should provide methods using which observers can register and unregister themselves. Subject also contains a method to notify all observers of any change in state and either it can send the update while notifying the observer or it can provide another method to get the update.

Observer should have a method to set the object to watch and another method that will be used by Subject to notify them of any update.

The definition provided in the original Gang of Four book is given below

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

The real life example of Observer Pattern is we want to publish recruitment status through three different medium such as PostCard, NewsPaper and Internet whenever an update or change occurs in the recruitment status.

The observer pattern is mainly used to implement distributed event handling systems. The Observer pattern is also a key part in the familiar model–view–controller (MVC) architectural pattern. Continue reading “Observer Design Pattern in Java”

Memento Design Pattern in Java

Memento Pattern is one of the behavioral patterns. Memento pattern is a software design pattern that provides the ability to restore state of an object to its previous state.

The memento pattern is implemented with three objects: the originator, a caretaker and a memento. The originator is an object that has an internal state. The caretaker asks the originator for a memento object. Then caretaker does something which make changes to the originator, but has the ability to undo the change. To roll back to the state before operations, it returns the memento object to the originator.

For more information please go to Continue reading “Memento Design Pattern in Java”

Mediator Design Pattern in Java

Mediator Pattern is one of the behavioral patterns as it deals with the behavior of the program. Usually a program is made up of a large number of classes. So the logic and computation are distributed among these classes. As more classes are developed in a program, the number of classes are increased, thus arising the problem of communication among these classes and it becomes much difficult to read and maintain. Therefore, the mediator pattern comes in rescue to the complexity which comes in the coding when number of classes increase.

Mediator design pattern is very helpful in an enterprise application where multiple objects are interacting with each other. Mediator design pattern is used to provide a centralized communication medium among different objects. In this pattern, objects no longer communicate directly with each other in a tightly-coupled manner that makes maintainability cost higher and not flexible to extend easily, but instead communicate through the mediator. This reduces the dependencies between communicating objects, thereby implementing the lose-coupling.

For more info you can find at Continue reading “Mediator Design Pattern in Java”

Iterator Design Pattern in Java

Iterator pattern falls under behavioral design pattern. Iterator pattern is very commonly used design pattern in Java. This pattern is used to iterate through a collection of objects without exposing its underlying representation. It is used to access the elements of an aggregate object sequentially.

For example, Java’s collections like ArrayList and HashMap have implemented the iterator pattern. Continue reading “Iterator Design Pattern in Java”

Command Design Pattern in Java

The command pattern comes under behavioral design pattern. The Command pattern is used to create objects that represent actions and events in an application. In the command pattern, a command interface declares a method for executing a particular action. A command object encapsulates an action or event and contains all information required to understand the action or event. This command object can be used as a parameter to extract information about occurred actions and events.

In the command pattern, the invoker is decoupled from the action performed by the receiver. The invoker invokes a command that executes the appropriate action of the receiver. Hence invoker is unaware of details of the action to be performed.

The command pattern can be used to perform redo/undo functionality. Continue reading “Command Design Pattern in Java”

Chain of Responsibility Design Pattern in Java

The Chain of Responsibility is known as a behavioral pattern, as it is used to manage algorithms, relationships and responsibilities between objects. The Chain of Responsibility pattern consists of a series of processing objects where each object containing the type of commands it can handle. Each object in the series either does its own task or passes to the next object if it cannot handle the request. A mechanism also exists for adding new processing objects to the end of this chain or series.

In this pattern objects in the chain are linked, typically as a one-directional linked list, with a next reference from one node to the next node. The request is passed along the chain, from one object to the next, until an object (a handler or receiver) handles it. When the request is handled, it is no longer passed on to the next level. If no object handles the request, a default object at the end of the chain can be made to handle it, or it may be that no object handles the request. Sometimes, if required, multiple objects may handle the request. Continue reading “Chain of Responsibility Design Pattern in Java”

Proxy Design Pattern in Java

The Proxy is known as a structural pattern, as it is used to form large object structures across many disparate objects. It functions as an interface to something else such as a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate.

For more information on usage or for below use cases you can refer to

Use Cases

Remote Proxy – Represents an object locally which belongs to a different address space. Take an example of ATM machine, it will hold proxy objects for bank information that exists in the remote server. Continue reading “Proxy Design Pattern in Java”

Facade Design Pattern in Java

This design pattern comes under structural pattern as this pattern adds an interface to the existing system to hide all its complexities. As the name suggests Facade means the face of the building block.

The real life examples of this pattern can be explained as explained below :

We know that Computer system starts up when we push a start button on the CPU but we do not know how that start button sends signal to other internal circuits to actually boot the computer system. So the Facade pattern just gives us a friendly start button to start up the computer and hides all internal complexities of how it happens.
We can also take an another example of starting a car. We just push the key and start the ignition switch to start the car and we are not bother about how that ignition switch (sends signal to other internal circuits) starts the car.

This is how facade pattern is used. It hides the complexities of the system and provides an interface to the client from where the client can access the system.
Design pattern of JDBC can be called as a facade pattern. Clients create connection using the java.sql.Connection interface, the implementation of which clients are not concerned about. The implementation is left to the vendor of driver.

Let’s try and understand the facade pattern better using a simple example. Let’s consider a starting mechanism of a car engine. We will see how car engine starts up once we turns the ignition switch on to the car. Continue reading “Facade Design Pattern in Java”