Chain Of Responsibilities Pattern Implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Sometimes we may come across a situation where there should not be direct coupling between sender and receiver of a request. Consider scenario where sender of request does not have any knowledge about who is going to handle the request. Chain of responsibilities pattern implementation comes to rescue here.

Chain Of Responsibilities Pattern Implementation:

Gang of Four defines chain of responsibilities design pattern as

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

Class Diagram:

Chain of Responsibilities Class Diagram

Chain of Responsibilities Class Diagram

Components in diagram are:

  • Handler: An abstract class which defines request handling behaviour which handles request
  • ConcreteHandlers: Define more specific behaviour for handling request, if they can’t handle it, they will simply call superclass handle method.
  • Client: Instantiates all concrete handlers and chains them dynamically.

Chain of responsibilities implementation maintains a chain of all objects which are capable of handling requests. This chain is created an maintained by client. If the request is passed to a handler and it does not know how to handle it, then it is passed along the chain by some way. Here we are doing it by calling super.handleRequest().

Important points:

  • Base class Handler has reference to next handler which itself is of type Handler.
  • Each concrete handler implements its own behaviour of handling request.
  • If request needs to be passed down the chain, concrete handler calls base class behaviour.
  • The client creates a request and leaves to the chain. Any one concrete handler handles it.

Chain of Responsibilities Pattern Implementation Example:

Consider a leave request is applied and its approval is done by different roles based on number of days.

  • If 5 days, then it is approved by supervisor.
  • If 5 to 15 days, then it is approved by manager.
  • If more than 15 days, then it is approved by account head.

We will create our classes as below.

LeaveRequest.java

Approver.java

Supervisor.java

Manager.java

AccountHead.java

LeaveSystem.java

Points to notice:

  • LeaveRequest is the actual request object that will be created by client and passed to chain of responsibilities.
  • Approver is an abstract class which has protected variable nextApprover, which will hold reference to next responsibility handler in the chain.
  • SuperVisor, Manager and AccountHead are concrete subclasses of Approver, which provide their own specific behaviour based on our conditions for leave approval. If they can’t handle LeaveRequest, they simply call super.approveRequest(request), which in turn calls method from nextApprover.
  • LeaveSystem is actual client which instantiates all responsibilities and create a chain by setting next approver for each responsibility.

When you run this program, you will get below output.

Leave request approved for 4 days by Supervisor
Leave request approved for 40 days by AccountHead
Leave request approved for 12 days by Manager

Class diagram for chain of responsibilities pattern implementation is as below.

Chain of Responsibilites example

Chain of Responsibilites Example

I hope the article helped understand Chain of Responsibilities Pattern Implementation. In further articles we will see other behavioural design patterns.

Share Button

Leave a Reply

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