-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathProgram.cs
66 lines (56 loc) · 1.52 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
namespace FlyweightPattern
{
class Program
{
// Anything that will be cached is flyweight.
// Types of tea here will be flyweights.
class KarakTea
{
}
// Acts as a factory and saves the tea
class TeaMaker
{
private Dictionary<string, KarakTea> mAvailableTea = new Dictionary<string, KarakTea>();
public KarakTea Make(string preference)
{
if (!mAvailableTea.ContainsKey(preference))
{
mAvailableTea[preference] = new KarakTea();
}
return mAvailableTea[preference];
}
}
class TeaShop
{
private Dictionary<int, KarakTea> mOrders = new Dictionary<int, KarakTea>();
private readonly TeaMaker mTeaMaker;
public TeaShop(TeaMaker teaMaker)
{
mTeaMaker = teaMaker ?? throw new ArgumentNullException("teaMaker", "teaMaker cannot be null");
}
public void TakeOrder(string teaType, int table)
{
mOrders[table] = mTeaMaker.Make(teaType);
}
public void Serve()
{
foreach (var table in mOrders.Keys)
{
Console.WriteLine("Serving Tea to table # {0}", table);
}
}
}
static void Main(string[] args)
{
var teaMaker = new TeaMaker();
var teaShop = new TeaShop(teaMaker);
teaShop.TakeOrder("less sugar", 1);
teaShop.TakeOrder("more milk", 2);
teaShop.TakeOrder("without sugar", 5);
teaShop.Serve();
Console.ReadLine();
}
}
}