动机:
在软件系统中,经常面临着“某个对象”的创建工作;由于需求的变化,这个对象经常面临着剧烈的变化,但是它却拥有比较稳定的接口。
如何应对这种变化?如何提供一种“封闭机制”来隔离出“这个易变对象”的变化,从而保持系统中“其他依赖该对象”不随着需求改变而改变?
意图:
定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使得一个类的实例化延迟到子类。
出自:《设计模式》GoF
Factory Method模式的几个要点:
1、Factory Method模式主要用于隔离类对象的使用者和具体类型之间的耦合关系。面对一个经常变化的具体类型,紧耦合关系会导致软件的脆弱。
2、Factory Method模式通过面向对象的手法,将所要创建的具体对象工作延迟到子类,从面实现一个种扩展(而非更改)的策略,较好地解决了这种对耦合关系。
3、Factory Method模式解决“单个对象”的需求变化,Abstract Factory模式解决“系列对象”的需求变化,Builder模式解决“对象部分”的需求变化。
识别工厂方法模式:
工厂方法模式需要包括一个操作,这个操作创建了一个对象,同时也使客户无需了解应该具体实例化哪个类.
稳定部分代码:
1
using
System;
2
3
namespace
NameFactory
4
{
5
/**//// <summary>
6
/// Summary description for Namer.
7
/// </summary>
8
//Base class for getting split names
9
public class Namer
{
10
//parts stored here
11
protected string frName, lName;
12
13
//return first name
14
public string getFrname()
{
15
return frName;
16
}
17
//return last name
18
public string getLname()
{
19
return lName;
20
}
21
}
22
}
具体类

2

3

4



5


6

7

8

9



10

11

12

13

14



15

16

17

18



19

20

21

22

1
using
System;
2
3
namespace
NameFactory
4
{
5
/**//// <summary>
6
/// Summary description for FirstFirst.
7
/// </summary>
8
public class FirstFirst : Namer
9
{
10
public FirstFirst(string name)
11
{
12
int i = name.IndexOf (" ");
13
if(i > 0)
{
14
frName = name.Substring (0, i).Trim ();
15
lName = name.Substring (i + 1).Trim ();
16
}
17
else
{
18
lName = name;
19
frName = "";
20
}
21
}
22
}
23
public class LastFirst : Namer
24
{
25
public LastFirst(string name)
{
26
int i = name.IndexOf (",");
27
if(i > 0)
{
28
lName = name.Substring (0, i);
29
frName = name.Substring (i + 1).Trim ();
30
}
31
else
{
32
lName = name;
33
frName = "";
34
}
35
}
36
}
37
}
38
实例化:

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

1
using
System;
2
3
namespace
NameFactory
4
{
5
/**//// <summary>
6
/// Summary description for NameFactory.
7
/// </summary>
8
public class NameFactory
{
9
public NameFactory()
{}
10
11
public Namer getName(string name)
{
12
int i = name.IndexOf (",");
13
if(i > 0)
14
return new LastFirst (name);
15
else
16
return new FirstFirst (name);
17
}
18
}
19
}
20
主程序

2

3

4



5


6

7

8



9



10

11



12

13

14

15

16

17

18

19

20

