生成器模式的定义:将一个复合对象的结构和对象的呈现分开,这样相同的结构可以生成不同的显示。
考虑下面这个例子,每个国家都有它自己的饮食习惯,运动习惯,娱乐习惯,导游(Direct)会根据自己的顾客国籍安排不同的菜谱,推荐不同的运动项目和娱乐节目,从而生成不同的活动清单。(Product)
生成活动清单接口前,先定义一个国家类
public
class
Country
... {
public string foodcustomer;//饮食习惯
public string sportcustomer;//运动
public string funcustomer;//娱乐
//----------
public void setfoodcustomer(string str)
...{
foodcustomer=str;
}
public void setsportcustomer(string str)
...{
sportcustomer=str;
}
public void setfuncustomer(string str)
...{
funcustomer=str;
}
//显示
public void show(Label lb)
...{
lb.Text="饮食习惯 "+foodcustomer+"<BR>运动习惯 "+sportcustomer+"<BR>娱乐习惯 "+funcustomer;
}
}
... {
public string foodcustomer;//饮食习惯
public string sportcustomer;//运动
public string funcustomer;//娱乐
//----------
public void setfoodcustomer(string str)
...{
foodcustomer=str;
}
public void setsportcustomer(string str)
...{
sportcustomer=str;
}
public void setfuncustomer(string str)
...{
funcustomer=str;
}
//显示
public void show(Label lb)
...{
lb.Text="饮食习惯 "+foodcustomer+"<BR>运动习惯 "+sportcustomer+"<BR>娱乐习惯 "+funcustomer;
}
}
接着定义接口
//
定义生成活动清单的接口
public interface BuilderIntface
... {
void BuildFood();//创建饮食习惯
void BuildSport();
void BuildFun();
Country GetCountry();
}
// 继承上面接口的两个国籍
public class China:BuilderIntface
... {
private Country china;
public void BuildFood()
...{
china=new Country();
china.setfoodcustomer("饺子");
}
public void BuildSport()
...{
china.setsportcustomer("乒乓球");
}
public void BuildFun()
...{
china.setfuncustomer("喜欢爬山");
}
public Country GetCountry()
...{
return china;
}
}
public class Ameirican:BuilderIntface
... {
private Country american;
public void BuildFood()
...{
american=new Country();
american.setfoodcustomer("汉堡");
}
public void BuildSport()
...{
american.setsportcustomer("蓝球");
}
public void BuildFun()
...{
american.setfuncustomer("喜欢游泳");
}
public Country GetCountry()
...{
return american;
}
}
// 定义导游类
public class Direct
... {
public void setCountyStyle(BuilderIntface countyname)
...{
countyname.BuildFood();
countyname.BuildSport();
countyname.BuildFun();
}
}
public interface BuilderIntface
... {
void BuildFood();//创建饮食习惯
void BuildSport();
void BuildFun();
Country GetCountry();
}
// 继承上面接口的两个国籍
public class China:BuilderIntface
... {
private Country china;
public void BuildFood()
...{
china=new Country();
china.setfoodcustomer("饺子");
}
public void BuildSport()
...{
china.setsportcustomer("乒乓球");
}
public void BuildFun()
...{
china.setfuncustomer("喜欢爬山");
}
public Country GetCountry()
...{
return china;
}
}
public class Ameirican:BuilderIntface
... {
private Country american;
public void BuildFood()
...{
american=new Country();
american.setfoodcustomer("汉堡");
}
public void BuildSport()
...{
american.setsportcustomer("蓝球");
}
public void BuildFun()
...{
american.setfuncustomer("喜欢游泳");
}
public Country GetCountry()
...{
return american;
}
}
// 定义导游类
public class Direct
... {
public void setCountyStyle(BuilderIntface countyname)
...{
countyname.BuildFood();
countyname.BuildSport();
countyname.BuildFun();
}
}
最后按钮事件来实现
private
void
Button2_Click(
object
sender, System.EventArgs e)
... {
Direct director=new Direct();
BuilderIntface b1=new China();
BuilderIntface b2=new Ameirican();
//创建两个不同的活动清单
director.setCountyStyle(b1);
Country p1=b1.GetCountry();
p1.show(lbResult1);
//
director.setCountyStyle(b2);
Country p2=b2.GetCountry();
p2.show(lbResult2);
}
... {
Direct director=new Direct();
BuilderIntface b1=new China();
BuilderIntface b2=new Ameirican();
//创建两个不同的活动清单
director.setCountyStyle(b1);
Country p1=b1.GetCountry();
p1.show(lbResult1);
//
director.setCountyStyle(b2);
Country p2=b2.GetCountry();
p2.show(lbResult2);
}