Skip to content

Commit 8c1bd3a

Browse files
committed
implement factory method, add prototype pattern.
1 parent d81446f commit 8c1bd3a

12 files changed

+230
-3
lines changed

Creational/FactoryMethod/FactoryMethod.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@
4343
<Reference Include="System.Xml" />
4444
</ItemGroup>
4545
<ItemGroup>
46+
<Compile Include="MagicMazeGame.cs" />
47+
<Compile Include="MagicRoom.cs" />
48+
<Compile Include="MazeGame.cs" />
4649
<Compile Include="Program.cs" />
4750
<Compile Include="Properties\AssemblyInfo.cs" />
51+
<Compile Include="Room.cs" />
4852
</ItemGroup>
4953
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5054
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace FactoryMethod {
2+
3+
class MagicMazeGame : MazeGame {
4+
5+
protected override Room MakeRoom() {
6+
return new MagicRoom();
7+
}
8+
}
9+
}

Creational/FactoryMethod/MagicRoom.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace FactoryMethod {
2+
3+
class MagicRoom : Room {
4+
5+
}
6+
}

Creational/FactoryMethod/MazeGame.cs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections.Generic;
2+
3+
namespace FactoryMethod {
4+
5+
abstract class MazeGame {
6+
7+
private readonly IList<Room> _rooms = new List<Room>();
8+
9+
protected MazeGame() {
10+
var room1 = this.MakeRoom();
11+
var room2 = this.MakeRoom();
12+
room1.Connect(room2);
13+
this.AddRoom(room1);
14+
this.AddRoom(room2);
15+
}
16+
17+
private void AddRoom(Room room) {
18+
this._rooms.Add(room);
19+
}
20+
21+
protected abstract Room MakeRoom();
22+
23+
}
24+
}

Creational/FactoryMethod/Program.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52

63
namespace FactoryMethod {
74

85
class Program {
96

107
static void Main(string[] args) {
8+
9+
MazeGame game = new MagicMazeGame();
10+
11+
Console.ReadKey();
1112
}
1213
}
1314
}

Creational/FactoryMethod/Room.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace FactoryMethod {
2+
3+
class Room {
4+
5+
private Room _next;
6+
7+
public void Connect(Room other) {
8+
this._next = other;
9+
}
10+
}
11+
}

Creational/Prototype/Program.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace Prototype {
4+
5+
class Program {
6+
7+
static void Main(string[] args) {
8+
Prototype prototype = new PrototypeImpl(1000);
9+
10+
for (int i = 1; i < 10; i++) {
11+
var tempotype = (Prototype)prototype.Clone();
12+
13+
// Usage of values in prototype to derive a new value.
14+
tempotype.X = tempotype.X * i;
15+
tempotype.PrintX();
16+
}
17+
18+
Console.ReadKey();
19+
}
20+
}
21+
}
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("Prototype")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Prototype")]
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("f44fa181-60ef-47c9-9b69-69d45b9c5f68")]
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")]

Creational/Prototype/Prototype.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace Prototype {
4+
5+
abstract class Prototype : ICloneable {
6+
7+
public abstract int X {
8+
get;
9+
set;
10+
}
11+
12+
public abstract void PrintX();
13+
14+
public abstract object Clone();
15+
16+
}
17+
}

Creational/Prototype/Prototype.csproj

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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>{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Prototype</RootNamespace>
12+
<AssemblyName>Prototype</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="Program.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
<Compile Include="Prototype.cs" />
49+
<Compile Include="PrototypeImpl.cs" />
50+
</ItemGroup>
51+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
52+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
53+
Other similar extension points exist, see Microsoft.Common.targets.
54+
<Target Name="BeforeBuild">
55+
</Target>
56+
<Target Name="AfterBuild">
57+
</Target>
58+
-->
59+
</Project>

Creational/Prototype/PrototypeImpl.cs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
3+
namespace Prototype {
4+
5+
class PrototypeImpl : Prototype {
6+
7+
private int _x;
8+
9+
public override int X {
10+
get {
11+
return this._x;
12+
}
13+
set {
14+
this._x = value;
15+
}
16+
}
17+
18+
public PrototypeImpl(int x) {
19+
this._x = x;
20+
}
21+
22+
public override void PrintX() {
23+
Console.WriteLine("value : {0}", this.X);
24+
}
25+
26+
public override object Clone() {
27+
return new PrototypeImpl(this._x);
28+
}
29+
}
30+
}

DesignPatterns.sln

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Builder", "Creational\Build
3333
EndProject
3434
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryMethod", "Creational\FactoryMethod\FactoryMethod.csproj", "{040AFA71-3175-4C6C-8DB2-98810D3E98F4}"
3535
EndProject
36+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prototype", "Creational\Prototype\Prototype.csproj", "{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}"
37+
EndProject
3638
Global
3739
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3840
Debug|Any CPU = Debug|Any CPU
@@ -127,6 +129,12 @@ Global
127129
{040AFA71-3175-4C6C-8DB2-98810D3E98F4}.Release|Any CPU.ActiveCfg = Release|x86
128130
{040AFA71-3175-4C6C-8DB2-98810D3E98F4}.Release|x86.ActiveCfg = Release|x86
129131
{040AFA71-3175-4C6C-8DB2-98810D3E98F4}.Release|x86.Build.0 = Release|x86
132+
{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}.Debug|Any CPU.ActiveCfg = Debug|x86
133+
{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}.Debug|x86.ActiveCfg = Debug|x86
134+
{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}.Debug|x86.Build.0 = Debug|x86
135+
{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}.Release|Any CPU.ActiveCfg = Release|x86
136+
{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}.Release|x86.ActiveCfg = Release|x86
137+
{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}.Release|x86.Build.0 = Release|x86
130138
EndGlobalSection
131139
GlobalSection(SolutionProperties) = preSolution
132140
HideSolutionNode = FALSE
@@ -146,5 +154,6 @@ Global
146154
{FF8A73BD-7DCB-46C1-8F83-EE84D65320B4} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C}
147155
{92346BF9-D8E8-4D1E-A3B6-48EA49BD9ADC} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C}
148156
{040AFA71-3175-4C6C-8DB2-98810D3E98F4} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C}
157+
{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C}
149158
EndGlobalSection
150159
EndGlobal

0 commit comments

Comments
 (0)