Chain of Responsibility – Behavioral Design Pattern

The Chain of Responsibility Pattern comes under Behavioral design pattern, the main motive of this pattern is to accomplish loose coupling in Software design process where the client request is passes to series (CHAIN) of objects to process the client request. If one object is not able to handle the request then it will pass the request to the next object (Loose coupling) and so on.

So in this chain of objects, each object is given some responsibility and once its processing is complete, that object will forward the command to the next object in the chain.

In this type of pattern there may be a possibility of many handler objects and many client requests which must be handled by these handlers. So it’s important to efficiently process the client requests.

Chain of Responsibility Pattern by Example

Generally, each company has its own website on which there will be a contact section where email id is given, which can be used by different people for different queries like some people may contact for job related things, some may contact to give feedback, some may contact for business related things Etc.

So in company there may be different departments which handles different queries like job queries can be handled by Recruitment Dept, General feedback, complaints can be handled by HR dept, Business related queries by Business Development Dept etc.

By considering above scenario we are going to write down java program to implement chain of responsibility pattern.

IMPLEMENTATION:

Whenever new Email comes our handler checks the email subject and forwards it to particular handler. It will pass from chain of handlers.

In this java program, we created 4 handlers.

  1. For Spam mail : spam mails are deleted by SpamHandler
  2. For job related mail : this mail is forwarded to RecHandler handler
  3. For general enquiry or feedback mail. : this mail forwarded to OtherMailHandler
  4. For business related mail forwarded to NewBusHandler

Here, we created handler interface where setNextChain() method is used to form a chain between different handlers and forwardEMail() method is used to forward the mail to particular dept.

In this example there are 4 chain handlers, all implement the Handler Interface. Every chain Handler checks EMail subject and so it will handle the request or it will pass it to the next handler as per the sequence set by setNextChain() method.

Lets define a simple Email class to hold the subject only for our demonstration.

Now define a interface Handler, which will be implemented by different classes

NewBusHandler

OtherMailHandler

RecHandler

SpamHandler

Running the example

Output

CONCLUSION:

This is all about Chain of Responsibility pattern, which defines a chain or list of handlers, each of handlers is able to process client request and whenever the request is submitted to this chain it is passed to the very first handler in the list to process it and pass the request along the chain until an object handles it. One thing is important regarding this pattern is that don’t use this pattern when every request is handled by only one handler.

Add a Comment

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