-
Notifications
You must be signed in to change notification settings - Fork 31
/
Prototype.cs
71 lines (62 loc) · 1.57 KB
/
Prototype.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
namespace DesignPatterns.Creational;
public class Prototype
{
/// <summary>
/// Client
/// </summary>
/// <remarks>
/// Creates a new object by asking a prototype to clone itself.
/// </remarks>
[Fact]
public void Execute()
{
var sheepCollection = new Sheep[]
{
new BlackSheep(),
new WhiteSheep()
};
foreach (Sheep sheep in sheepCollection)
{
Sheep clone = sheep.Clone();
clone.Shave();
Assert.NotEqual(sheep.Shaved, clone.Shaved);
Assert.Equal(sheep.HairColor, clone.HairColor);
}
}
/// <summary>
/// Prototype
/// </summary>
/// <remarks>
/// Declares an interface for cloning itself.
/// </remarks>
public abstract class Sheep(string color)
{
public string HairColor { get; } = color;
public bool Shaved { get; private set; }
public abstract Sheep Clone();
public void Shave()
{
Shaved = true;
}
}
/// <summary>
/// Concrete Prototype
/// </summary>
/// <remarks>
/// Implements an operation for cloning itself
/// </remarks>
public class BlackSheep() : Sheep("Black")
{
public override Sheep Clone() => new BlackSheep();
}
/// <summary>
/// Concrete Prototype
/// </summary>
/// <remarks>
/// Implements an operation for cloning itself
/// </remarks>
public class WhiteSheep() : Sheep("White")
{
public override Sheep Clone() => new WhiteSheep();
}
}