| 名称 | Abstract Factory |
| 结构 | ![]() |
| 意图 | 提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。 |
| 适用性 |
|
1
// Abstract Factory
2
3
// Intent: "Provide an interface for creating families of related or
4
// dependent objects without specifying their concrete classes".
5
6
// For further information, read "Design Patterns", p87, Gamma et al.,
7
// Addison-Wesley, ISBN:0-201-63361-2
8
9
/* Notes:
10
* When the construction needed involves many objects, possible organised
11
* in multi-faceted arrangements, the entire construction can be delegated
12
* to an abstract factory. This exposes standardised creation functionality
13
* which can be customised in concrete implementation to suit your specific
14
* needs, and avoid embedding this information in higher level code - it
15
* just needs to know how to call the abstract factory.
16
*
17
* In this sample, we have a framework with three abstract operating classes,
18
* called DPDocument, DPWorkspace and DPView and one abstract construction
19
* class, called DPFactory. An application-level class, called DPApplication
20
* is responsible for construction.
21
*
22
* We have a series of application-level operating classes derived from this
23
* framework - MyDocument, MyWorkspace and MyView. For design reasons we
24
* assume we wish to instantiate these from inside DPApplication. As there
25
* are multiple objects needed and they could be arranged in different
26
* lattices, we use a factory, MyFactory (in our example, there are all
27
* simple siblings), which is called inside DPApplication.
28
*
29
*/
30
31
namespace AbstractFactory_DesignPattern
32
{
33
using System;
34
35
// These classes could be part of a framework,
36
// which we will call DP
37
// ===========================================
38
39
abstract class DPDocument
40
{
41
abstract public void Dump();
42
}
43
44
abstract class DPWorkspace
45
{
46
abstract public void Dump();
47
}
48
49
abstract class DPView
50
{
51
abstract public void Dump();
52
}
53
54
abstract class DPFactory
55
{
56
abstract public DPDocument CreateDocument();
57
abstract public DPView CreateView();
58
abstract public DPWorkspace CreateWorkspace();
59
}
60
61
abstract class DPApplication
62
{
63
protected DPDocument doc;
64
protected DPWorkspace workspace;
65
protected DPView view;
66
67
public void ConstructObjects(DPFactory factory)
68
{
69
// Create objects as needed
70
doc = factory.CreateDocument();
71
workspace = factory.CreateWorkspace();
72
view = factory.CreateView();
73
}
74
75
abstract public void Dump();
76
77
public void DumpState()
78
{
79
if (doc != null) doc.Dump();
80
if (workspace != null) workspace.Dump();
81
if (view != null) view.Dump();
82
}
83
}
84
85
// These classes could be part of an application
86
class MyApplication : DPApplication
87
{
88
MyFactory myFactory = new MyFactory();
89
90
override public void Dump()
91
{
92
Console.WriteLine("MyApplication exists");
93
}
94
95
public void CreateFamily()
96
{
97
MyFactory myFactory = new MyFactory();
98
ConstructObjects(myFactory);
99
}
100
}
101
102
class MyDocument : DPDocument
103
{
104
public MyDocument()
105
{
106
Console.WriteLine("in MyDocument constructor");
107
}
108
109
override public void Dump()
110
{
111
Console.WriteLine("MyDocument exists");
112
}
113
}
114
115
class MyWorkspace : DPWorkspace
116
{
117
override public void Dump()
118
{
119
Console.WriteLine("MyWorkspace exists");
120
}
121
}
122
123
class MyView : DPView
124
{
125
override public void Dump()
126
{
127
Console.WriteLine("MyView exists");
128
}
129
}
130
131
class MyFactory : DPFactory
132
{
133
override public DPDocument CreateDocument()
134
{
135
return new MyDocument();
136
}
137
override public DPWorkspace CreateWorkspace()
138
{
139
return new MyWorkspace();
140
}
141
override public DPView CreateView()
142
{
143
return new MyView();
144
}
145
}
146
147
/// <summary>
148
/// Summary description for Client.
149
/// </summary>
150
public class Client
151
{
152
public static int Main(string[] args)
153
{
154
MyApplication myApplication = new MyApplication();
155
156
myApplication.CreateFamily();
157
158
myApplication.DumpState();
159
160
return 0;
161
}
162
}
163
}
164
165
// Abstract Factory2

