FactoryMethod工厂方法(创建型模式)

 最近看了李建宗老师的面向对象的设计模式的课程。我把里面的代码记录了下来以供大家参考。其中有的地方我整理了一下,可能有的地方和课程里的不一样。

动机:
  在软件系统中,经常面临着“某个对象”的创建工作;由于需求的变化,这个对象经常面临着剧烈的变化,但是它却拥有比较稳定的接口。
  如何应对这种变化?如何提供一种“封闭机制”来隔离出“这个易变对象”的变化,从而保持系统中“其他依赖该对象”不随着需求改变而改变?

意图:
  定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使得一个类的实例化延迟到子类。
  出自:《设计模式》GoF

Factory Method模式的几个要点:
  1、Factory Method模式主要用于隔离类对象的使用者和具体类型之间的耦合关系。面对一个经常变化的具体类型,紧耦合关系会导致软件的脆弱。
  2、Factory Method模式通过面向对象的手法,将所要创建的具体对象工作延迟到子类,从面实现一个种扩展(而非更改)的策略,较好地解决了这种对耦合关系。
  3、Factory Method模式解决“单个对象”的需求变化,Abstract Factory模式解决“系列对象”的需求变化,Builder模式解决“对象部分”的需求变化。
识别工厂方法模式:
   工厂方法模式需要包括一个操作,这个操作创建了一个对象,同时也使客户无需了解应该具体实例化哪个类.

稳定部分代码:
 1 None.gif using  System;
 2 None.gif
 3 None.gif namespace  NameFactory
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 6InBlock.gif    /// Summary description for Namer.
 7ExpandedSubBlockEnd.gif    /// </summary>

 8InBlock.gif    //Base class for getting split names
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    public class Namer     dot.gif{
10InBlock.gif        //parts stored here
11InBlock.gif        protected string frName, lName;
12InBlock.gif        
13InBlock.gif        //return first name
14ExpandedSubBlockStart.gifContractedSubBlock.gif        public string getFrname()dot.gif{
15InBlock.gif            return frName;
16ExpandedSubBlockEnd.gif        }

17InBlock.gif        //return last name
18ExpandedSubBlockStart.gifContractedSubBlock.gif        public string getLname() dot.gif{
19InBlock.gif            return lName;
20ExpandedSubBlockEnd.gif        }

21ExpandedSubBlockEnd.gif    }

22ExpandedBlockEnd.gif}
具体类
 1 None.gif using  System;
 2 None.gif
 3 None.gif namespace  NameFactory
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 6InBlock.gif    /// Summary description for FirstFirst.
 7ExpandedSubBlockEnd.gif    /// </summary>

 8InBlock.gif    public class FirstFirst : Namer
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        public FirstFirst(string name)
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
12InBlock.gif            int i = name.IndexOf (" ");
13ExpandedSubBlockStart.gifContractedSubBlock.gif            if(i > 0dot.gif{
14InBlock.gif                frName = name.Substring (0, i).Trim ();
15InBlock.gif                lName = name.Substring (i + 1).Trim ();
16ExpandedSubBlockEnd.gif            }

17ExpandedSubBlockStart.gifContractedSubBlock.gif            else dot.gif{
18InBlock.gif                lName = name;
19InBlock.gif                frName = "";
20ExpandedSubBlockEnd.gif            }

21ExpandedSubBlockEnd.gif        }

22ExpandedSubBlockEnd.gif    }

23InBlock.gif    public class LastFirst : Namer
24ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
25ExpandedSubBlockStart.gifContractedSubBlock.gif        public LastFirst(string name)         dot.gif{
26InBlock.gif            int i = name.IndexOf (",");
27ExpandedSubBlockStart.gifContractedSubBlock.gif            if(i > 0dot.gif{
28InBlock.gif                lName = name.Substring (0, i);
29InBlock.gif                frName = name.Substring (i + 1).Trim ();
30ExpandedSubBlockEnd.gif            }

31ExpandedSubBlockStart.gifContractedSubBlock.gif            else dot.gif{
32InBlock.gif                lName = name;
33InBlock.gif                frName = "";
34ExpandedSubBlockEnd.gif            }

35ExpandedSubBlockEnd.gif        }

36ExpandedSubBlockEnd.gif    }

37ExpandedBlockEnd.gif}

