Skip to content

Commit eded1e7

Browse files
committed
add prototype pattern
1 parent 3bcbb40 commit eded1e7

File tree

8 files changed

+135
-2
lines changed

8 files changed

+135
-2
lines changed

CSharpDesignPatterns.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27004.2005
4+
VisualStudioVersion = 15.0.27004.2002
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Adapter", "Adapter\Adapter.csproj", "{734C21F0-242C-46AB-970B-71BF2D12DAB0}"
77
EndProject
@@ -45,7 +45,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Visitor", "Visitor\Visitor.
4545
EndProject
4646
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Decorator", "Decorator\Decorator.csproj", "{06E9B0DE-83C3-48F3-B5B1-11DA11AB1090}"
4747
EndProject
48-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Iterator", "Iterator\Iterator.csproj", "{D88BB8E4-F91C-40A1-9A85-DA4CC002F145}"
48+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Iterator", "Iterator\Iterator.csproj", "{D88BB8E4-F91C-40A1-9A85-DA4CC002F145}"
49+
EndProject
50+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prototype", "Prototype\Prototype.csproj", "{FC095363-0A6F-4E09-8342-9474A289C1AA}"
4951
EndProject
5052
Global
5153
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -141,6 +143,10 @@ Global
141143
{D88BB8E4-F91C-40A1-9A85-DA4CC002F145}.Debug|Any CPU.Build.0 = Debug|Any CPU
142144
{D88BB8E4-F91C-40A1-9A85-DA4CC002F145}.Release|Any CPU.ActiveCfg = Release|Any CPU
143145
{D88BB8E4-F91C-40A1-9A85-DA4CC002F145}.Release|Any CPU.Build.0 = Release|Any CPU
146+
{FC095363-0A6F-4E09-8342-9474A289C1AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
147+
{FC095363-0A6F-4E09-8342-9474A289C1AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
148+
{FC095363-0A6F-4E09-8342-9474A289C1AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
149+
{FC095363-0A6F-4E09-8342-9474A289C1AA}.Release|Any CPU.Build.0 = Release|Any CPU
144150
EndGlobalSection
145151
GlobalSection(SolutionProperties) = preSolution
146152
HideSolutionNode = FALSE
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Prototype.AbstractPrototype
2+
{
3+
interface IPerson
4+
{
5+
IPerson Clone();
6+
}
7+
}

Prototype/ConcretePrototype/Dick.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Prototype.AbstractPrototype;
2+
3+
namespace Prototype.ConcretePrototype
4+
{
5+
class Dick : IPerson
6+
{
7+
private string _name = "Dick";
8+
9+
public IPerson Clone()
10+
{
11+
return (IPerson)this.MemberwiseClone();
12+
}
13+
14+
public override string ToString()
15+
{
16+
return _name;
17+
}
18+
}
19+
}

Prototype/ConcretePrototype/Harry.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Prototype.AbstractPrototype;
2+
3+
namespace Prototype.ConcretePrototype
4+
{
5+
class Harry : IPerson
6+
{
7+
private string _name = "Harry";
8+
9+
public IPerson Clone()
10+
{
11+
return (IPerson)this.MemberwiseClone();
12+
}
13+
14+
public override string ToString()
15+
{
16+
return _name;
17+
}
18+
}
19+
}

Prototype/ConcretePrototype/Tom.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Prototype.AbstractPrototype;
2+
3+
namespace Prototype.ConcretePrototype
4+
{
5+
class Tom : IPerson
6+
{
7+
private string _name = "Tom";
8+
9+
public IPerson Clone()
10+
{
11+
return (IPerson)this.MemberwiseClone();
12+
}
13+
14+
public override string ToString()
15+
{
16+
return _name;
17+
}
18+
}
19+
}

Prototype/Program.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using Prototype.AbstractPrototype;
3+
using Prototype.PrototypeUsage;
4+
5+
namespace Prototype
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
var people = new[] { "tom", "dick", "harry" };
12+
13+
foreach (var item in people)
14+
{
15+
IPerson prototype = Factory.GetPrototype(item);
16+
17+
Console.WriteLine(item);
18+
}
19+
20+
Console.WriteLine(Factory.GetPrototype("tom"));
21+
}
22+
}
23+
}

Prototype/Prototype.csproj

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>

Prototype/PrototypeUsage/Factory.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Prototype.AbstractPrototype;
4+
using Prototype.ConcretePrototype;
5+
6+
namespace Prototype.PrototypeUsage
7+
{
8+
class Factory
9+
{
10+
private static readonly Dictionary<string, IPerson> Prototypes = new Dictionary<string, IPerson>();
11+
12+
static Factory()
13+
{
14+
Prototypes.Add("tom", new Tom());
15+
Prototypes.Add("dick", new Dick());
16+
Prototypes.Add("harry", new Harry());
17+
}
18+
19+
public static IPerson GetPrototype(string type)
20+
{
21+
try
22+
{
23+
return Prototypes.GetValueOrDefault(type).Clone();
24+
}
25+
catch (NullReferenceException ex)
26+
{
27+
Console.WriteLine("Prototype with name: " + type + ", doesn't exist");
28+
return null;
29+
}
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)