Searching...
Wednesday, 28 May 2014

Chain of responsibility design pattern example

11:26

Chain of responsibility is one of the behavioral design pattern. It describes how can we handle a single request by a chain of multiple handler objects. The current handler is able to process the request then the request will be processed otherwise it will push to next handlers until the request is processed.

Let us take an example of a Parcel service and this service has branches in Bangalore, Bombay and Delhi. The routes are defined like as follows

Bangalore –> Bombay –> Delhi 

In case i am sending a parcel to Delhi, first send to Bombay branch and from there it will send to Delhi.

1. Create a Chain interface

package com.pretech.design.chain;
public interface Chain {
	 public abstract void setNextDestination(Chain nextDestination);
	 public abstract void processParcel(Parcel parcel);
}

2. Create handler (Bangalore)

package com.pretech.design.chain;
public class Bangalore implements Chain {
	private Chain nextDestination;
	public void setNextDestination(Chain nextDest) {
		nextDestination = nextDest;
	}
	public void processParcel(Parcel parcel) {
		if (parcel.getDestination().equalsIgnoreCase("Bangalore")) {
			System.out.println("Processing parcel in bangalore :" + parcel);
		}
		if (parcel.getDestination().equalsIgnoreCase("Bombay")
				|| parcel.getDestination().equalsIgnoreCase("Delhi")) {
			nextDestination.processParcel(parcel);
		}
	}
}

3. Create handler (Bombay)

package com.pretech.design.chain;
public class Bombay implements Chain {
	private Chain nextDestination;
	public void setNextDestination(Chain nextDest) {
		nextDestination = nextDest;
	}
	public void processParcel(Parcel parcel) {
		if (parcel.getDestination().equalsIgnoreCase("Bombay")) {
			System.out.println("Processing parcel in Bombay :" + parcel);
		}
		if (parcel.getDestination().equalsIgnoreCase("Delhi")) {
			nextDestination.processParcel(parcel);
		}
	}
}

4. Create handler (Bangalore)

package com.pretech.design.chain;
public class Delhi implements Chain {
	private Chain nextDestination;
	public void setNextDestination(Chain nextDest) {
		nextDestination = nextDest;
	}
	public void processParcel(Parcel parcel) {
		if (parcel.getDestination().equalsIgnoreCase("Delhi")) {
			System.out.println("Processing parcel in Delhi :" + parcel);
		}
	
	}
}

  5. Create Parcel object

package com.pretech.design.chain;
public class Parcel {
	private String source;
	private String destination;
	private String details;
	public String getSource() {
		return source;
	}
	public void setSource(String source) {
		this.source = source;
	}
	public String getDestination() {
		return destination;
	}
	public void setDestination(String destination) {
		this.destination = destination;
	}
	public String getDetails() {
		return details;
	}
	public void setDetails(String details) {
		this.details = details;
	}
	@Override
	public String toString() {
		return "Parcel [source=" + source + ", destination=" + destination
				+ ", details=" + details + "]";
	}
	
}

6. Create Responsibilities (ParcelService.java)

package com.pretech.design.chain;
public class ParcelService {
	public static void main(String[] args) {
		Chain bangalore = new Bangalore();
		Chain bombay = new Bombay();
		Chain delhi = new Delhi();
		bangalore.setNextDestination(bombay);
		bombay.setNextDestination(delhi);
		Parcel p1 = new Parcel();
		p1.setDestination("Bombay");
		p1.setDetails("Parcel to Bombay");
		Parcel p2 = new Parcel();
		p2.setDestination("Delhi");
		p2.setDetails("Parcel to Delhi");
		bangalore.processParcel(p1);
		bangalore.processParcel(p2);
	}
}

Output



Processing parcel in Bombay :Parcel [source=null, destination=Bombay, details=Parcel to Bombay]
Processing parcel in Delhi :Parcel [source=null, destination=Delhi, details=Parcel to Delhi]


0 comments: