Chain of responsibility pattern

Pattern name: Chain of responsibility

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

Family of patterns: Behavioral Patterns

Structure of patterns:

Figure: structure of chain of responsibility pattern

Handler— define an interface for handle request

Concrete handler— concrete handler implements the handler interface and handle the request it is responsible for if it can handle the request, otherwise it sends the request to its successor.

Clients— send the request to the first object of the chain that may handle the commands

When to use this pattern: this pattern is recommended when—

  •  Multiple objects can handle a request and the handler doesn’t have to be specific object
  •   A set of object should be able to handle a request with the handler determined at runtime
  •  A request  not being handled is an acceptable outcome

Downside: this pattern has mixed blessing. The request could reach the end of the chain and not be handled at all.

Real Life Example: let’s think a scenario; I have an email client which store email in different folders to store emails. If it is from Gmail, it will store in Gmail folder and if it is from business.com, it will store in business.com folder. So have different email handlers, they do basically same type of job. We can solve this problem using chain of responsibility pattern.

Code Example:

Interface Handler


package com.codexplo.patterns.behavioural.chainofresponsibility;

public interface EmailHandler {
public void setNext(EmailHandler emailHandler);

public void handleRequest(String email);
}


 

 

 

Concrete Handler 1


package com.codexplo.patterns.behavioural.chainofresponsibility;

public class GmailEmail implements EmailHandler {
private EmailHandler next;

@Override
public void setNext(EmailHandler emailHandler) {
this.next = emailHandler;
}

@Override
public void handleRequest(String email) {
if (email.contains("@gmail.com")) {
System.out
.println("Email is from gmail.com, so it will go in Gmail folder");
} else {
next.handleRequest(email);
}
}
}



Concrete Handler 2


package com.codexplo.patterns.behavioural.chainofresponsibility;

public class BusinessEmail implements EmailHandler {

private EmailHandler next;

@Override
public void setNext(EmailHandler emailHandler) {
this.next = emailHandler;
}

@Override
public void handleRequest(String email) {
if (email.contains("@business.com")) {
System.out
.println("Email is from Business.com, so it will go in Business.com Folder");
} else {
next.handleRequest(email);
}
}
}



Email Processor


package com.codexplo.patterns.behavioural.chainofresponsibility;

public class EmailProccessor {
private EmailHandler successor;
private EmailHandler first;

public void addHandler(EmailHandler handler) {
if (this.first == null) {
this.first = handler;
} else
this.successor.setNext(handler);
this.successor = handler;
}

public void handle(String email) {
first.handleRequest(email);
}
}



Email Client


package com.codexplo.patterns.behavioural.chainofresponsibility;

public class EmailClient {
private EmailProccessor proccessor;

public EmailClient() {
createProccessor();
}

private void createProccessor() {
proccessor = new EmailProccessor();
proccessor.addHandler(new BusinessEmail());
proccessor.addHandler(new GmailEmail());
}

public void emailRecieved(String email) {
proccessor.handle(email);
}

public static void main(String[] args) {
EmailClient client = new EmailClient();
client.emailRecieved("rokonoid@gmail.com");
client.emailRecieved("rokonoid@business.com");
}
}



Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s