/
Prototype.cs
58 lines (50 loc) · 1.2 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
using System;
namespace Prototype
{
class MainApp
{
static void Main()
{
// Create two instances and clone each
Prototype prototype1 = new ConcretePrototype1("I");
Prototype clonedPrototype1 = prototype1.Clone();
Console.WriteLine ("Cloned: {0}", clonedPrototype1 .Id);
Prototype prototype2 = new ConcretePrototype2("II");
Prototype clonedPrototype2 = prototype2.Clone();
Console.WriteLine ("Cloned: {0}", clonedPrototype2 .Id);
}
}
// "Prototype"
public abstract class Prototype
{
// Constructor
public Prototype(string id)
{
this.Id = id;
Console.Write("Base constructor is called.");
}
// Property
public string Id { get; private set; }
public virtual Prototype Clone()
{
// Shallow copy
return (Prototype)this.MemberwiseClone();
}
}
// "ConcretePrototype1"
public class ConcretePrototype1 : Prototype
{
// Constructor
public ConcretePrototype1(string id) : base(id)
{
}
}
// "ConcretePrototype2"
public class ConcretePrototype2 : Prototype
{
// Constructor
public ConcretePrototype2(string id) : base(id)
{
}
}
}