Csharp: Simple Factory Pattern
/// <summary> /// Summary description for Namer. /// Base class for getting split names /// Simple Factory Patterns /// geovindu,Geovin Du,涂聚文 edit /// </summary> public class Namer { /// <summary> /// parts stored here /// </summary> protected string frName, lName; /// <summary> /// return first name /// </summary> /// <returns></returns> public string getFrname() { return frName; } /// <summary> /// return last name /// </summary> /// <returns></returns> public string getLname() { return lName; } }
/// Summary description for LastFirst. /// Simple Factory Patterns /// geovindu,Geovin Du,涂聚文 edit /// </summary> public class LastFirst : Namer { /// <summary> /// /// </summary> /// <param name="name"></param> public LastFirst(string name) { int i = name.IndexOf(","); if (i > 0) { lName = name.Substring(0, i); frName = name.Substring(i + 1).Trim(); } else { lName = name; frName = ""; } } }
/// <summary> /// Summary description for FirstFirst. /// Simple Factory Patterns /// geovindu,Geovin Du,涂聚文 edit /// </summary> public class FirstFirst : Namer { /// <summary> /// /// </summary> /// <param name="name"></param> public FirstFirst(string name) { int i = name.IndexOf(" "); if (i > 0) { frName = name.Substring(0, i).Trim(); lName = name.Substring(i + 1).Trim(); } else { lName = name; frName = ""; } } }
/// <summary> /// Summary description for NameFactory. /// Simple Factory Patterns /// geovindu,Geovin Du,涂聚文 edit /// </summary> public class NameFactory { public NameFactory() { } /// <summary> /// /// </summary> /// <param name="name"></param> /// <returns></returns> public Namer getName(string name) { int i = name.IndexOf(","); if (i > 0) return new LastFirst(name); else return new FirstFirst(name); } }
窗體中調用:
/// <summary> /// Simple Factory Patterns /// geovindu,Geovin Du,涂聚文 edit /// </summary> public partial class SimpleFactoryForm : Form { private NameFactory nameFact; /// <summary> /// /// </summary> public SimpleFactoryForm() { InitializeComponent(); nameFact = new NameFactory(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SimpleFactoryForm_Load(object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { Namer name = nameFact.getName(txtbName.Text); txtbFirst.Text = name.getFrname(); txtbLast.Text = name.getLname(); } }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)