Skip to content

Commit 9befb0d

Browse files
committed
interpreter, observer, memento, mediator
1 parent c1d38ca commit 9befb0d

File tree

12 files changed

+550
-0
lines changed

12 files changed

+550
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ Full Playlist can be found [here](https://www.youtube.com/playlist?list=PLOeFnOV
2929
- [Interpreter](https://youtu.be/Vc3aiaAcIME)
3030
- [Chain of Responsibility](https://youtu.be/YQ03IyRu1Zo)
3131
- [Command](https://youtu.be/nW2ahdZojho)
32+
- [Mediator](https://youtu.be/VYLD75sU1rw)
33+
- [Memento](https://youtu.be/SAaIsErpGDY)
34+
- [Observer](https://youtu.be/8nICu7ZSPtE)

interpreter/domain_language.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+
// Language
6+
var domainLanguage = "order x10 '2L water bottles' from Tesco";
7+
Order.Parse(domainLanguage).Dump();
8+
}
9+
10+
// Grammar Representation
11+
const string optionalSpace = " ?";
12+
const string qty = "x(?<qty>\\d+)" + optionalSpace;
13+
const string product = "'(?<product>[\\w ]+)'" + optionalSpace;
14+
const string source = "from (?<source>\\w+)";
15+
const string orderCommand = "order" + optionalSpace + qty + product + source;
16+
17+
public class Order
18+
{
19+
static Regex _regex = new Regex(orderCommand);
20+
21+
Order(int qty, string product, string source)
22+
{
23+
Qty = qty;
24+
Product = product;
25+
Source = source;
26+
}
27+
28+
public int Qty { get; }
29+
public string Product { get; }
30+
public string Source { get; }
31+
32+
// Interpreter
33+
public static Order Parse(string command)
34+
{
35+
var match = _regex.Match(command);
36+
if (!match.Success)
37+
{
38+
return null;
39+
}
40+
41+
var qty = int.Parse(match.Groups["qty"].Value);
42+
var product = match.Groups["product"].Value;
43+
var source = match.Groups["source"].Value;
44+
45+
return new Order(qty, product, source);
46+
}
47+
}

mediator/intro.linq

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
public interface IMediator{
9+
public void Interaction();
10+
}
11+
12+
public class Mediator : IMediator
13+
{
14+
public Piece piece;
15+
public Part part;
16+
public Thing thing;
17+
18+
public Mediator()
19+
{
20+
// setup components
21+
}
22+
23+
public void Interaction()
24+
{
25+
26+
}
27+
}
28+
29+
public class Piece
30+
{
31+
IMediator mediator;
32+
public Piece(IMediator mediator) => this.mediator = mediator;
33+
34+
public void Invoke(){
35+
mediator.Interaction();
36+
}
37+
}
38+
39+
public class Part
40+
{
41+
IMediator mediator;
42+
public Part(IMediator mediator) => this.mediator = mediator;
43+
44+
}
45+
46+
public class Thing
47+
{
48+
IMediator mediator;
49+
public Thing(IMediator mediator) => this.mediator = mediator;
50+
51+
}

mediator/problem.linq

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
public class Product
9+
{
10+
public int Id { get; set; }
11+
}
12+
13+
public class Cart
14+
{
15+
List<Product> products;
16+
17+
public void AddProduct(Product product)
18+
{
19+
products.Add(product);
20+
}
21+
}
22+
23+
public class Catalog
24+
{
25+
List<Product> products;
26+
27+
public List<Product> getProducts(){
28+
return products;
29+
}
30+
}
31+
32+
public class ProductPage
33+
{
34+
Cart cart;
35+
Catalog catalog;
36+
37+
public ProductPage(Catalog catalog, Cart cart)
38+
{
39+
this.cart = cart;
40+
this.catalog = catalog;
41+
}
42+
43+
public void AddToCart(int id)
44+
{
45+
var product = catalog.getProducts().First(x => x.Id == id);
46+
cart.AddProduct(product);
47+
}
48+
49+
public void DisplayProducts(){
50+
// set ui elements from catalog
51+
}
52+
}

mediator/solution.linq

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
public class Product
9+
{
10+
public int Id { get; set; }
11+
}
12+
13+
public class ShopContext {
14+
Cart cart;
15+
Catalog catalog;
16+
ProductPage page;
17+
18+
public ShopContext(Cart cart, Catalog catalog, ProductPage page)
19+
{
20+
this.cart = cart;
21+
this.catalog = catalog;
22+
this.page = page;
23+
24+
this.page.DisplayProducts(catalog.getProducts());
25+
}
26+
27+
public void AddToCart(int id)
28+
{
29+
cart.AddProduct(catalog.getProducts().First(x => x.Id == id));
30+
}
31+
}
32+
33+
public class Cart
34+
{
35+
List<Product> products;
36+
37+
public void AddProduct(Product product)
38+
{
39+
products.Add(product);
40+
}
41+
}
42+
43+
public class Catalog
44+
{
45+
List<Product> products;
46+
47+
public List<Product> getProducts(){
48+
return products;
49+
}
50+
}
51+
52+
public class ProductPage
53+
{
54+
ShopContext context;
55+
public ProductPage(ShopContext context) => this.context = context;
56+
57+
public void AddToCart(int id) => context.AddToCart(id);
58+
59+
public void DisplayProducts(List<Product> products)
60+
{
61+
// set ui elements
62+
}
63+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
public class Product
9+
{
10+
public int Id { get; set; }
11+
}
12+
13+
public interface ICartInteraction {
14+
void AddToCart(int id);
15+
}
16+
17+
public class ShopContext : ICartInteraction {
18+
Cart cart;
19+
Catalog catalog;
20+
ProductPage page;
21+
22+
public ShopContext(Cart cart, Catalog catalog, ProductPage page)
23+
{
24+
this.cart = cart;
25+
this.catalog = catalog;
26+
this.page = page;
27+
28+
this.page.DisplayProducts(catalog.getProducts());
29+
}
30+
31+
public void AddToCart(int id)
32+
{
33+
cart.AddProduct(catalog.getProducts().First(x => x.Id == id));
34+
}
35+
}
36+
37+
public class Cart
38+
{
39+
List<Product> products;
40+
41+
public void AddProduct(Product product)
42+
{
43+
products.Add(product);
44+
}
45+
}
46+
47+
public class Catalog
48+
{
49+
List<Product> products;
50+
51+
public List<Product> getProducts(){
52+
return products;
53+
}
54+
}
55+
56+
public class ProductPage
57+
{
58+
ICartInteraction cart;
59+
public ProductPage(ICartInteraction cart) => this.cart = cart;
60+
61+
public void AddToCart(int id) => cart.AddToCart(id);
62+
63+
public void DisplayProducts(List<Product> products)
64+
{
65+
// set ui elements
66+
}
67+
}

mediator/with_mediator.linq

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
public class LogicA {
9+
10+
Mediator m;
11+
12+
public void Do(){
13+
m.DoLogic("b");
14+
}
15+
}
16+
17+
public class Mediator {
18+
19+
public void DoLogic(string logic){
20+
if(logic == "b"){
21+
new LogicB().Do();
22+
}
23+
}
24+
}
25+
26+
public class LogicB
27+
{
28+
public void Do()
29+
{
30+
31+
}
32+
}

mediator/without_mediator.linq

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
6+
}
7+
8+
public class LogicA {
9+
10+
LogicB b;
11+
12+
public void Do(){
13+
b.Do();
14+
}
15+
}
16+
17+
public class LogicB
18+
{
19+
public void Do()
20+
{
21+
22+
}
23+
}

0 commit comments

Comments
 (0)