Skip to content

Commit e917a17

Browse files
committed
Flyweight pattern example
1 parent a4bf68b commit e917a17

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FacadePattern", "FacadePatt
2323
EndProject
2424
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DecoratorPattern", "DecoratorPattern\DecoratorPattern.csproj", "{E16867D5-A235-437D-ADBF-8A4418E7038B}"
2525
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlyweightPattern", "FlyweightPattern\FlyweightPattern.csproj", "{FB1E171E-9C39-4EF1-9377-9786AF3C61DA}"
27+
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2830
Debug|Any CPU = Debug|Any CPU
@@ -69,6 +71,10 @@ Global
6971
{E16867D5-A235-437D-ADBF-8A4418E7038B}.Debug|Any CPU.Build.0 = Debug|Any CPU
7072
{E16867D5-A235-437D-ADBF-8A4418E7038B}.Release|Any CPU.ActiveCfg = Release|Any CPU
7173
{E16867D5-A235-437D-ADBF-8A4418E7038B}.Release|Any CPU.Build.0 = Release|Any CPU
74+
{FB1E171E-9C39-4EF1-9377-9786AF3C61DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75+
{FB1E171E-9C39-4EF1-9377-9786AF3C61DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
76+
{FB1E171E-9C39-4EF1-9377-9786AF3C61DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
77+
{FB1E171E-9C39-4EF1-9377-9786AF3C61DA}.Release|Any CPU.Build.0 = Release|Any CPU
7278
EndGlobalSection
7379
GlobalSection(SolutionProperties) = preSolution
7480
HideSolutionNode = FALSE
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

FlyweightPattern/Program.cs

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

0 commit comments

Comments
 (0)