chain of responsibility

15
Team 13

Upload: achini-perera

Post on 17-Jun-2015

881 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chain of responsibility

Team 13

Page 2: Chain of responsibility

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

Page 3: Chain of responsibility

Class Diagram

client successor

Handler

handleRequest()

ConcreteHandler1 ConcreteHandler2

handleRequest() handleRequest()

Request

Page 4: Chain of responsibility

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

Page 5: Chain of responsibility

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.…)

Page 6: Chain of responsibility

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.

Page 7: Chain of responsibility

public interface Chain

{

void calculate(Numbers request);

void setChain(Chain nextChain);

}

}

Implementation

Interface

Page 8: Chain of responsibility

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

Page 9: Chain of responsibility

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

Page 10: Chain of responsibility

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

Page 11: Chain of responsibility

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

Page 12: Chain of responsibility

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

Page 13: Chain of responsibility

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

Page 14: Chain of responsibility

Pros

More efficient

More flexible

Refactor and change the code is easy

Cons

Handling isn't guaranteed

Pros & Cons

Page 15: Chain of responsibility