-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
94 lines (79 loc) · 2.11 KB
/
Program.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
var maxCustomerCountHandle = new MaxCustomerCountHandler();
var dueDateHandle = new DueDateHandler();
var startDateHandle = new StartDateHandle();
var creditBalanceHandle = new CreditBalanceHandle();
maxCustomerCountHandle
.setNext(dueDateHandle)
.setNext(startDateHandle)
.setNext(creditBalanceHandle);
List<String> customers = new List<string>()
{
"Customer A", "Customer B", "Customer C", "Customer D"
};
var processor = new Processor(maxCustomerCountHandle);
processor.Start(customers);
interface Handler
{
Handler setNext(Handler? handler);
Object handle(String message);
}
abstract class AbstractHandler : Handler
{
private Handler _handler;
public Handler setNext(Handler handler)
{
return _handler = handler;
}
public virtual Object handle(String message)
{
if (_handler == null) return null;
return _handler.handle(message);
}
}
sealed class MaxCustomerCountHandler : AbstractHandler
{
public override object handle(string message)
{
Console.WriteLine($"Max customer count validation handled for {message}");
return base.handle(message);
}
}
sealed class DueDateHandler : AbstractHandler
{
public override object handle(string message)
{
Console.WriteLine($"End date validation handled for {message}");
return base.handle(message);
}
}
sealed class StartDateHandle : AbstractHandler
{
public override object handle(string message)
{
Console.WriteLine($"Start date validation handled for {message}");
return base.handle(message);
}
}
sealed class CreditBalanceHandle : AbstractHandler
{
public override object handle(string message)
{
Console.WriteLine($"Credit balance handled for {message}");
return base.handle(message);
}
}
sealed class Processor
{
private readonly AbstractHandler _handler;
public Processor(AbstractHandler handler)
{
_handler = handler;
}
public void Start(List<String> messages)
{
foreach (string message in messages)
{
_handler.handle(message);
}
}
}