设计模式--原型模式(附带类关系图一张)

原型模式

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
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
public class Simple : ICloneable
{
private int field1;
private int field2;

public int Property1
{
get { return field1; }
set { field1 = value; }
}

public int Property2
{
get { return field2; }
set { field2 = value; }
}

public object Clone()
{
return this.MemberwiseClone();
}
}

public class Complecated : ICloneable
{
private Simple simple;

public Complecated()
{
simple = new Simple();
}

private Complecated(Simple s)
{
simple = (Simple) s.Clone();
}

public object Clone()
{
Complecated c = new Complecated(simple);
return c;
}

public void SetMe(int a, int b)
{
simple.Property1 = a;
simple.Property2 = b;
}

public void Show()
{
Console.WriteLine(simple.Property1);
Console.WriteLine(simple.Property2);
}
}

public class Client
{
public static void Main()
{
Complecated c = new Complecated();
c.SetMe(1, 2);
Complecated c2 = (Complecated) c.Clone();


c2.SetMe(3, 4);

c.Show();
c2.Show();

Console.Read();
}
}
}