File tree Expand file tree Collapse file tree 8 files changed +338
-0
lines changed
Expand file tree Collapse file tree 8 files changed +338
-0
lines changed Original file line number Diff line number Diff line change @@ -32,3 +32,6 @@ Full Playlist can be found [here](https://www.youtube.com/playlist?list=PLOeFnOV
3232- [ Mediator] ( https://youtu.be/VYLD75sU1rw )
3333- [ Memento] ( https://youtu.be/SAaIsErpGDY )
3434- [ Observer] ( https://youtu.be/8nICu7ZSPtE )
35+ - [ Strategy] ( https://youtu.be/25qf3VkAlis )
36+ - [ State] ( https://youtu.be/rYmVpqiGL14 )
37+
Original file line number Diff line number Diff line change 1+ < Query Kind = "Program" / >
2+
3+ void Main ( )
4+ {
5+
6+ }
7+
8+ public partial class Computer
9+ {
10+ private State _state = new Off ( ) ;
11+
12+ private void SetState ( State state )
13+ {
14+ _state = state ;
15+ }
16+
17+ public void PressPowerButton ( )
18+ {
19+ _state . PressPowerButton ( this ) ;
20+ }
21+ }
22+
23+ public partial class Computer
24+ {
25+ private interface State
26+ {
27+ void PressPowerButton ( Computer computer ) ;
28+ }
29+
30+ private class Off : State
31+ {
32+ public void PressPowerButton ( Computer computer )
33+ {
34+ // perform some work
35+ computer . SetState ( new On ( ) ) ;
36+ }
37+ }
38+
39+ private class On : State
40+ {
41+ private bool charging ;
42+
43+ public void PressPowerButton ( Computer computer )
44+ {
45+ // perform some work
46+ if ( charging )
47+ {
48+ computer . SetState ( new Standby ( ) ) ;
49+ }
50+ else
51+ {
52+ computer . SetState ( new Off ( ) ) ;
53+ }
54+ }
55+ }
56+
57+ private class Standby : State
58+ {
59+ public void PressPowerButton ( Computer computer )
60+ {
61+ // perform some work
62+ computer . SetState ( new On ( ) ) ;
63+ }
64+ }
65+ }
Original file line number Diff line number Diff line change 1+ < Query Kind = "Program" / >
2+
3+ void Main ( )
4+ {
5+
6+ }
7+
8+ public partial class Pen
9+ {
10+ private State _state = new Idle ( ) ;
11+
12+ private void SetState ( State state ) => _state = state ;
13+
14+ public void OnClick ( Pen pen ) => _state . OnClick ( this ) ;
15+
16+ public void OnClickFinish ( Pen pen ) => _state . OnClickFinish ( this ) ;
17+
18+ public void OnMove ( Pen pen ) => _state . OnMove ( this ) ;
19+ }
20+
21+ public partial class Pen
22+ {
23+ private interface State
24+ {
25+ void OnMove ( Pen pen ) ;
26+ void OnClick ( Pen pen ) ;
27+ void OnClickFinish ( Pen pen ) ;
28+ }
29+
30+ private class Idle : State
31+ {
32+ public void OnClick ( Pen pen )
33+ {
34+ pen . SetState ( new Writing ( ) ) ;
35+ }
36+
37+ public void OnClickFinish ( Pen pen )
38+ {
39+ // do nothing
40+ }
41+
42+ public void OnMove ( Pen pen )
43+ {
44+ // do nothing
45+ }
46+ }
47+
48+ private class Writing : State
49+ {
50+ public void OnClick ( Pen pen )
51+ {
52+ // DRAW HARDER
53+ }
54+
55+ public void OnClickFinish ( Pen pen )
56+ {
57+ pen . SetState ( new Idle ( ) ) ;
58+ }
59+
60+ public void OnMove ( Pen pen )
61+ {
62+ // draw on canvas
63+ }
64+ }
65+
66+ }
Original file line number Diff line number Diff line change 1+ < Query Kind = "Program" / >
2+
3+ void Main ( )
4+ {
5+
6+ }
7+
8+ public class Computer
9+ {
10+ private int state = 0 ;
11+ private bool charging = true ;
12+
13+ public void PressPowerButton ( )
14+ {
15+ // off
16+ if ( state == 0 )
17+ {
18+ // do work
19+ state = 1 ;
20+ return ;
21+ }
22+
23+ // on
24+ if ( state == 1 )
25+ {
26+ // do work
27+ if ( charging )
28+ {
29+ state = 2 ;
30+ return ;
31+ }
32+ state = 0 ;
33+ return ;
34+ }
35+
36+ // standby
37+ // do work
38+ state = 1 ;
39+ }
40+ }
41+
42+
Original file line number Diff line number Diff line change 1+ < Query Kind = "Program" / >
2+
3+ void Main ( )
4+ {
5+ var chain1 = new Log ( ) . Append ( new Transaction ( ) ) . Append ( new Add ( 5 ) ) ;
6+ chain1 . Do ( 10 ) ;
7+
8+ "-------------------" . Dump ( ) ;
9+
10+ var chain2 = new Log ( ) . Append ( new Add ( 10 ) ) . Append ( new Transaction ( ) ) ;
11+ chain2 . Do ( 10 ) ;
12+ }
13+
14+ public abstract class Strategy {
15+
16+ private Strategy _next ;
17+
18+ public Strategy Append ( Strategy next ) {
19+ if ( _next == null ) {
20+ _next = next ;
21+ } else {
22+ _next . Append ( next ) ;
23+ }
24+
25+ return this ;
26+ }
27+
28+ public abstract void Do ( int n ) ;
29+
30+ protected void Next ( int n ) => _next ? . Do ( n ) ;
31+ }
32+
33+ public class Log : Strategy
34+ {
35+ public override void Do ( int n )
36+ {
37+ n . Dump ( "Logging number:" ) ;
38+ Next ( n ) ;
39+ }
40+ }
41+
42+ public class Transaction : Strategy
43+ {
44+ public override void Do ( int n )
45+ {
46+ n . Dump ( "Starting Transaction" ) ;
47+ Next ( n ) ;
48+ n . Dump ( "Finishing Transaction" ) ;
49+ }
50+ }
51+
52+ public class Add : Strategy
53+ {
54+ int _num ;
55+ public Add ( int num ) => _num = num ;
56+
57+ public override void Do ( int n )
58+ {
59+ n . Dump ( $ "Adding { _num } to { n } ") ;
60+ int result = n += _num ;
61+ n . Dump ( $ "Result: { result } ") ;
62+ Next ( result ) ;
63+ }
64+ }
Original file line number Diff line number Diff line change 1+ < Query Kind = "Program" / >
2+
3+ void Main ( )
4+ {
5+
6+ }
7+
8+ public class FileController
9+ {
10+ IFileStorage filestorage ;
11+ public FileController ( IFileStorage filestorage ) => this . filestorage = filestorage ;
12+ }
13+
14+
15+ public interface IFileStorage
16+ {
17+ byte [ ] GetFile ( string id ) ;
18+ void SaveFile ( ) ;
19+ }
20+
21+ public class Local : IFileStorage
22+ {
23+ public byte [ ] GetFile ( string id )
24+ {
25+ throw new NotImplementedException ( ) ;
26+ }
27+
28+ public void SaveFile ( )
29+ {
30+ throw new NotImplementedException ( ) ;
31+ }
32+ }
33+
34+ public class S3 : IFileStorage
35+ {
36+ public byte [ ] GetFile ( string id )
37+ {
38+ throw new NotImplementedException ( ) ;
39+ }
40+
41+ public void SaveFile ( )
42+ {
43+ throw new NotImplementedException ( ) ;
44+ }
45+ }
46+
47+ public class Bespoke : IFileStorage
48+ {
49+ public byte [ ] GetFile ( string id )
50+ {
51+ throw new NotImplementedException ( ) ;
52+ }
53+
54+ public void SaveFile ( )
55+ {
56+ throw new NotImplementedException ( ) ;
57+ }
58+ }
Original file line number Diff line number Diff line change 1+ < Query Kind = "Program" / >
2+
3+ void Main ( )
4+ {
5+ new Thing < A > ( ) . Do ( ) ;
6+ new Thing < B > ( ) . Do ( ) ;
7+ }
8+
9+ public class Thing < T > where T : class , IStrategy , new ( )
10+ {
11+ public void Do ( ) => new T ( ) . Do ( ) ;
12+ }
13+
14+ public interface IStrategy {
15+ void Do ( ) ;
16+ }
17+
18+ public class A : IStrategy
19+ {
20+ public void Do ( ) => "a" . Dump ( ) ;
21+ }
22+
23+ public class B : IStrategy
24+ {
25+ public void Do ( ) => "b" . Dump ( ) ;
26+ }
Original file line number Diff line number Diff line change 1+ < Query Kind = "Program" / >
2+
3+ void Main ( )
4+ {
5+ var process = new Process ( ) ;
6+ process . Setup ( p =>
7+ {
8+ // ad hoc strategy
9+ } ) ;
10+ }
11+
12+ public class Process {
13+ public void Setup ( Action < Process > setup ) => setup ( this ) ;
14+ }
You can’t perform that action at this time.
0 commit comments