3
// Intent: "Provide an interface for creating families of related or4
// dependent objects without specifying their concrete classes". 5

6
// For further information, read "Design Patterns", p87, Gamma et al.,7
// Addison-Wesley, ISBN:0-201-63361-28

9
/* Notes:10
* When the construction needed involves many objects, possible organised11
* in multi-faceted arrangements, the entire construction can be delegated12
* to an abstract factory. This exposes standardised creation functionality13
* which can be customised in concrete implementation to suit your specific14
* needs, and avoid embedding this information in higher level code - it15
* just needs to know how to call the abstract factory.16
* 17
* In this sample, we have a framework with three abstract operating classes, 18
* called DPDocument, DPWorkspace and DPView and one abstract construction 19
* class, called DPFactory. An application-level class, called DPApplication20
* is responsible for construction. 21
* 22
* We have a series of application-level operating classes derived from this23
* framework - MyDocument, MyWorkspace and MyView. For design reasons we 24
* assume we wish to instantiate these from inside DPApplication. As there25
* are multiple objects needed and they could be arranged in different 26
* lattices, we use a factory, MyFactory (in our example, there are all 27
* simple siblings), which is called inside DPApplication. 28
* 29
*/30
31
namespace AbstractFactory_DesignPattern32
{33
using System;34

35
// These classes could be part of a framework,36
// which we will call DP37
// ===========================================38
39
abstract class DPDocument 40
{41
abstract public void Dump(); 42
}43

44
abstract class DPWorkspace45
{46
abstract public void Dump();47
}48
49
abstract class DPView 50
{51
abstract public void Dump();52
} 53
54
abstract class DPFactory 55
{56
abstract public DPDocument CreateDocument();57
abstract public DPView CreateView();58
abstract public DPWorkspace CreateWorkspace();59
}60

61
abstract class DPApplication 62
{63
protected DPDocument doc;64
protected DPWorkspace workspace;65
protected DPView view;66
67
public void ConstructObjects(DPFactory factory)68
{69
// Create objects as needed70
doc = factory.CreateDocument();71
workspace = factory.CreateWorkspace();72
view = factory.CreateView();73
} 74
75
abstract public void Dump();76

77
public void DumpState()78
{79
if (doc != null) doc.Dump();80
if (workspace != null) workspace.Dump();81
if (view != null) view.Dump();82
}83
}84

85
// These classes could be part of an application 86
class MyApplication : DPApplication 87
{88
MyFactory myFactory = new MyFactory();89

90
override public void Dump()91
{92
Console.WriteLine("MyApplication exists");93
}94

95
public void CreateFamily()96
{97
MyFactory myFactory = new MyFactory();98
ConstructObjects(myFactory); 99
}100
} 101

102
class MyDocument : DPDocument 103
{104
public MyDocument()105
{106
Console.WriteLine("in MyDocument constructor"); 107
}108
109
override public void Dump()110
{111
Console.WriteLine("MyDocument exists");112
}113
}114

115
class MyWorkspace : DPWorkspace 116
{117
override public void Dump()118
{119
Console.WriteLine("MyWorkspace exists");120
}121
}122

123
class MyView : DPView 124
{125
override public void Dump()126
{127
Console.WriteLine("MyView exists");128
}129
}130

131
class MyFactory : DPFactory 132
{133
override public DPDocument CreateDocument()134
{135
return new MyDocument();136
}137
override public DPWorkspace CreateWorkspace()138
{139
return new MyWorkspace();140
} 141
override public DPView CreateView()142
{143
return new MyView();144
}145
}146

147
/// <summary>148
/// Summary description for Client.149
/// </summary>150
public class Client151
{152
public static int Main(string[] args)153
{154
MyApplication myApplication = new MyApplication();155

156
myApplication.CreateFamily();157

158
myApplication.DumpState();159
160
return 0;161
}162
}163
}164

165