38 None.gif
实例化:
 1 None.gif using  System;
 2 None.gif
 3 None.gif namespace  NameFactory
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 6InBlock.gif    /// Summary description for NameFactory.
 7ExpandedSubBlockEnd.gif    /// </summary>

 8ExpandedSubBlockStart.gifContractedSubBlock.gif    public class NameFactory     dot.gif{
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        public NameFactory() dot.gif{}
10InBlock.gif
11ExpandedSubBlockStart.gifContractedSubBlock.gif        public Namer getName(string name) dot.gif{
12InBlock.gif            int i = name.IndexOf (",");
13InBlock.gif            if(i > 0)
14InBlock.gif                return new LastFirst (name);
15InBlock.gif            else
16InBlock.gif                return new FirstFirst (name);
17ExpandedSubBlockEnd.gif        }

18ExpandedSubBlockEnd.gif    }

19ExpandedBlockEnd.gif}

20 None.gif
主程序
  1 None.gif using  System;
  2 None.gif using  System.Drawing;
  3 None.gif using  System.Collections;
  4 None.gif using  System.ComponentModel;
  5 None.gif using  System.Windows.Forms;
  6 None.gif using  System.Data;
  7 None.gif
  8 None.gif namespace  NameFactory
  9 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 11InBlock.gif    /// Summary description for Form1.
 12ExpandedSubBlockEnd.gif    /// </summary>

 13InBlock.gif    public class Form1 : System.Windows.Forms.Form
 14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 15InBlock.gif        private System.Windows.Forms.Label label1;
 16InBlock.gif        private System.Windows.Forms.Button btCompute;
 17InBlock.gif        private System.Windows.Forms.Label label2;
 18InBlock.gif        private System.Windows.Forms.Label label3;
 19InBlock.gif        private System.Windows.Forms.TextBox txFirst;
 20InBlock.gif        private NameFactory nameFact;
 21InBlock.gif        private System.Windows.Forms.TextBox txName;
 22InBlock.gif        private System.Windows.Forms.TextBox txLast;
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 24InBlock.gif        /// Required designer variable.
 25ExpandedSubBlockEnd.gif        /// </summary>

 26InBlock.gif        private System.ComponentModel.Container components = null;
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        private void init() dot.gif{
 28InBlock.gif            nameFact = new NameFactory ();
 29ExpandedSubBlockEnd.gif        }

 30InBlock.gif        public Form1()
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 32InBlock.gif            //
 33InBlock.gif            // Required for Windows Form Designer support
 34InBlock.gif            //
 35InBlock.gif            InitializeComponent();
 36InBlock.gif            init();
 37InBlock.gif
 38InBlock.gif            //
 39InBlock.gif            // TODO: Add any constructor code after InitializeComponent call
 40InBlock.gif            //
 41ExpandedSubBlockEnd.gif        }

 42InBlock.gif
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 44InBlock.gif        /// Clean up any resources being used.
 45ExpandedSubBlockEnd.gif        /// </summary>

 46InBlock.gif        protected override void Dispose( bool disposing )
 47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 48InBlock.gif            if( disposing )
 49ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 50InBlock.gif                if (components != null
 51ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 52InBlock.gif                    components.Dispose();
 53ExpandedSubBlockEnd.gif                }

 54ExpandedSubBlockEnd.gif            }

 55InBlock.gif            base.Dispose( disposing );
 56ExpandedSubBlockEnd.gif        }

 57InBlock.gif
 58ContractedSubBlock.gifExpandedSubBlockStart.gif        Windows Form Designer generated code#region Windows Form Designer generated code
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 60InBlock.gif        /// Required method for Designer support - do not modify
 61InBlock.gif        /// the contents of this method with the code editor.
 62ExpandedSubBlockEnd.gif        /// </summary>

 63InBlock.gif        private void InitializeComponent()
 64ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 65InBlock.gif            this.txName = new System.Windows.Forms.TextBox();
 66InBlock.gif            this.label1 = new System.Windows.Forms.Label();
 67InBlock.gif            this.btCompute = new System.Windows.Forms.Button();
 68InBlock.gif            this.label2 = new System.Windows.Forms.Label();
 69InBlock.gif            this.label3 = new System.Windows.Forms.Label();
 70InBlock.gif            this.txFirst = new System.Windows.Forms.TextBox();
 71InBlock.gif            this.txLast = new System.Windows.Forms.TextBox();
 72InBlock.gif            this.SuspendLayout();
 73InBlock.gif            // 
 74InBlock.gif            // txName
 75InBlock.gif            // 
 76InBlock.gif            this.txName.Location = new System.Drawing.Point(5640);
 77InBlock.gif            this.txName.Name = "txName";
 78InBlock.gif            this.txName.Size = new System.Drawing.Size(15220);
 79InBlock.gif            this.txName.TabIndex = 0;
 80InBlock.gif            this.txName.Text = "";
 81InBlock.gif            // 
 82InBlock.gif            // label1
 83InBlock.gif            // 
 84InBlock.gif            this.label1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
 85InBlock.gif            this.label1.Location = new System.Drawing.Point(648);
 86InBlock.gif            this.label1.Name = "label1";
 87InBlock.gif            this.label1.Size = new System.Drawing.Size(8816);
 88InBlock.gif            this.label1.TabIndex = 1;
 89InBlock.gif            this.label1.Text = "Enter name";
 90InBlock.gif            // 
 91InBlock.gif            // btCompute
 92InBlock.gif            // 
 93InBlock.gif            this.btCompute.Location = new System.Drawing.Point(96176);
 94InBlock.gif            this.btCompute.Name = "btCompute";
 95InBlock.gif            this.btCompute.Size = new System.Drawing.Size(7224);
 96InBlock.gif            this.btCompute.TabIndex = 2;
 97InBlock.gif            this.btCompute.Text = "Compute";
 98InBlock.gif            this.btCompute.Click += new System.EventHandler(this.btCompute_Click);
 99InBlock.gif            // 
