-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChainofResponsibility.cs
149 lines (135 loc) · 5.04 KB
/
ChainofResponsibility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* -------------------------------------------------
* Behaviour Pattern ---- Chain of Responsibility
* -------------------------------------------------
* Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
* Chain the receiving objects and pass the request along the chain until an object handles it.
* This pattern has 3 components - A handler, Concrete Handlers, Client
*
* 1. Handler : This can be an interface or an abstract class which recieves a request and dispatches the request to the chain of handlers.
* It refers to ONLY THE FIRST handler in the chain and does not know anything about rest of the handlers.
* 2. Concrete handlers : These are the actual handlers of the request chained in a sequential order. The order is determined by THE CLIENT.
* 3. Client : Client is the originator of a request and accesses the handler to handle it.
*/
using System;
namespace DesignPatternPlayground
{
//1. Handler
interface IArithmaticOperation
{
public void setNext(IArithmaticOperation nextInChain);
public void Operation(Request request);
}
// Context
class Request
{
private int _num1, _num2;
public Request(int num1, int num2)
{
_num1 = num1;
_num2 = num2;
}
public int getNumberOne()
{
return _num1;
}
public int getNumberTwo()
{
return _num2;
}
}
//2. Concrete Handler One
class AdditionOperation : IArithmaticOperation
{
private static Request _request;
private static IArithmaticOperation _nextinchain;
public void setNext(IArithmaticOperation nextInChain)
{
_nextinchain = nextInChain;
}
public void Operation(Request request)
{
_request = request;
Console.WriteLine($"The sum of {request.getNumberOne()} and {request.getNumberTwo()} is : {request.getNumberOne() + request.getNumberTwo()}");
_nextinchain?.Operation(_request);
}
}
//2. Concrete Handler Two
class SubtractionOperation : IArithmaticOperation
{
private static Request _request;
private static IArithmaticOperation _nextinchain;
public void setNext(IArithmaticOperation nextInChain)
{
_nextinchain = nextInChain;
}
public void Operation(Request request)
{
_request = request;
Console.WriteLine($"The difference between {request.getNumberOne()} and {request.getNumberTwo()} is : {request.getNumberOne() - request.getNumberTwo()}");
_nextinchain?.Operation(_request);
}
}
//2. Concrete Handler Three
class MultiplicationOperation : IArithmaticOperation
{
private static Request _request;
private static IArithmaticOperation _nextinchain;
public void setNext(IArithmaticOperation nextInChain)
{
_nextinchain = nextInChain;
}
public void Operation(Request request)
{
_request = request;
Console.WriteLine($"The product of {request.getNumberOne()} and {request.getNumberTwo()} is : {request.getNumberOne() * request.getNumberTwo()}");
_nextinchain?.Operation(_request);
}
}
//2. Concrete Handler Four
class DivisionOperation : IArithmaticOperation
{
private static Request _request;
private static IArithmaticOperation _nextinchain;
public void setNext(IArithmaticOperation nextInChain)
{
_nextinchain = nextInChain;
}
public void Operation(Request request)
{
_request = request;
Console.WriteLine($"The ratio of {request.getNumberOne()} and {request.getNumberTwo()} is : {request.getNumberOne() / request.getNumberTwo()}");
_nextinchain?.Operation(_request);
}
}
//3. Client
public class Arithmaticcalculator
{
private static int _num1, _num2;
public Arithmaticcalculator(int num1, int num2)
{
_num1 = num1;
_num2 = num2;
}
public void operationhandler()
{
IArithmaticOperation addition = new AdditionOperation();
IArithmaticOperation subtraction = new SubtractionOperation();
IArithmaticOperation multiplication = new MultiplicationOperation();
IArithmaticOperation division = new DivisionOperation();
addition.setNext(subtraction);
subtraction.setNext(multiplication);
multiplication.setNext(division);
Request request = new Request(_num1, _num2);
addition.Operation(request);
}
}
// Calls the client
public class ChainofResponsibility
{
public static void MainCaller()
{
Arithmaticcalculator calculator = new Arithmaticcalculator(10, 4);
calculator.operationhandler();
}
}
}