Skip to content

Commit 9f28a60

Browse files
committed
add facade, flyweight pattern
1 parent ce0e9b9 commit 9f28a60

File tree

15 files changed

+396
-0
lines changed

15 files changed

+396
-0
lines changed

DesignPatterns.sln

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Composite", "Structural\Com
4747
EndProject
4848
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Decorator", "Structural\Decorator\Decorator.csproj", "{7F3A9BF2-137D-41AA-9346-4E117326850D}"
4949
EndProject
50+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Facade", "Structural\Facade\Facade.csproj", "{A3F748C6-13F4-4DE1-880B-73664F801EA7}"
51+
EndProject
52+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Flyweight", "Structural\Flyweight\Flyweight.csproj", "{7E303E38-84E0-45B9-8580-3109479B1F49}"
53+
EndProject
5054
Global
5155
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5256
Debug|Any CPU = Debug|Any CPU
@@ -177,6 +181,18 @@ Global
177181
{7F3A9BF2-137D-41AA-9346-4E117326850D}.Release|Any CPU.ActiveCfg = Release|x86
178182
{7F3A9BF2-137D-41AA-9346-4E117326850D}.Release|x86.ActiveCfg = Release|x86
179183
{7F3A9BF2-137D-41AA-9346-4E117326850D}.Release|x86.Build.0 = Release|x86
184+
{A3F748C6-13F4-4DE1-880B-73664F801EA7}.Debug|Any CPU.ActiveCfg = Debug|x86
185+
{A3F748C6-13F4-4DE1-880B-73664F801EA7}.Debug|x86.ActiveCfg = Debug|x86
186+
{A3F748C6-13F4-4DE1-880B-73664F801EA7}.Debug|x86.Build.0 = Debug|x86
187+
{A3F748C6-13F4-4DE1-880B-73664F801EA7}.Release|Any CPU.ActiveCfg = Release|x86
188+
{A3F748C6-13F4-4DE1-880B-73664F801EA7}.Release|x86.ActiveCfg = Release|x86
189+
{A3F748C6-13F4-4DE1-880B-73664F801EA7}.Release|x86.Build.0 = Release|x86
190+
{7E303E38-84E0-45B9-8580-3109479B1F49}.Debug|Any CPU.ActiveCfg = Debug|x86
191+
{7E303E38-84E0-45B9-8580-3109479B1F49}.Debug|x86.ActiveCfg = Debug|x86
192+
{7E303E38-84E0-45B9-8580-3109479B1F49}.Debug|x86.Build.0 = Debug|x86
193+
{7E303E38-84E0-45B9-8580-3109479B1F49}.Release|Any CPU.ActiveCfg = Release|x86
194+
{7E303E38-84E0-45B9-8580-3109479B1F49}.Release|x86.ActiveCfg = Release|x86
195+
{7E303E38-84E0-45B9-8580-3109479B1F49}.Release|x86.Build.0 = Release|x86
180196
EndGlobalSection
181197
GlobalSection(SolutionProperties) = preSolution
182198
HideSolutionNode = FALSE
@@ -202,5 +218,7 @@ Global
202218
{FECD9206-68C5-4F31-A7AB-AD876E05ABDA} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E}
203219
{F8ED6676-6701-4476-9606-4D150EF4A537} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E}
204220
{7F3A9BF2-137D-41AA-9346-4E117326850D} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E}
221+
{A3F748C6-13F4-4DE1-880B-73664F801EA7} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E}
222+
{7E303E38-84E0-45B9-8580-3109479B1F49} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E}
205223
EndGlobalSection
206224
EndGlobal

Structural/Facade/Computer.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Facade {
2+
3+
class Computer {
4+
5+
private readonly Cpu _cpu;
6+
private readonly Memory _memory;
7+
private readonly HardDrive _hardDrive;
8+
9+
private const long BootAddress = 0;
10+
11+
public Computer() {
12+
this._cpu = new Cpu();
13+
this._memory = new Memory();
14+
this._hardDrive = new HardDrive();
15+
}
16+
17+
public void StartComputer() {
18+
this._cpu.Freeze();
19+
this._memory.Load(BootAddress, this._hardDrive.Read(BootAddress));
20+
this._cpu.Execute();
21+
}
22+
}
23+
}

Structural/Facade/Cpu.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Facade {
2+
3+
class Cpu {
4+
5+
public void Freeze() {
6+
}
7+
8+
public void Jump(long position) {
9+
}
10+
11+
public void Execute() {
12+
}
13+
14+
}
15+
}

Structural/Facade/Facade.csproj

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{A3F748C6-13F4-4DE1-880B-73664F801EA7}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Facade</RootNamespace>
12+
<AssemblyName>Facade</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Computer.cs" />
47+
<Compile Include="Cpu.cs" />
48+
<Compile Include="HardDrive.cs" />
49+
<Compile Include="Memory.cs" />
50+
<Compile Include="Program.cs" />
51+
<Compile Include="Properties\AssemblyInfo.cs" />
52+
</ItemGroup>
53+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
55+
Other similar extension points exist, see Microsoft.Common.targets.
56+
<Target Name="BeforeBuild">
57+
</Target>
58+
<Target Name="AfterBuild">
59+
</Target>
60+
-->
61+
</Project>

Structural/Facade/HardDrive.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Facade {
2+
3+
class HardDrive {
4+
5+
public byte[] Read(long address) {
6+
return new byte[0];
7+
}
8+
9+
}
10+
}

Structural/Facade/Memory.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Facade {
2+
3+
class Memory {
4+
5+
public void Load(long address, byte[] data) {
6+
}
7+
8+
}
9+
}

Structural/Facade/Program.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Facade {
2+
3+
class Program {
4+
5+
static void Main(string[] args) {
6+
var facade = new Computer();
7+
facade.StartComputer();
8+
}
9+
10+
}
11+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Facade")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Facade")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("19b69e2a-36b9-42ba-bb2d-1e6770e448e7")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace Flyweight {
4+
5+
public class CoffeeFlavor : ICoffeeOrder {
6+
7+
public string Flavor {
8+
get;
9+
private set;
10+
}
11+
12+
public CoffeeFlavor(string newFlavor) {
13+
this.Flavor = newFlavor;
14+
}
15+
16+
public void ServeCoffee(CoffeeOrderContext context) {
17+
Console.WriteLine("Serving coffee flavor {0} to table {1} .", this.Flavor, context.Table);
18+
}
19+
20+
}
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
3+
namespace Flyweight {
4+
5+
public class CoffeeFlavorFactory {
6+
7+
private readonly IDictionary<string, CoffeeFlavor> _flavors = new Dictionary<string, CoffeeFlavor>();
8+
9+
public CoffeeFlavor GetCoffeeFlavor(string flavorName) {
10+
CoffeeFlavor flavor;
11+
if (this._flavors.TryGetValue(flavorName, out flavor)) {
12+
return flavor;
13+
}
14+
flavor = new CoffeeFlavor(flavorName);
15+
this._flavors.Add(flavorName, flavor);
16+
return flavor;
17+
}
18+
19+
public int TotalFlaversMade {
20+
get {
21+
return this._flavors.Count;
22+
}
23+
}
24+
25+
}
26+
}

0 commit comments

Comments
 (0)