Skip to content

Commit 5afe9f3

Browse files
committed
facade & composite
1 parent 721875a commit 5afe9f3

File tree

24 files changed

+348
-0
lines changed

24 files changed

+348
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Full Playlist can be found [here](https://www.youtube.com/playlist?list=PLOeFnOV
1717
- [Adapter](https://youtu.be/9ZFN8DrvcYA)
1818
- [Decorator](https://youtu.be/6rTnCkdbJA4)
1919
- [Proxy](https://youtu.be/m0aXyRDEQqU)
20+
- [Facade](https://youtu.be/xzpp5_ak8Hg)
21+
- [Composite](https://youtu.be/UsynwPeipb8)
2022

2123

2224
### Behavioral Patterns

composite/example.linq

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
new CompositeComponent().Dump();
6+
}
7+
8+
public interface IComponent
9+
{
10+
IList<IComponent> Children { get; }
11+
}
12+
13+
public class RegularComponent : IComponent
14+
{
15+
public string Name = nameof(RegularComponent);
16+
public IList<IComponent> Children => null;
17+
}
18+
19+
public class OrdinaryComponent : IComponent
20+
{
21+
public string Name = nameof(OrdinaryComponent);
22+
public IList<IComponent> Children => null;
23+
}
24+
25+
public class CompositeComponent : IComponent
26+
{
27+
public string Name = nameof(CompositeComponent);
28+
public IList<IComponent> Children => new List<IComponent>
29+
{
30+
new RegularComponent(),
31+
new OrdinaryComponent(),
32+
new CompositeComponent(),
33+
new CompositeComponent(),
34+
};
35+
}

composite/linked_list.linq

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var list = new LinkedList<int>();
6+
7+
list.Add(1);
8+
list.Add(2);
9+
list.Add(3);
10+
list.Add(4);
11+
12+
list.Get(0).Dump();
13+
list.Get(1).Dump();
14+
list.Get(2).Dump();
15+
list.Get(3).Dump();
16+
list.Get(4).Dump();
17+
}
18+
19+
public class LinkedList<T>
20+
{
21+
public LinkedList() {}
22+
public LinkedList(T v) => value = v;
23+
24+
T value;
25+
LinkedList<T> _next;
26+
27+
public T Get(int i)
28+
{
29+
if (_next == null)
30+
{
31+
throw new IndexOutOfRangeException();
32+
}
33+
34+
if (i == 0)
35+
{
36+
return value;
37+
}
38+
39+
return _next.Get(--i);
40+
}
41+
42+
public void Add(T v)
43+
{
44+
if (_next == null)
45+
{
46+
value = v;
47+
_next = new LinkedList<T>();
48+
}
49+
else
50+
{
51+
_next.Add(v);
52+
}
53+
}
54+
55+
}

composite/products.linq

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
new Table().Dump();
6+
}
7+
8+
public abstract class Product
9+
{
10+
public List<Product> Components { get; set; } = new List<Product>();
11+
}
12+
13+
public class Table : Product
14+
{
15+
public Table()
16+
{
17+
Components.Add(new Legs());
18+
Components.Add(new Top());
19+
Components.Add(new Screws());
20+
}
21+
}
22+
23+
public class Top : Product { }
24+
25+
public class Legs : Product
26+
{
27+
public Legs()
28+
{
29+
Components.Add(new Feet());
30+
Components.Add(new Padding());
31+
Components.Add(new Screws());
32+
}
33+
}
34+
35+
public class Feet : Product
36+
{
37+
}
38+
39+
public class Padding : Product
40+
{
41+
}
42+
43+
public class Screws : Product
44+
{
45+
}

composite/tree.linq

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var top = new Node("top");
6+
7+
top.Nodes.Add(new Node("2nd level - 0"));
8+
var lvl21 = new Node("2nd level - 1");
9+
top.Nodes.Add(lvl21);
10+
var lvl2 = new Node(">> 2nd level - 2");
11+
top.Nodes.Add(lvl2);
12+
13+
lvl21.Nodes.Add(new Node("3rd level - 0"));
14+
lvl21.Nodes.Add(new Node("3rd level - 1"));
15+
lvl21.Nodes.Add(new Node("3rd level - 2"));
16+
17+
18+
top.ComputeNodes("nothing");
19+
}
20+
21+
public class Node
22+
{
23+
public Node(string data)
24+
{
25+
this.NodeData = data;
26+
}
27+
28+
public string NodeData { get; set; }
29+
30+
public List<Node> Nodes { get; set; } = new List<Node>() {};
31+
32+
33+
public void ComputeNodes(string parentScope) {
34+
var data = NodeData.Dump("start --" + parentScope);
35+
36+
foreach (var n in Nodes)
37+
{
38+
n.ComputeNodes(data);
39+
}
40+
41+
NodeData.Dump("end");
42+
}
43+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Madness.Appliences
2+
{
3+
public class Fridge
4+
{
5+
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Madness.Appliences
2+
{
3+
public class Microwave
4+
{
5+
6+
}
7+
}

facade/Kitchen/Appliences/Oven.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Madness.Appliences
2+
{
3+
public class Oven
4+
{
5+
6+
}
7+
}

facade/Kitchen/Appliences/Stove.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Madness.Appliences
2+
{
3+
public class Stove
4+
{
5+
6+
}
7+
}

facade/Kitchen/Dishes/BigMac.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Madness.Dishes
2+
{
3+
public class BigMac
4+
{
5+
6+
}
7+
}

0 commit comments

Comments
 (0)