Skip to content

Commit 955c386

Browse files
committed
visitor & template method
1 parent 822655f commit 955c386

File tree

8 files changed

+389
-0
lines changed

8 files changed

+389
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ Full Playlist can be found [here](https://www.youtube.com/playlist?list=PLOeFnOV
3434
- [Observer](https://youtu.be/8nICu7ZSPtE)
3535
- [Strategy](https://youtu.be/25qf3VkAlis)
3636
- [State](https://youtu.be/rYmVpqiGL14)
37+
- [Visitor](https://youtu.be/6SByyc-0c0s)
38+
- [Template Method](https://youtu.be/mcc4XQHpVC8)
3739

template method/avoid.linq

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
}
6+
7+
public class Process
8+
{
9+
public void Start()
10+
{
11+
A();
12+
B();
13+
C();
14+
}
15+
16+
public void A() => "".Dump();
17+
public void B() => "".Dump();
18+
public void C() => "".Dump();
19+
}
20+
21+
public class Process_Configured
22+
{
23+
// more state === harder to understand
24+
public void Start(int a)
25+
{
26+
A();
27+
B();
28+
if (a == 0)
29+
{
30+
C();
31+
}
32+
else
33+
{
34+
D();
35+
}
36+
}
37+
38+
public void A() => "".Dump();
39+
public void B() => "".Dump();
40+
public void C() => "".Dump();
41+
public void D() => "".Dump();
42+
}
43+
44+
45+
public class Process_Strategy
46+
{
47+
// use strategy to supply the algorithm, DO NOT vary it
48+
Action strategy;
49+
public Process_Strategy(Action strategy) => this.strategy = strategy;
50+
51+
public void Start()
52+
{
53+
A();
54+
B();
55+
strategy();
56+
}
57+
58+
public void A() => "".Dump();
59+
public void B() => "".Dump();
60+
}

template method/example.linq

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
new Process().Start();
6+
"--".Dump();
7+
new Variation1().Start();
8+
"--".Dump();
9+
new Variation2().Start();
10+
"--".Dump();
11+
new Variation3().Start();
12+
}
13+
14+
public class Process
15+
{
16+
// template method
17+
public void Start()
18+
{
19+
Step1();
20+
Step2();
21+
Step3();
22+
}
23+
24+
protected virtual void Step1() => "Step 1".Dump();
25+
protected virtual void Step2() => "Step 2".Dump();
26+
protected virtual void Step3() => "Step 3".Dump();
27+
}
28+
29+
public class Variation1 : Process
30+
{
31+
protected override void Step1() => "Step 1 Adapted".Dump();
32+
}
33+
34+
public class Variation2 : Process
35+
{
36+
protected override void Step3() => "Step 3 Adapted".Dump();
37+
}
38+
39+
public class Variation3 : Process
40+
{
41+
protected override void Step1() => "Step 1 Mega Adapted".Dump();
42+
43+
protected override void Step2() => "Step 2 Adapted".Dump();
44+
}

template method/factory.linq

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
new Variation1().Start();
6+
"--".Dump();
7+
new Variation2().Start();
8+
"--".Dump();
9+
new Variation3().Start();
10+
}
11+
12+
public abstract class Process
13+
{
14+
public void Start()
15+
{
16+
var word = FactoryMethod();
17+
// do something with word
18+
}
19+
20+
protected abstract string FactoryMethod();
21+
}
22+
23+
public class Variation1 : Process
24+
{
25+
protected override string FactoryMethod() => "FactoryMethod Variation1".Dump();
26+
}
27+
28+
public class Variation2 : Process
29+
{
30+
protected override string FactoryMethod() => "FactoryMethod Variation2".Dump();
31+
}
32+
33+
public class Variation3 : Process
34+
{
35+
protected override string FactoryMethod() => "FactoryMethod Variation3".Dump();
36+
}

template method/hook.linq

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
}
6+
7+
public abstract class Process
8+
{
9+
10+
public void Start()
11+
{
12+
AbstractHook();
13+
DefaultHook();
14+
}
15+
16+
public abstract void AbstractHook();
17+
18+
public virtual void DefaultHook() {
19+
// default func
20+
}
21+
22+
}
23+
24+
public class A : Process
25+
{
26+
public override void AbstractHook()
27+
{
28+
// override
29+
}
30+
31+
public override void DefaultHook() {
32+
// prepend
33+
base.DefaultHook();
34+
// extend
35+
}
36+
}

visitor/example.linq

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
public abstract class Place
9+
{
10+
public abstract void Visit(Visitor visitor);
11+
}
12+
13+
public abstract class Visitor
14+
{
15+
public abstract void VisitPark(Park park);
16+
public abstract void VisitHome(Home park);
17+
}
18+
19+
public class Park : Place
20+
{
21+
public override void Visit(Visitor visitor)
22+
{
23+
visitor.VisitPark(this);
24+
}
25+
}
26+
27+
public class Home : Place
28+
{
29+
public override void Visit(Visitor visitor)
30+
{
31+
visitor.VisitHome(this);
32+
}
33+
}
34+
35+
public class Dude : Visitor
36+
{
37+
public override void VisitHome(Home park) => "play video games".Dump();
38+
39+
public override void VisitPark(Park park) => "smoke weed".Dump();
40+
}
41+
42+
public class Robber : Visitor
43+
{
44+
public override void VisitHome(Home park) => "steal some socks".Dump();
45+
46+
public override void VisitPark(Park park) => "steal a dog".Dump();
47+
}

visitor/the_mess.linq

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
9+
// other types? == more repos? generic repos?...
10+
public interface IRepository
11+
{
12+
int Get();
13+
int[] List();
14+
void Write(int id);
15+
void Update(int id);
16+
}
17+
18+
public class Postgresql : IRepository
19+
{
20+
21+
public int Get()
22+
{
23+
throw new NotImplementedException();
24+
}
25+
26+
public int[] List()
27+
{
28+
throw new NotImplementedException();
29+
}
30+
31+
public void Update(int id)
32+
{
33+
throw new NotImplementedException();
34+
}
35+
36+
public void Write(int id)
37+
{
38+
throw new NotImplementedException();
39+
}
40+
}
41+
42+
public class MongoDb : IRepository
43+
{
44+
public int Get()
45+
{
46+
throw new NotImplementedException();
47+
}
48+
49+
public int[] List()
50+
{
51+
throw new NotImplementedException();
52+
}
53+
54+
public void Update(int id)
55+
{
56+
throw new NotImplementedException();
57+
}
58+
59+
public void Write(int id)
60+
{
61+
throw new NotImplementedException();
62+
}
63+
}
64+
65+
public class Redis : IRepository
66+
{
67+
public int Get()
68+
{
69+
throw new NotImplementedException();
70+
}
71+
72+
public int[] List()
73+
{
74+
throw new NotImplementedException();
75+
}
76+
77+
public void Update(int id)
78+
{
79+
throw new NotImplementedException();
80+
}
81+
82+
public void Write(int id)
83+
{
84+
throw new NotImplementedException();
85+
}
86+
}

0 commit comments

Comments
 (0)