Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to implement Chain Of Responsibility Design Pattern in Java using simple example ?.

Program to implement Chain Of Responsibility Design Pattern in Java using simple example.

1. Handler.java
package com.hubberspot.designpatterns.chainofresponsibility;

// The chain of responsibility Design Pattern has a
// group of objects that are able to solve a problem
// between them based on some internal communication.
// If one of the object is unable to solve it, it passes
// the problem to the next object in the chain

// Lets create a Handler interface which has two methods 
public interface Handler {

	// It passes the data to next Handler in chain 
	// if one fails to handle the problem
	public void nextHandler(Handler nextHandler);
	
	// It processes the request and if it fails it just passes
	// it to next Handler in the chain.
	public void processRequest(Numbers request);

}



2. Numbers.java
package com.hubberspot.designpatterns.chainofresponsibility;

// This class has three instance variables
// number1 and number2 are operands and based
// on the String operation respective operation
// on them will be performed.
public class Numbers {

	private int number1;
	private int number2;
	private String operation;

	// Constructor for initializing the values
	public Numbers(int number1, int number2, String operation) {
		super();
		this.number1 = number1;
		this.number2 = number2;
		this.operation = operation;
	}

	// Getters and Setters for all the properties
	public int getNumber1() {
		return number1;
	}
	public void setNumber1(int number1) {
		this.number1 = number1;
	}
	public int getNumber2() {
		return number2;
	}
	public void setNumber2(int number2) {
		this.number2 = number2;
	}
	public String getOperation() {
		return operation;
	}
	public void setOperation(String operation) {
		this.operation = operation;
	}
}



3. Addition.java
package com.hubberspot.designpatterns.chainofresponsibility;

public class Addition implements Handler {

	// It defines basically next handler in chain
	// if this operation doesn't process the request
	public Handler nextHandler;

	@Override
	public void nextHandler(Handler nextHandler) {

		this.nextHandler = nextHandler;

	}

	// It processes the requested operation and if it
	// fails to process based on operation string it 
	// passes the responsibility of processing request
	// to nextHandler in chain as provided by setter above.
	@Override
	public void processRequest(Numbers request) {

		if(request.getOperation() == "ADD") {
			int number1 = request.getNumber1();
			int number2 = request.getNumber2();
			int sum = number1 + number2;
			System.out.println(number1 + " + " + number2 + " = "+ sum );
		} 
		else {

			nextHandler.processRequest(request);

		}

	}

}



4. Subtraction.java
package com.hubberspot.designpatterns.chainofresponsibility;

public class Subtraction implements Handler {

	// It defines basically next handler in chain
	// if this operation doesn't process the request
	public Handler nextHandler;

	@Override
	public void nextHandler(Handler nextHandler) {

		this.nextHandler = nextHandler;

	}

	// It processes the requested operation and if it
	// fails to process based on operation string it 
	// passes the responsibility of processing request
	// to nextHandler in chain as provided by setter above.
	@Override
	public void processRequest(Numbers request) {

		if(request.getOperation() == "SUBTRACT") {
			int number1 = request.getNumber1();
			int number2 = request.getNumber2();
			int subtract = number1 - number2;
			System.out.println(number1 + " - " + number2 + " = "+ subtract );
		} 
		else {

			nextHandler.processRequest(request);

		}

	}

}


5. Multiply.java
package com.hubberspot.designpatterns.chainofresponsibility;

public class Multiply implements Handler {

	// It defines basically next handler in chain
	// if this operation doesn't process the request
	public Handler nextHandler;

	@Override
	public void nextHandler(Handler nextHandler) {

		this.nextHandler = nextHandler;

	}

	// It processes the requested operation and if it
	// fails to process based on operation string it 
	// passes the responsibility of processing request
	// to nextHandler in chain as provided by setter above.
	@Override
	public void processRequest(Numbers request) {

		if(request.getOperation() == "MULTIPLY") {
			int number1 = request.getNumber1();
			int number2 = request.getNumber2();
			int multiply = number1 * number2;
			System.out.println(number1 + " * " + number2 + " = "+ multiply );
		} 
		else {

			nextHandler.processRequest(request);

		}

	}

}



6. Division.java
package com.hubberspot.designpatterns.chainofresponsibility;

public class Division implements Handler {

	// It defines basically next handler in chain
	// if this operation doesn't process the request
	public Handler nextHandler;

	@Override
	public void nextHandler(Handler nextHandler) {

		this.nextHandler = nextHandler;

	}

	// It processes the requested operation and if it
	// fails to process based on operation string it 
	// passes the responsibility of processing request
	// to nextHandler in chain as provided by setter above.
	@Override
	public void processRequest(Numbers request) {

		if(request.getOperation() == "DIVISION") {
			int number1 = request.getNumber1();
			int number2 = request.getNumber2();
			int division = number1 / number2;
			System.out.println(number1 + " / " + number2 + " = "+ division );
		} 
		else {

			System.out.println("Request cant get processed by any of them ... :( ");

		}

	}

}



7. TestThePattern.java
package com.hubberspot.designpatterns.chainofresponsibility;

public class TestThePattern {

	public static void main(String[] args) {

		// Lets instantiate all the objects in the chain
		Handler handler1 = new Addition();
		Handler handler2 = new Subtraction();
		Handler handler3 = new Multiply();
		Handler handler4 = new Division();

		// Lets assign a every object created above in chain
		// that is to tell each object to where forward the 
		// request if it fails to process the request

		// Here Addition handler passes the request to
		// Subtraction handler if it can't process it. 
		// And Subtraction handler passes the request to
		// Multiply handler if it can't process it.
		// and finally Multiply handler passes the request to
		// Division handler if it can't process it. 

		handler1.nextHandler(handler2);
		handler2.nextHandler(handler3);
		handler3.nextHandler(handler4);

		// Lets create Numbers object by passing two integers
		// and the operation we want to perform on them

		// Addition
		Numbers request = new Numbers(10 , 2 , "ADD");
		handler1.processRequest(request);

		// Subtraction 
		request = new Numbers(10 , 2 , "SUBTRACT");
		handler1.processRequest(request);

		// Multiply
		request = new Numbers(10 , 2 , "MULTIPLY");
		handler1.processRequest(request);

		// Division
		request = new Numbers(10 , 2 , "DIVISION");
		handler1.processRequest(request);

		// Modulus
		request = new Numbers(10 , 2 , "MODULUS");
		handler1.processRequest(request);


	}

}



Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot