-
Notifications
You must be signed in to change notification settings - Fork 636
/
Copy pathBeverageFlyweightFactory.cs
46 lines (42 loc) · 1.56 KB
/
BeverageFlyweightFactory.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
using System;
using System.Collections.Generic;
namespace FlyweightPattern
{
public class BeverageFlyweightFactory
{
private readonly Dictionary<BeverageType, IBeverage> _beverages;
public BeverageFlyweightFactory()
{
_beverages = new Dictionary<BeverageType, IBeverage>();
}
public IBeverage MakeBeverage(BeverageType type)
{
_beverages.TryGetValue(type, out var beverage);
if (beverage == null)
{
switch (type)
{
case BeverageType.BubbleMilk:
beverage = new BubbleMilkTea();
_beverages.Add(BeverageType.BubbleMilk, beverage);
break;
case BeverageType.FoamMilk:
beverage = new FoamMilkTea();
_beverages.Add(BeverageType.FoamMilk, beverage);
break;
case BeverageType.OolongMilk:
beverage = new OolingMilkTea();
_beverages.Add(BeverageType.OolongMilk, beverage);
break;
case BeverageType.CoconutMilk:
beverage = new CoconutMilkTea();
_beverages.Add(BeverageType.CoconutMilk, beverage);
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}
return beverage;
}
}
}