Skip to content

Commit 4eaa8ed

Browse files
committed
bridge, flyweight, iterator
1 parent 5afe9f3 commit 4eaa8ed

12 files changed

+524
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ Full Playlist can be found [here](https://www.youtube.com/playlist?list=PLOeFnOV
1919
- [Proxy](https://youtu.be/m0aXyRDEQqU)
2020
- [Facade](https://youtu.be/xzpp5_ak8Hg)
2121
- [Composite](https://youtu.be/UsynwPeipb8)
22+
- [Bridge](https://youtu.be/ECBimbOaPs0)
23+
- [Flyweight](https://youtu.be/XxOFsO-Xw8k)
2224

2325

2426
### Behavioral Patterns
25-
WIP
27+
28+
- [Iterator](https://youtu.be/Gco6zF_ygSc)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
// top level abstraction
9+
public abstract class Vehicle {
10+
private Make make;
11+
}
12+
13+
public abstract class Make {}
14+
15+
// implementation
16+
public class Lada : Make { }
17+
public class Volvo : Make { }
18+
19+
// lower level abstraction
20+
public class Car : Vehicle { }
21+
public class Truck : Vehicle { }

bridge/problem_statement.linq

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
// top level abstraction
9+
public abstract class Vehicle {}
10+
11+
// implementation
12+
public class Lada : Vehicle { }
13+
public class Volvo : Vehicle { }
14+
15+
// lower level abstraction
16+
public class LCar : Lada { }
17+
public class LTruck : Lada { }
18+
public class VCar : Volvo { }
19+
public class VTruck : Volvo { }
20+
21+
// ---------------
22+
// try to re-arrange
23+
// ---------------
24+
25+
// lower level abstraction
26+
public class Car : Vehicle { }
27+
public class Truck : Vehicle { }
28+
29+
// implementations
30+
public class L2Car : Car { }
31+
public class L2Truck : Truck { }
32+
public class V2Car : Car { }
33+
public class V2Truck : Truck { }

bridge/real_world_example.linq

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
List<string> stringList = new List<string>();
6+
ICollection<string> stringCollection = new List<string>();
7+
IEnumerable<string> stringEnumerable = new List<string>();
8+
9+
List<int> intList = new List<int>();
10+
ICollection<int> intCollection = new List<int>();
11+
IEnumerable<int> intEnumerable = new List<int>();
12+
}

bridge/see_the_braids.linq

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
// top level abstraction
9+
public abstract class Vehicle
10+
{
11+
private Make make;
12+
13+
public void Start()
14+
{
15+
make.PerformRitual();
16+
make.StartCar();
17+
}
18+
19+
public abstract bool AllowedToDrive(string person);
20+
}
21+
22+
public abstract class Make
23+
{
24+
public abstract void PerformRitual();
25+
public abstract void StartCar();
26+
}
27+
28+
// implementation
29+
public class Lada : Make
30+
{
31+
public override void PerformRitual() => "Hit the car".Dump();
32+
33+
public override void StartCar() {
34+
"fix the wiring".Dump();
35+
"lube the engine".Dump();
36+
"put the key in".Dump();
37+
"turn the key".Dump();
38+
}
39+
}
40+
41+
public class Volvo : Make
42+
{
43+
public override void PerformRitual() => "Greatful for buying a volvo".Dump();
44+
45+
public override void StartCar() => "Press button".Dump();
46+
}
47+
48+
// lower level abstraction
49+
public class Car : Vehicle
50+
{
51+
public override bool AllowedToDrive(string person) => person == "has license";
52+
}
53+
54+
public class Truck : Vehicle
55+
{
56+
public override bool AllowedToDrive(string person) => person == "has special truck license";
57+
}
58+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
9+
public class Icon
10+
{
11+
public Icon(string type)
12+
{
13+
// load icon
14+
}
15+
}
16+
17+
static class ButtonProvider
18+
{
19+
private static Dictionary<string, Button> _cache = new Dictionary<string, Button>();
20+
21+
public static Button GetButton(string type, Func<Button> buttonFactory)
22+
{
23+
if (!_cache.ContainsKey(type))
24+
{
25+
_cache[type] = buttonFactory();
26+
}
27+
28+
return _cache[type];
29+
}
30+
}
31+
32+
static Button SettingsButtonFactory() => new SettingsButton();
33+
34+
abstract class Button
35+
{
36+
// intrinsic state
37+
public Icon Icon { get; set; }
38+
public abstract void Click();
39+
}
40+
41+
class SettingsButton : Button
42+
{
43+
public SettingsButton()
44+
{
45+
Icon = new Icon("settings");
46+
}
47+
48+
public override void Click()
49+
{
50+
// do something
51+
}
52+
}
53+
54+
class DropdownButton : Button
55+
{
56+
Button _btn;
57+
public DropdownButton(Button btn, params string[] options)
58+
{
59+
_btn = btn;
60+
}
61+
62+
public override void Click()
63+
{
64+
_btn.Click(); // do the original thing
65+
// display options
66+
}
67+
}
68+
69+
class SolutionWindow
70+
{
71+
Button settings = new DropdownButton(ButtonProvider.GetButton("settings", SettingsButtonFactory), "option1", "option2");
72+
}
73+
74+
class TerminalWindow
75+
{
76+
Button settings = new DropdownButton(ButtonProvider.GetButton("settings", SettingsButtonFactory), "option2", "option3", "option4");
77+
}
78+
79+
class TestRunnerWindow
80+
{
81+
Button settings = new DropdownButton(ButtonProvider.GetButton("settings", SettingsButtonFactory), "option1", "option2", "option4");
82+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var btn = ButtonProvider.GetButton<string[]>("settings", SettingsButtonFactory);
6+
7+
8+
btn.Click(new[] {"option1", "option2", "option3", "option4"})
9+
}
10+
11+
12+
public class Icon
13+
{
14+
public Icon(string type)
15+
{
16+
// load icon
17+
}
18+
}
19+
20+
static class ButtonProvider
21+
{
22+
private static Dictionary<string, Button> _cache = new Dictionary<string, Button>();
23+
24+
public static Button<T> GetButton<T>(string type, Func<Button<T>> buttonFactory)
25+
{
26+
if (!_cache.ContainsKey(type))
27+
{
28+
_cache[type] = buttonFactory();
29+
}
30+
31+
return (Button<T>) _cache[type];
32+
}
33+
}
34+
35+
static SettingsButton SettingsButtonFactory() => new SettingsButton();
36+
37+
abstract class Button
38+
{
39+
// intrinsic state
40+
public Icon Icon { get; set; }
41+
}
42+
43+
abstract class Button<T> : Button
44+
{
45+
public abstract void Click(T config);
46+
}
47+
48+
class SettingsButton : Button<string[]>
49+
{
50+
public SettingsButton()
51+
{
52+
Icon = new Icon("settings");
53+
}
54+
55+
public override void Click(string[] config)
56+
{
57+
// display options
58+
}
59+
}

flyweight/the_problem.linq

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
public class Icon
9+
{
10+
public Icon(string path)
11+
{
12+
// load icon
13+
}
14+
}
15+
16+
abstract class Button
17+
{
18+
public Icon Icon { get; set; }
19+
}
20+
21+
class SettingsButton : Button
22+
{
23+
public SettingsButton()
24+
{
25+
Icon = new Icon("path_to_settings_icon");
26+
}
27+
}
28+
29+
class SolutionWindow
30+
{
31+
SettingsButton settings = new SettingsButton();
32+
}
33+
34+
class TerminalWindow
35+
{
36+
SettingsButton settings = new SettingsButton();
37+
}
38+
39+
class TestRunnerWindow
40+
{
41+
SettingsButton settings = new SettingsButton();
42+
}

flyweight/the_solution.linq

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
9+
// flyweight
10+
public class Icon
11+
{
12+
public Icon(string type)
13+
{
14+
// load icon
15+
}
16+
}
17+
18+
static class IconProvider
19+
{
20+
private static Dictionary<string, Icon> _cache = new Dictionary<string, Icon>();
21+
22+
public static Icon GetIcon(string type)
23+
{
24+
if (!_cache.ContainsKey(type))
25+
{
26+
_cache[type] = new Icon(type);
27+
}
28+
29+
return _cache[type];
30+
}
31+
}
32+
33+
abstract class Button
34+
{
35+
public Icon Icon { get; set; }
36+
}
37+
38+
class SettingsButton : Button
39+
{
40+
public SettingsButton()
41+
{
42+
Icon = IconProvider.GetIcon("settings");
43+
}
44+
}
45+
46+
class SolutionWindow
47+
{
48+
SettingsButton settings = new SettingsButton();
49+
}
50+
51+
class TerminalWindow
52+
{
53+
SettingsButton settings = new SettingsButton();
54+
}
55+
56+
class TestRunnerWindow
57+
{
58+
SettingsButton settings = new SettingsButton();
59+
}

0 commit comments

Comments
 (0)