Your SlideShare is downloading. ×
0
Team 13
Introduction                   Behavioral design pattern. Uses a chain of objects to handle a request. Objects in the ...
Class Diagram                                               successorclient                         Handler            Re...
Usage                       Several objects have similar methods that could be  appropriate for the action that the prog...
Usage (cont.…)                 Having new objects that want to add to the list of  processing options while the program ...
Implementation                  In brief, We create four objects that can either “Add”,  “Subtract”, “Multiply” or “Divi...
Implementation                Interface  public interface Chain      {           void calculate(Numbers request);       ...
Implementation Numbers Class                public class Numbers {        private int _number1, _number2;        private...
Implementation Addition Class                 Public Addition : Chain {      private Chain _nextChain;      public void ...
Implementation Subtraction Class                    Public Subtraction : Chain {       private Chain _nextChain;       p...
Implementation Multiplication Class                       Public Multiplication : Chain {       private Chain _nextChain...
Implementation Division Class                 Public Division : Chain {       private Chain _nextChain;       public voi...
Implementation Demo Class class Demo{        public static void Main()        {            Chain chainCalc1 = new   Addi...
Pros & Cons                    Pros      More efficient      More flexible      Refactor and change the code is easy Co...
Chain of responsibility
Upcoming SlideShare
Loading in...5
×

Chain of responsibility

892

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
892
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Chain of responsibility"

  1. 1. Team 13
  2. 2. Introduction  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.
  3. 3. Class Diagram  successorclient Handler Request handleRequest() ConcreteHandler1 ConcreteHandler2 handleRequest() handleRequest()
  4. 4. Usage  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.
  5. 5. Usage (cont.…)  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
  6. 6. 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.
  7. 7. Implementation  Interface public interface Chain { void calculate(Numbers request); void setChain(Chain nextChain); } }
  8. 8. Implementation Numbers Class 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; } }}
  9. 9. Implementation Addition 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; } }
  10. 10. Implementation Subtraction 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; } }
  11. 11. Implementation Multiplication 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; } }
  12. 12. Implementation Division 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; } }
  13. 13. Implementation Demo 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(); }
  14. 14. Pros & Cons  Pros More efficient More flexible Refactor and change the code is easy Cons Handling isnt guaranteed
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×