创建型模式之简单工厂模式
/Base class for geting split names
public class Namer()
{
protected string frName,lName;
public string getFrname()
{
return frName;
}
public string getLname()
{
return lName;
}
}
public class FirstFirst:Namer
{
public FirstFirst(string name)
{
int i=name.IndexOf(" ");
if(i>0)
{
lName=name.Substring(0,i).Trim();
frName=name.Substring(i+1).Trim();
}else{
lName=name;
frName="";
}
}
public class LastFirst:Namer
{
public LastFirst(string name)
{
int i=name.IndexOf(",");
if(i>0)
{
lName=name.Substring(0,i).Trim();
frName=name.Substring(i+1).Trim();
}else{
lName=name;
frName="";
}
}
}
public class Namefactory()
{
public Namefactory(){}
public Namer getName(string name)
int i=name.IndexOf(",");
if(i>0)
{
return new LastFirst(name);
}else
{
return new FirstFirst(name);
}
}
private void btnCompute_Click(object sender,System.Event.args e)
{
Namer nm=nameFact.getName(txtName.Text);
txtFirst.Text = nm.getFrname();
txtLast.Text = nm.getLname();
}