-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCorPatternComplexExample.cs
More file actions
80 lines (68 loc) · 4.1 KB
/
CorPatternComplexExample.cs
File metadata and controls
80 lines (68 loc) · 4.1 KB
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
using GofPatterns.Behavioral.ChainOfResponsibilityPattern.Orchestrators;
using GofPatterns.Behavioral.ChainOfResponsibilityPattern.Orchestrators.Complex;
using GofPatterns.Behavioral.ChainOfResponsibilityPattern.Responsibilities.Implementations;
using static GofPatterns.Behavioral.ChainOfResponsibilityPattern.Orchestrators.Complex.Enums.ChainOrchestratorHandleOptions;
using static GofPatterns.Behavioral.ChainOfResponsibilityPattern.Orchestrators.Complex.Enums.ChainOrchestratorInvokeNextOptions;
using ResponsibilityBar = GofConsoleApp.Examples.Behavioral.CorPattern.InputExampleComponents.ResponsibilityBar;
using ResponsibilityFoo = GofConsoleApp.Examples.Behavioral.CorPattern.InputExampleComponents.ResponsibilityFoo;
using ResponsibilityFooBar = GofConsoleApp.Examples.Behavioral.CorPattern.InputExampleComponents.ResponsibilityFooBar;
namespace GofConsoleApp.Examples.Behavioral.CorPattern;
internal class CorPatternComplexExample : BaseExample
{
protected override bool Execute()
{
Example1();
Example2();
return true;
}
private void Example1()
{
var orchestrator = new ResponsibilityChainOrchestrator<string>();
// Responsibility - Foo, Handle - WhenResponsible, Invoke Next >>> WhenNotResponsible
var executeFoo = new Action<string>(x => Logger.Log($"Handling '{x}' by 'Foo Handler'"));
var fooHandler = new Responsibility<string>(i => "Foo".Equals(i), executeFoo);
orchestrator.Append(fooHandler, HandleWhenResponsible, InvokeNextWhenNotResponsible, "FooResponsibility");
// Responsibility - Bar, Handle - WhenResponsible, Invoke Next - WhenNotResponsible
var barExecute = new Action<string>(x => Logger.Log($"Handling '{x}' by 'Bar Handler'"));
var barHandler = new Responsibility<string>(i => "Bar".Equals(i), barExecute);
orchestrator.Append(barHandler, HandleWhenResponsible, InvokeNextWhenNotResponsible, "BarResponsibility");
Logger.Log("------------- START Orchestrator with input Foo -------------");
// - Start with >>> Foo
// - Input >> Foo
// ### HandleWhenResponsible >>>> Foo (Executes)
orchestrator.Execute("Foo");
Logger.Log("------------- START Orchestrator with input Bar -------------");
// - Start with >>> Foo
// - Input >> Bar
// ### HandleWhenResponsible >>>> Foo (Not Executes)
// *** Invokes >> Bar (InvokeNextWhenNotResponsible)
// ### HandleWhenResponsible >>>>> Bar (Executes)
orchestrator.Execute("Bar");
}
private void Example2()
{
var orchestrator1 = new ResponsibilityChainOrchestrator<string> { Name = "Orchestrator 1" };
orchestrator1.Append(new ResponsibilityFoo(Logger), HandleWhenResponsible, InvokeNextWhenNotResponsible);
orchestrator1.Append(new ResponsibilityBar(Logger), HandleWhenResponsible, InvokeNextWhenNotResponsible);
Logger.Log($"------------- START {orchestrator1.Name} -------------");
// - Start with >>> Foo
// - Input >> Foo
// ### HandleWhenResponsible >>>> Foo (Not Executes)
// *** Invokes >> Bar (InvokeNextWhenNotResponsible)
// ### HandleWhenResponsible >>>>> Bar (Executes)
orchestrator1.Execute("Bar");
var orchestrator2 = new ResponsibilityChainOrchestrator<string> { Name = "Orchestrator 2" };
orchestrator2.Append(new ResponsibilityFoo(Logger), HandleAlways, InvokeNextAlways);
orchestrator2.Append(new ResponsibilityBar(Logger), HandleWhenResponsible, InvokeNextWhenNotResponsible);
orchestrator2.Append(new ResponsibilityFooBar(Logger), HandleWhenResponsible, InvokeNextWhenNotResponsible);
Logger.Log($"------------- START {orchestrator2.Name} -------------");
// - Start with >>> Foo
// - Input >> FooBar
// ### HandleAlways >>>> Foo (Executes)
// *** Invokes >> Bar (InvokeNextAlways)
// ### HandleWhenResponsible >>>>> Bar (Not Executes)
// *** Invokes >> FooBar (InvokeNextWhenNotResponsible)
// ### HandleWhenResponsible >>>>> FooBar (Executes)
orchestrator2.Execute("FooBar");
}
}