-
Notifications
You must be signed in to change notification settings - Fork 1
/
2-6-flyweight.vala
49 lines (36 loc) · 1.08 KB
/
2-6-flyweight.vala
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
using Gee;
class KarakTea {
}
class TeaMaker {
protected HashMap<string, KarakTea> available_tea = new HashMap<string, KarakTea> ();
public KarakTea make (string preference) {
if (!available_tea.has_key (preference)) {
available_tea[preference] = new KarakTea ();
}
return available_tea[preference];
}
}
class TeaShop {
protected HashMap<int, KarakTea> orders = new HashMap<int, KarakTea> ();
protected TeaMaker tea_maker;
public TeaShop (TeaMaker tea_maker) {
this.tea_maker = tea_maker;
}
public void take_order (string tea_type, int table) {
orders[table] = tea_maker.make (tea_type);
}
public void serve () {
foreach (int table in orders.keys) {
print ("Serving tea to table# %d\n", table);
}
}
}
public int main (string[] args) {
var tea_maker = new TeaMaker ();
var shop = new TeaShop (tea_maker);
shop.take_order ("less sugar", 1);
shop.take_order ("more milk", 2);
shop.take_order ("without sugar", 5);
shop.serve ();
return 0;
}