Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7465f32

Browse files
Add implementation of Prototype DP.
1 parent 7157d28 commit 7465f32

File tree

6 files changed

+214
-16
lines changed

6 files changed

+214
-16
lines changed

‎Prototype/Participants.txt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
The classes and objects participating in this pattern are:
2+
3+
Prototype (ColorPrototype)
4+
declares an interface for cloning itself
5+
6+
ConcretePrototype (Color)
7+
implements an operation for cloning itself
8+
9+
Client (ColorManager)
10+
creates a new object by asking a prototype to clone itself

‎Prototype/Program.RealWorldCode.cs‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace DoFactory.GangOfFour.Prototype.RealWorld
5+
{
6+
/// <summary>
7+
/// MainApp startup class for Real-World
8+
/// Prototype Design Pattern.
9+
/// </summary>
10+
class MainApp
11+
{
12+
/// <summary>
13+
/// Entry point into console application.
14+
/// </summary>
15+
static void Main()
16+
{
17+
ColorManager colormanager = new ColorManager();
18+
19+
// Initialize with standard colors
20+
colormanager["red"] = new Color(255, 0, 0);
21+
colormanager["green"] = new Color(0, 255, 0);
22+
colormanager["blue"] = new Color(0, 0, 255);
23+
24+
// User adds personalized colors
25+
colormanager["angry"] = new Color(255, 54, 0);
26+
colormanager["peace"] = new Color(128, 211, 128);
27+
colormanager["flame"] = new Color(211, 34, 20);
28+
29+
// User clones selected colors
30+
Color color1 = colormanager["red"].Clone() as Color;
31+
Color color2 = colormanager["peace"].Clone() as Color;
32+
Color color3 = colormanager["flame"].Clone() as Color;
33+
34+
// Wait for user
35+
Console.ReadKey();
36+
}
37+
}
38+
39+
/// <summary>
40+
/// The 'Prototype' abstract class
41+
/// </summary>
42+
abstract class ColorPrototype
43+
{
44+
public abstract ColorPrototype Clone();
45+
}
46+
47+
/// <summary>
48+
/// The 'ConcretePrototype' class
49+
/// </summary>
50+
class Color : ColorPrototype
51+
{
52+
private int _red;
53+
private int _green;
54+
private int _blue;
55+
56+
// Constructor
57+
public Color(int red, int green, int blue)
58+
{
59+
this._red = red;
60+
this._green = green;
61+
this._blue = blue;
62+
}
63+
64+
// Create a shallow copy
65+
public override ColorPrototype Clone()
66+
{
67+
Console.WriteLine(
68+
"Cloning color RGB: {0,3},{1,3},{2,3}",
69+
_red, _green, _blue);
70+
71+
return this.MemberwiseClone() as ColorPrototype;
72+
}
73+
}
74+
75+
/// <summary>
76+
/// Prototype manager
77+
/// </summary>
78+
class ColorManager
79+
{
80+
private Dictionary<string, ColorPrototype> _colors =
81+
new Dictionary<string, ColorPrototype>();
82+
83+
// Indexer
84+
public ColorPrototype this[string key]
85+
{
86+
get { return _colors[key]; }
87+
set { _colors.Add(key, value); }
88+
}
89+
}
90+
}
91+
92+
//Output:
93+
94+
//Cloning color RGB: 255, 0, 0
95+
//Cloning color RGB: 128,211,128
96+
//Cloning color RGB: 211, 34, 20

‎Prototype/Program.StructuralCode.cs‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
3+
/// <summary>
4+
///A fully initialized instance to be copied or cloned
5+
///
6+
/// Specify the kind of objects to create using a prototypical instance,
7+
/// and create new objects by copying this prototype.
8+
///
9+
/// Frequency of use: 3 Medium.
10+
/// </summary>
11+
namespace DoFactory.GangOfFour.Prototype.Structural
12+
{
13+
/// <summary>
14+
/// MainApp startup class for Structural
15+
/// Prototype Design Pattern.
16+
/// </summary>
17+
class MainApp
18+
{
19+
/// <summary>
20+
/// Entry point into console application.
21+
/// </summary>
22+
static void Main()
23+
{
24+
// Create two instances and clone each
25+
26+
ConcretePrototype1 p1 = new ConcretePrototype1("I");
27+
ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
28+
Console.WriteLine("Cloned: {0}", c1.Id);
29+
30+
ConcretePrototype2 p2 = new ConcretePrototype2("II");
31+
ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
32+
Console.WriteLine("Cloned: {0}", c2.Id);
33+
34+
// Wait for user
35+
Console.ReadKey();
36+
}
37+
}
38+
39+
/// <summary>
40+
/// The 'Prototype' abstract class
41+
/// </summary>
42+
abstract class Prototype
43+
{
44+
private string _id;
45+
46+
// Constructor
47+
public Prototype(string id)
48+
{
49+
this._id = id;
50+
}
51+
52+
// Gets id
53+
public string Id
54+
{
55+
get { return _id; }
56+
}
57+
58+
public abstract Prototype Clone();
59+
}
60+
61+
/// <summary>
62+
/// A 'ConcretePrototype' class
63+
/// </summary>
64+
class ConcretePrototype1 : Prototype
65+
{
66+
// Constructor
67+
public ConcretePrototype1(string id)
68+
: base(id)
69+
{
70+
}
71+
72+
// Returns a shallow copy
73+
public override Prototype Clone()
74+
{
75+
return (Prototype)this.MemberwiseClone();
76+
}
77+
}
78+
79+
/// <summary>
80+
/// A 'ConcretePrototype' class
81+
/// </summary>
82+
class ConcretePrototype2 : Prototype
83+
{
84+
// Constructor
85+
public ConcretePrototype2(string id)
86+
: base(id)
87+
{
88+
}
89+
90+
// Returns a shallow copy
91+
public override Prototype Clone()
92+
{
93+
return (Prototype)this.MemberwiseClone();
94+
}
95+
}
96+
}
97+
98+
99+
//Output:
100+
101+
//Cloned: I
102+
//Cloned: II

‎Prototype/Program.cs‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎Prototype/Prototype.csproj‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@
4242
<Reference Include="System.Xml" />
4343
</ItemGroup>
4444
<ItemGroup>
45-
<Compile Include="Program.cs" />
45+
<Compile Include="Program.RealWorldCode.cs" />
46+
<Compile Include="Program.StructuralCode.cs" />
4647
<Compile Include="Properties\AssemblyInfo.cs" />
4748
</ItemGroup>
4849
<ItemGroup>
4950
<None Include="App.config" />
5051
</ItemGroup>
52+
<ItemGroup>
53+
<Content Include="Participants.txt" />
54+
<Content Include="prototype.gif" />
55+
</ItemGroup>
5156
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5257
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5358
Other similar extension points exist, see Microsoft.Common.targets.

‎Prototype/prototype.gif‎

6.42 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /