Skip to content

Commit 222d1f6

Browse files
committed
chain of responsibility
1 parent 4eaa8ed commit 222d1f6

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ Full Playlist can be found [here](https://www.youtube.com/playlist?list=PLOeFnOV
2626
### Behavioral Patterns
2727

2828
- [Iterator](https://youtu.be/Gco6zF_ygSc)
29+
- [Chain of Responsibility](https://youtu.be/YQ03IyRu1Zo)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
// select code in editor tab
6+
var ide = new IDE(null);
7+
var editor = new CodeEditor(ide);
8+
var codeSelection = new CodeSelection(editor);
9+
10+
codeSelection.HandleKey("Ctrl+F");
11+
codeSelection.HandleKey("Alt+F4");
12+
}
13+
14+
interface IKeyHandler
15+
{
16+
void HandleKey(string key);
17+
}
18+
19+
class IDE : IKeyHandler
20+
{
21+
IKeyHandler _handler;
22+
23+
public IDE(IKeyHandler handler) => _handler = handler;
24+
25+
public void HandleKey(string key)
26+
{
27+
if (key == "Ctrl+F")
28+
{
29+
"Full Search".Dump();
30+
}
31+
else if (key == "Alt+F4")
32+
{
33+
"Close Application?".Dump();
34+
}
35+
else
36+
{
37+
_handler?.HandleKey(key);
38+
}
39+
}
40+
}
41+
42+
class CodeEditor : IKeyHandler
43+
{
44+
IKeyHandler _handler;
45+
46+
public CodeEditor(IKeyHandler handler) => _handler = handler;
47+
48+
public void HandleKey(string key)
49+
{
50+
if (key == "Ctrl+F")
51+
{
52+
"Local Search".Dump();
53+
}
54+
else
55+
{
56+
_handler?.HandleKey(key);
57+
}
58+
}
59+
}
60+
61+
class CodeSelection : IKeyHandler
62+
{
63+
IKeyHandler _handler;
64+
65+
public CodeSelection(IKeyHandler handler) => _handler = handler;
66+
67+
public void HandleKey(string key)
68+
{
69+
if (key == "Ctrl+F")
70+
{
71+
"Selection Search".Dump();
72+
}
73+
else
74+
{
75+
_handler?.HandleKey(key);
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)