File tree Expand file tree Collapse file tree 6 files changed +304
-0
lines changed
Expand file tree Collapse file tree 6 files changed +304
-0
lines changed Original file line number Diff line number Diff line change @@ -26,4 +26,6 @@ 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+ - [ Interpreter] ( https://youtu.be/Vc3aiaAcIME )
2930- [ Chain of Responsibility] ( https://youtu.be/YQ03IyRu1Zo )
31+ - [ Command] ( https://youtu.be/nW2ahdZojho )
Original file line number Diff line number Diff line change 1+ < Query Kind = "Program" / >
2+
3+ void Main ( )
4+ {
5+
6+ }
7+ public class Canvas
8+ {
9+
10+ }
11+
12+ public interface Command
13+ {
14+ void Execute ( Canvas canvas ) ;
15+ }
16+
17+ public class Tool
18+ {
19+ static string state ;
20+
21+ public static Command GetCommand ( )
22+ {
23+ if ( state == "brush" )
24+ return new Brush ( ) ;
25+ if ( state == "erase" )
26+ return new Erase ( ) ;
27+ if ( state == "fill" )
28+ return new Fill ( ) ;
29+
30+ return null ;
31+ }
32+ }
33+
34+ public class Brush : Command
35+ {
36+ public void Execute ( Canvas canvas )
37+ {
38+ // perform action on the canvas
39+ }
40+ }
41+
42+ public class Erase : Command
43+ {
44+ public void Execute ( Canvas canvas )
45+ {
46+ // perform action on the canvas
47+ }
48+ }
49+
50+ public class Fill : Command
51+ {
52+ public void Execute ( Canvas canvas )
53+ {
54+ // perform action on the canvas
55+ }
56+ }
57+
58+
59+
60+ Canvas canvas ;
61+
62+ public void Click ( )
63+ {
64+ Tool . GetCommand ( ) . Execute ( canvas ) ;
65+ }
Original file line number Diff line number Diff line change 1+ < Query Kind = "Program" / >
2+
3+ void Main ( )
4+ {
5+
6+ }
7+
8+ interface Command {
9+ void Execute ( ) ;
10+ }
11+
12+ class HighlightText : Command {
13+
14+ // text is the reciever
15+ public HighlightText ( string text )
16+ {
17+
18+ }
19+
20+ public void Execute ( )
21+ {
22+ // highlight text range
23+ }
24+ }
25+
26+ class BoldText : Command
27+ {
28+ // text is the reciever
29+ public BoldText ( string text )
30+ {
31+
32+ }
33+
34+ public void Execute ( )
35+ {
36+ // make text range bold
37+ }
38+ }
39+
40+ // button is the the decoupling
41+ // we don't know about the operation or the reciever
42+ class Button {
43+ Command command;
44+ public Button ( Command command ) => this . command = command ;
45+
46+ public void Click ( ) => command . Execute ( ) ;
47+ }
Original file line number Diff line number Diff line change 1+ < Query Kind = "Program" / >
2+
3+ void Main ( )
4+ {
5+
6+ }
7+
8+ interface Command
9+ {
10+ void Execute ( ) ;
11+ void Undo ( ) ;
12+ }
13+
14+ public class HighlightConfig
15+ {
16+ public int Colour { get ; set ; }
17+ public int Height { get ; set ; }
18+ }
19+
20+ class HighlightText : Command
21+ {
22+
23+ // text is the reciever
24+ // config is some singleton holding the state of the colour tool
25+ public HighlightText ( string text , HighlightConfig config )
26+ {
27+
28+ }
29+
30+ public void Execute ( )
31+ {
32+ // highlight text range
33+ }
34+
35+ public void Undo ( )
36+ {
37+ // un-highlight text range
38+ }
39+ }
40+
41+ class BoldText : Command
42+ {
43+ // text is the reciever
44+ public BoldText ( string text )
45+ {
46+
47+ }
48+
49+ public void Execute ( )
50+ {
51+ // make text range bold
52+ }
53+
54+ public void Undo ( )
55+ {
56+ // un-highlight text range
57+ }
58+ }
59+
60+ // button is the the decoupling
61+ // we don't know about the operation or the reciever
62+ class Button
63+ {
64+ Command command;
65+ public Button ( Command command ) => this . command = command ;
66+
67+ public void Click ( ) => command . Execute ( ) ;
68+ }
Original file line number Diff line number Diff line change 1+ < Query Kind = "Program" / >
2+
3+ void Main ( )
4+ {
5+ var textContext = new TextContext ( ) ;
6+ var c1 = new DynamicCommand < TextContext > ( textContext , tc => tc . HighlightText ( ) ) ;
7+ var c2 = new DynamicCommand < TextContext > ( textContext , tc => tc . BoldText ( ) ) ;
8+
9+ // via reflection:
10+ // typeof(TextContext).GetMethod("HighlightText").Invoke(textContext, null);
11+ }
12+
13+ public class HighlightConfig
14+ {
15+ public int Colour { get ; set ; }
16+ public int Height { get ; set ; }
17+ }
18+
19+ public class TextContext
20+ {
21+ public void HighlightText ( )
22+ {
23+
24+ // highlight text range
25+ }
26+
27+ public void BoldText ( )
28+ {
29+ // make text range bold
30+ }
31+ }
32+
33+
34+ interface ICommand
35+ {
36+ void Execute ( ) ;
37+ }
38+
39+
40+ class DynamicCommand < T > : ICommand
41+ {
42+ T reciever ;
43+ Action < T > action ;
44+ public DynamicCommand ( T reciever , Action < T > action )
45+ {
46+ this . reciever = reciever ;
47+ this . action = action ;
48+ }
49+
50+ public void Execute ( )
51+ {
52+ action ( reciever ) ;
53+ }
54+ }
55+
56+
57+ // button is the the decoupling
58+ // we don't know about the operation or the reciever
59+ class Button
60+ {
61+ ICommand command ;
62+ public Button ( ICommand command ) => this . command = command ;
63+
64+ public void Click ( ) => command . Execute ( ) ;
65+ }
Original file line number Diff line number Diff line change 1+ < Query Kind = "Program" / >
2+
3+ void Main ( )
4+ {
5+
6+ }
7+
8+ interface Command
9+ {
10+ void Execute ( ) ;
11+ }
12+
13+ public class HighlightConfig
14+ {
15+ public int Colour { get ; set ; }
16+ public int Height { get ; set ; }
17+ }
18+
19+ class HighlightText : Command
20+ {
21+
22+ // text is the reciever
23+ // config is some singleton holding the state of the colour tool
24+ public HighlightText ( string text , HighlightConfig config )
25+ {
26+
27+ }
28+
29+ public void Execute ( )
30+ {
31+ // highlight text range
32+ }
33+ }
34+
35+ class BoldText : Command
36+ {
37+ // text is the reciever
38+ public BoldText ( string text )
39+ {
40+
41+ }
42+
43+ public void Execute ( )
44+ {
45+ // make text range bold
46+ }
47+ }
48+
49+ // button is the the decoupling
50+ // we don't know about the operation or the reciever
51+ class Button
52+ {
53+ Command command;
54+ public Button ( Command command ) => this . command = command ;
55+
56+ public void Click ( ) => command . Execute ( ) ;
57+ }
You can’t perform that action at this time.
0 commit comments