设计模式——原型模式
名称 | Prototype |
结构 | ![]() |
意图 | 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。 |
适用性 |
|
1
// Prototype
2![](/Images/OutliningIndicators/None.gif)
3
// Intent: "Specify the kinds of objects to create using a prototypical
4
// instance and create new objects by copying this prototype".
5![](/Images/OutliningIndicators/None.gif)
6
// For further information, read "Design Patterns", p117, Gamma et al.,
7
// Addison-Wesley, ISBN:0-201-63361-2
8![](/Images/OutliningIndicators/None.gif)
9
/* Notes:
10
* When we are not in a position to call a constructor for an object
11
* directly, we could alternatively clone a pre-existing object
12
* (a prototype) of the same class.
13
*
14
* This results in specific class knowledge being only required in
15
* one area (to create the prototype itself), and then later cloned
16
* from code that knows nothing about the cloned prototype, except
17
* that it exposed a well-known cloning method.
18
*
19
*/
20
21
namespace Prototype_DesignPattern
22
{
23
using System;
24![](/Images/OutliningIndicators/InBlock.gif)
25
// Objects which are to work as prototypes must be based on classes which
26
// are derived from the abstract prototype class
27
abstract class AbstractPrototype
28
{
29
abstract public AbstractPrototype CloneYourself();
30
}
31![](/Images/OutliningIndicators/InBlock.gif)
32
// This is a sample object
33
class MyPrototype : AbstractPrototype
34
{
35
override public AbstractPrototype CloneYourself()
36
{
37
return ((AbstractPrototype)MemberwiseClone());
38
}
39
// lots of other functions go here!
40
}
41![](/Images/OutliningIndicators/InBlock.gif)
42
// This is the client piece of code which instantiate objects
43
// based on a prototype.
44
class Demo
45
{
46
private AbstractPrototype internalPrototype;
47![](/Images/OutliningIndicators/InBlock.gif)
48
public void SetPrototype(AbstractPrototype thePrototype)
49
{
50
internalPrototype = thePrototype;
51
}
52![](/Images/OutliningIndicators/InBlock.gif)
53
public void SomeImportantOperation()
54
{
55
// During Some important operation, imagine we need
56
// to instantiate an object - but we do not know which. We use
57
// the predefined prototype object, and ask it to clone itself.
58![](/Images/OutliningIndicators/InBlock.gif)
59
AbstractPrototype x;
60
x = internalPrototype.CloneYourself();
61
// now we have two instances of the class which as as a prototype
62
}
63
}
64![](/Images/OutliningIndicators/InBlock.gif)
65
/// <summary>
66
/// Summary description for Client.
67
/// </summary>
68
public class Client
69
{
70
public static int Main(string[] args)
71
{
72
Demo demo = new Demo();
73
MyPrototype clientPrototype = new MyPrototype();
74
demo.SetPrototype(clientPrototype);
75
demo.SomeImportantOperation();
76![](/Images/OutliningIndicators/InBlock.gif)
77
return 0;
78
}
79
}
80
}
81![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
20
![](/Images/OutliningIndicators/None.gif)
21
![](/Images/OutliningIndicators/None.gif)
22
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
63
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
79
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
80
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
81
![](/Images/OutliningIndicators/None.gif)