100InBlock.gif            // label2
101InBlock.gif            // 
102InBlock.gif            this.label2.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
103InBlock.gif            this.label2.Location = new System.Drawing.Point(4088);
104InBlock.gif            this.label2.Name = "label2";
105InBlock.gif            this.label2.Size = new System.Drawing.Size(7216);
106InBlock.gif            this.label2.TabIndex = 3;
107InBlock.gif            this.label2.Text = "First";
108InBlock.gif            // 
109InBlock.gif            // label3
110InBlock.gif            // 
111InBlock.gif            this.label3.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
112InBlock.gif            this.label3.Location = new System.Drawing.Point(40120);
113InBlock.gif            this.label3.Name = "label3";
114InBlock.gif            this.label3.Size = new System.Drawing.Size(7216);
115InBlock.gif            this.label3.TabIndex = 3;
116InBlock.gif            this.label3.Text = "Last";
117InBlock.gif            // 
118InBlock.gif            // txFirst
119InBlock.gif            // 
120InBlock.gif            this.txFirst.Location = new System.Drawing.Point(12888);
121InBlock.gif            this.txFirst.Name = "txFirst";
122InBlock.gif            this.txFirst.Size = new System.Drawing.Size(8820);
123InBlock.gif            this.txFirst.TabIndex = 4;
124InBlock.gif            this.txFirst.Text = "";
125InBlock.gif            // 
126InBlock.gif            // txLast
127InBlock.gif            // 
128InBlock.gif            this.txLast.Location = new System.Drawing.Point(128128);
129InBlock.gif            this.txLast.Name = "txLast";
130InBlock.gif            this.txLast.Size = new System.Drawing.Size(8820);
131InBlock.gif            this.txLast.TabIndex = 4;
132InBlock.gif            this.txLast.Text = "";
133InBlock.gif            // 
134InBlock.gif            // Form1
135InBlock.gif            // 
136InBlock.gif            this.AutoScaleBaseSize = new System.Drawing.Size(513);
137InBlock.gif            this.ClientSize = new System.Drawing.Size(292273);
138ExpandedSubBlockStart.gifContractedSubBlock.gif            this.Controls.AddRange(new System.Windows.Forms.Control[] dot.gif{
139InBlock.gif                                                                          this.txLast,
140InBlock.gif                                                                          this.txFirst,
141InBlock.gif                                                                          this.label3,
142InBlock.gif                                                                          this.label2,
143InBlock.gif                                                                          this.btCompute,
144InBlock.gif                                                                          this.label1,
145ExpandedSubBlockEnd.gif                                                                          this.txName}
);
146InBlock.gif            this.Name = "Form1";
147InBlock.gif            this.Text = "Name splitter";
148InBlock.gif            this.ResumeLayout(false);
149InBlock.gif
150ExpandedSubBlockEnd.gif        }

151ExpandedSubBlockEnd.gif        #endregion

152InBlock.gif
153ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
154InBlock.gif        /// The main entry point for the application.
155ExpandedSubBlockEnd.gif        /// </summary>

156InBlock.gif        [STAThread]
157InBlock.gif        static void Main() 
158ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
159InBlock.gif            Application.Run(new Form1());
160ExpandedSubBlockEnd.gif        }

161InBlock.gif
162ExpandedSubBlockStart.gifContractedSubBlock.gif        private void btCompute_Click(object sender, System.EventArgs e) dot.gif{
163InBlock.gif            Namer nm = nameFact.getName (txName.Text );
164InBlock.gif            txFirst.Text = nm.getFrname ();
165InBlock.gif            txLast.Text = nm.getLname ();
166ExpandedSubBlockEnd.gif        }

167ExpandedSubBlockEnd.gif    }

168ExpandedBlockEnd.gif}

169 None.gif

转载于:https://www.cnblogs.com/walker/archive/2006/08/16/478608.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值