动机:
在软件系统中,经常面临着“某个对象”的创建工作;由于需求的变化,这个对象经常面临着剧烈的变化,但是它却拥有比较稳定的接口。
如何应对这种变化?如何提供一种“封闭机制”来隔离出“这个易变对象”的变化,从而保持系统中“其他依赖该对象”不随着需求改变而改变?
意图:
定义一个用于创建对象的接口,让子类决定实例化哪一个类。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
}
具体类
using System;2

3
namespace NameFactory4
{5
/// <summary>6
/// Summary description for Namer.7
/// </summary>8
//Base class for getting split names9
public class Namer {10
//parts stored here11
protected string frName, lName;12
13
//return first name14
public string getFrname(){15
return frName;16
}17
//return last name18
public string getLname() {19
return lName;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
实例化:
using System;2

3
namespace NameFactory4
{5
/// <summary>6
/// Summary description for FirstFirst.7
/// </summary>8
public class FirstFirst : Namer9
{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 : Namer24
{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

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
主程序
using System;2

3
namespace NameFactory4
{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
else16
return new FirstFirst (name);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
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
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 NameFactory9
{10
/// <summary>11
/// Summary description for Form1.12
/// </summary>13
public class Form1 : System.Windows.Forms.Form14
{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 support34
//35
InitializeComponent();36
init();37

38
//39
// TODO: Add any constructor code after InitializeComponent call40
//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 code152

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


