----------
Sample
------------------------------
using System;
namespace Factory
{
/**//// <summary>
/// Namer 的摘要说明。
/// </summary>
public class Namer //基类
{
protected string fname,lname;
public string getFname()
{
return this.fname;
}
public string getLname()
{
return this.lname;
}
}
}
---------------------------
using System;
namespace Factory
{
/**//// <summary>
/// First 的摘要说明。
/// </summary>
public class First:Namer //继承类 用空格分开姓名的一种方法
{
public First(string name)
{
int i=name.Trim().IndexOf(" ");
if(i>0)
{
fname=name.Substring(0,i).Trim();
lname=name.Substring(i+1).Trim();
}
else
{
lname=name;
fname="";
}
}
}
}
------------------------------
using System;
namespace Factory
{
/**//// <summary>
/// Second 的摘要说明。
/// </summary>
public class Second:Namer //继承类 用逗号分开姓名的另一种方法
{
public Second(string name)
{
int i=name.IndexOf(",");
if(i>0)
{
lname=name.Substring(0,i);
fname=name.Substring(i+1).Trim();
}
else
{
lname=name;
fname="";
}
}
}
}
------------------------------
using System;
namespace Factory
{
/**//// <summary>
/// NameFactory 的摘要说明。
/// </summary>
public class NameFactory //工厂类
{
public Namer getName(string name)
{
int i=name.IndexOf(",");
if(i>0)
{
return new Second(name);
}
else
{
return new First(name);
}
}
}
}
--------------------------------
/**/ ///简单工厂模式示例//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Factory
{
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txname;
private System.Windows.Forms.Button btComputer;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txlname;
private System.Windows.Forms.TextBox txfname;
private System.Windows.Forms.Label label4;
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txname = new System.Windows.Forms.TextBox();
this.btComputer = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.txlname = new System.Windows.Forms.TextBox();
this.txfname = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 112);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "Enter name";
//
// txname
//
this.txname.Location = new System.Drawing.Point(168, 112);
this.txname.Name = "txname";
this.txname.TabIndex = 1;
this.txname.Text = "";
//
// btComputer
//
this.btComputer.Location = new System.Drawing.Point(120, 248);
this.btComputer.Name = "btComputer";
this.btComputer.TabIndex = 2;
this.btComputer.Text = "Computer";
this.btComputer.Click += new System.EventHandler(this.btComputer_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(32, 160);
this.label2.Name = "label2";
this.label2.TabIndex = 3;
this.label2.Text = "First Name";
//
// label3
//
this.label3.Location = new System.Drawing.Point(32, 200);
this.label3.Name = "label3";
this.label3.TabIndex = 4;
this.label3.Text = "Last Name";
//
// txlname
//
this.txlname.Location = new System.Drawing.Point(168, 200);
this.txlname.Name = "txlname";
this.txlname.TabIndex = 5;
this.txlname.Text = "";
//
// txfname
//
this.txfname.Location = new System.Drawing.Point(168, 152);
this.txfname.Name = "txfname";
this.txfname.TabIndex = 6;
this.txfname.Text = "";
//
// label4
//
this.label4.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label4.Location = new System.Drawing.Point(80, 32);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(208, 40);
this.label4.TabIndex = 7;
this.label4.Text = "简单工厂示例";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 307);
this.Controls.Add(this.label4);
this.Controls.Add(this.txfname);
this.Controls.Add(this.txlname);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.btComputer);
this.Controls.Add(this.txname);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void btComputer_Click(object sender, System.EventArgs e)
{
NameFactory nf=new NameFactory();
Namer nm=nf.getName(this.txname.Text);
this.txfname.Text=nm.getFname();
this.txlname.Text=nm.getLname();
}
}
}
Sample
------------------------------
using System;
namespace Factory
{
/**//// <summary>
/// Namer 的摘要说明。
/// </summary>
public class Namer //基类
{
protected string fname,lname;
public string getFname()
{
return this.fname;
}
public string getLname()
{
return this.lname;
}
}
}
---------------------------
using System;
namespace Factory
{
/**//// <summary>
/// First 的摘要说明。
/// </summary>
public class First:Namer //继承类 用空格分开姓名的一种方法
{
public First(string name)
{
int i=name.Trim().IndexOf(" ");
if(i>0)
{
fname=name.Substring(0,i).Trim();
lname=name.Substring(i+1).Trim();
}
else
{
lname=name;
fname="";
}
}
}
}
------------------------------
using System;
namespace Factory
{
/**//// <summary>
/// Second 的摘要说明。
/// </summary>
public class Second:Namer //继承类 用逗号分开姓名的另一种方法
{
public Second(string name)
{
int i=name.IndexOf(",");
if(i>0)
{
lname=name.Substring(0,i);
fname=name.Substring(i+1).Trim();
}
else
{
lname=name;
fname="";
}
}
}
}
------------------------------
using System;
namespace Factory
{
/**//// <summary>
/// NameFactory 的摘要说明。
/// </summary>
public class NameFactory //工厂类
{
public Namer getName(string name)
{
int i=name.IndexOf(",");
if(i>0)
{
return new Second(name);
}
else
{
return new First(name);
}
}
}
}
--------------------------------
/**/ ///简单工厂模式示例//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Factory
{
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txname;
private System.Windows.Forms.Button btComputer;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txlname;
private System.Windows.Forms.TextBox txfname;
private System.Windows.Forms.Label label4;
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txname = new System.Windows.Forms.TextBox();
this.btComputer = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.txlname = new System.Windows.Forms.TextBox();
this.txfname = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 112);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "Enter name";
//
// txname
//
this.txname.Location = new System.Drawing.Point(168, 112);
this.txname.Name = "txname";
this.txname.TabIndex = 1;
this.txname.Text = "";
//
// btComputer
//
this.btComputer.Location = new System.Drawing.Point(120, 248);
this.btComputer.Name = "btComputer";
this.btComputer.TabIndex = 2;
this.btComputer.Text = "Computer";
this.btComputer.Click += new System.EventHandler(this.btComputer_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(32, 160);
this.label2.Name = "label2";
this.label2.TabIndex = 3;
this.label2.Text = "First Name";
//
// label3
//
this.label3.Location = new System.Drawing.Point(32, 200);
this.label3.Name = "label3";
this.label3.TabIndex = 4;
this.label3.Text = "Last Name";
//
// txlname
//
this.txlname.Location = new System.Drawing.Point(168, 200);
this.txlname.Name = "txlname";
this.txlname.TabIndex = 5;
this.txlname.Text = "";
//
// txfname
//
this.txfname.Location = new System.Drawing.Point(168, 152);
this.txfname.Name = "txfname";
this.txfname.TabIndex = 6;
this.txfname.Text = "";
//
// label4
//
this.label4.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label4.Location = new System.Drawing.Point(80, 32);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(208, 40);
this.label4.TabIndex = 7;
this.label4.Text = "简单工厂示例";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 307);
this.Controls.Add(this.label4);
this.Controls.Add(this.txfname);
this.Controls.Add(this.txlname);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.btComputer);
this.Controls.Add(this.txname);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void btComputer_Click(object sender, System.EventArgs e)
{
NameFactory nf=new NameFactory();
Namer nm=nf.getName(this.txname.Text);
this.txfname.Text=nm.getFname();
this.txlname.Text=nm.getLname();
}
}
}
---------------------------
最后一个是简单工厂模式的应用。简单工厂模式有三个以上的类。一个是基类,一个以上的继承类和一个工厂类,最后一个当然是应用类。简单工厂模式是用多继承来实现的,可以说是应用于有多种解决方法的问题