1
using
System;
2
using
System.Drawing;
3
using
System.Collections;
4
using
System.ComponentModel;
5
using
System.Windows.Forms;
6
using
System.Data;
7
8
namespace
NameFactory
9
{
10
/**//// <summary>
11
/// Summary description for Form1.
12
/// </summary>
13
public class Form1 : System.Windows.Forms.Form
14
{
15
private System.Windows.Forms.Label label1;
16
private System.Windows.Forms.Button btCompute;
17
private System.Windows.Forms.Label label2;
18
private System.Windows.Forms.Label label3;
19
private System.Windows.Forms.TextBox txFirst;
20
private NameFactory nameFact;
21
private System.Windows.Forms.TextBox txName;
22
private System.Windows.Forms.TextBox txLast;
23
/**//// <summary>
24
/// Required designer variable.
25
/// </summary>
26
private System.ComponentModel.Container components = null;
27
private void init()
{
28
nameFact = new NameFactory ();
29
}
30
public Form1()
31
{
32
//
33
// Required for Windows Form Designer support
34
//
35
InitializeComponent();
36
init();
37
38
//
39
// TODO: Add any constructor code after InitializeComponent call
40
//
41
}
42
43
/**//// <summary>
44
/// Clean up any resources being used.
45
/// </summary>
46
protected override void Dispose( bool disposing )
47
{
48
if( disposing )
49
{
50
if (components != null)
51
{
52
components.Dispose();
53
}
54
}
55
base.Dispose( disposing );
56
}
57
58
Windows Form Designer generated code#region Windows Form Designer generated code
59
/**//// <summary>
60
/// Required method for Designer support - do not modify
61
/// the contents of this method with the code editor.
62
/// </summary>
63
private void InitializeComponent()
64
{
65
this.txName = new System.Windows.Forms.TextBox();
66
this.label1 = new System.Windows.Forms.Label();
67
this.btCompute = new System.Windows.Forms.Button();
68
this.label2 = new System.Windows.Forms.Label();
69
this.label3 = new System.Windows.Forms.Label();
70
this.txFirst = new System.Windows.Forms.TextBox();
71
this.txLast = new System.Windows.Forms.TextBox();
72
this.SuspendLayout();
73
//
74
// txName
75
//
76
this.txName.Location = new System.Drawing.Point(56, 40);
77
this.txName.Name = "txName";
78
this.txName.Size = new System.Drawing.Size(152, 20);
79
this.txName.TabIndex = 0;
80
this.txName.Text = "";
81
//
82
// label1
83
//
84
this.label1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
85
this.label1.Location = new System.Drawing.Point(64, 8);
86
this.label1.Name = "label1";
87
this.label1.Size = new System.Drawing.Size(88, 16);
88
this.label1.TabIndex = 1;
89
this.label1.Text = "Enter name";
90
//
91
// btCompute
92
//
93
this.btCompute.Location = new System.Drawing.Point(96, 176);
94
this.btCompute.Name = "btCompute";
95
this.btCompute.Size = new System.Drawing.Size(72, 24);
96
this.btCompute.TabIndex = 2;
97
this.btCompute.Text = "Compute";
98
this.btCompute.Click += new System.EventHandler(this.btCompute_Click);
99
//
100
// label2
101
//
102
this.label2.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
103
this.label2.Location = new System.Drawing.Point(40, 88);
104
this.label2.Name = "label2";
105
this.label2.Size = new System.Drawing.Size(72, 16);
106
this.label2.TabIndex = 3;
107
this.label2.Text = "First";
108
//
109
// label3
110
//
111
this.label3.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
112
this.label3.Location = new System.Drawing.Point(40, 120);
113
this.label3.Name = "label3";
114
this.label3.Size = new System.Drawing.Size(72, 16);
115
this.label3.TabIndex = 3;
116
this.label3.Text = "Last";
117
//
118
// txFirst
119
//
120
this.txFirst.Location = new System.Drawing.Point(128, 88);
121
this.txFirst.Name = "txFirst";
122
this.txFirst.Size = new System.Drawing.Size(88, 20);
123
this.txFirst.TabIndex = 4;
124
this.txFirst.Text = "";
125
//
126
// txLast
127
//
128
this.txLast.Location = new System.Drawing.Point(128, 128);
129
this.txLast.Name = "txLast";
130
this.txLast.Size = new System.Drawing.Size(88, 20);
131
this.txLast.TabIndex = 4;
132
this.txLast.Text = "";
133
//
134
// Form1
135
//
136
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
137
this.ClientSize = new System.Drawing.Size(292, 273);
138
this.Controls.AddRange(new System.Windows.Forms.Control[]
{
139
this.txLast,
140
this.txFirst,
141
this.label3,
142
this.label2,
143
this.btCompute,
144
this.label1,
145
this.txName});
146
this.Name = "Form1";
147
this.Text = "Name splitter";
148
this.ResumeLayout(false);
149
150
}
151
#endregion
152
153
/**//// <summary>
154
/// The main entry point for the application.
155
/// </summary>
156
[STAThread]
157
static void Main()
158
{
159
Application.Run(new Form1());
160
}
161
162
private void btCompute_Click(object sender, System.EventArgs e)
{
163
Namer nm = nameFact.getName (txName.Text );
164
txFirst.Text = nm.getFrname ();
165
txLast.Text = nm.getLname ();
166
}
167
}
168
}
169

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

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138



139

140

141

142

143

144

145

146

147

148

149

150

151

152

153


154

155

156

157

158



159

160

161

162



163

164

165

166

167

168

169
