chain of responsibility
TRANSCRIPT
Team 13
Behavioral design pattern.
Uses a chain of objects to handle a request.
Objects in the chain forward the request along the chain until one of the objects handles the request.
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
Introduction
Class Diagram
client successor
Handler
handleRequest()
ConcreteHandler1 ConcreteHandler2
handleRequest() handleRequest()
Request
Several objects have similar methods that could be appropriate for the action that the program is requesting.
One of the objects might be most suitable.
Usage
Having new objects that want to add to the list of processing options while the program execution.
When more than one object may handle a request and the actual handler is not know in advance
Usage (cont.…)
Implementation
In brief,
We create four objects that can either “Add”, “Subtract”, “Multiply” or “Divide”.
Send two numbers and a command, that allow above four objects to decide which can handle the requested calculation.
public interface Chain
{
void calculate(Numbers request);
void setChain(Chain nextChain);
}
}
Implementation
Interface
public class Numbers {
private int _number1, _number2;
private string _command;
public Numbers(int number1, int number2, string command)
{
_number1 = number1;
_number2 = number2;
_command = command;
}
public int getNumber1() { return _number1; }
public int getNumber2() { return _number2; }
public string getCommand() { return _command; }
}
}
Implementation
Numbers Class
Public Addition : Chain {
private Chain _nextChain;
public void calculate(Numbers request){
if (request.getCommand() == "add"){
Console.WriteLine(“Result: {0}",request.getNumber1()+request.getNumber2());
}else{ _nextChain.calculate(request);}
}
public void setChain(Chain nextChain){
_nextChain = nextChain;
}
}
Implementation
Addition Class
Public Subtraction : Chain {
private Chain _nextChain;
public void calculate(Numbers request){
if (request.getCommand() == "sub"){
Console.WriteLine(“Result: {0}",request.getNumber1()-request.getNumber2());
}else{ _nextChain.calculate(request);}
}
public void setChain(Chain nextChain){
_nextChain = nextChain;
}
}
Implementation
Subtraction Class
Public Multiplication : Chain {
private Chain _nextChain;
public void calculate(Numbers request){
if (request.getCommand() == "mul"){
Console.WriteLine(“Result: {0}",request.getNumber1()*request.getNumber2());
}else{ _nextChain.calculate(request);}
}
public void setChain(Chain nextChain){
_nextChain = nextChain;
}
}
Implementation
Multiplication Class
Public Division : Chain {
private Chain _nextChain;
public void calculate(Numbers request){
if (request.getCommand() == "div"){
Console.WriteLine(“Result: {0}",request.getNumber1()/request.getNumber2());
}else{ “Unidentified Command! Please Check again...”}
}
public void setChain(Chain nextChain){
_nextChain = nextChain;
}
}
Implementation
Division Class
class Demo{
public static void Main()
{
Chain chainCalc1 = new Addition();
Chain chainCalc2 = new Subtraction();
Chain chainCalc3 = new Multiplication();
Chain chainCalc4 = new Division();
chainCalc1.setChain(chainCalc2);
chainCalc2.setChain(chainCalc3);
chainCalc3.setChain(chainCalc4);
Numbers request1 = new Numbers(10,5,"add");
Numbers request2 = new Numbers(10,5,"mul");
chainCalc1.calculate(request1);
chainCalc1.calculate(request2);
Console.ReadLine();
}
Implementation
Demo Class
Pros
More efficient
More flexible
Refactor and change the code is easy
Cons
Handling isn't guaranteed
Pros & Cons