对于生成器模式我这里以生产自行车为例进行讲解,自行车由车轮与车架组成,由不同样式的车轮与不同样式的车架就可以组装成不同的自行车(如赛车,山地车等),下面的程式进行说明:
/// <summary>定義自行車架構</summary>
public struct Bike
{
private string bikeWheel;
private string bikeFrame;
public Bike(string wheel,string frame)
{
this.bikeWheel = wheel;
this.bikeFrame = frame;
}
public string GetBike
{
get
{
return bikeWheel + bikeFrame;
}
}
}
/// <summary>定義接口</summary>
public interface MainInterface
{
void CreateWheel(string bikeType);
void CreateFrame(string bikeType);
Bike GetBike();
}
/// <summary>創建自行車的各個部件</summary>
public class ConcreteClass : MainInterface
{
string wheel;
string frame;
/// <summary>創建自行車的車輪</summary>
public void CreateWheel(string bikeType)
{
switch(bikeType)
{
case "山地車":
wheel="山地車的車輪為@@@";
break;
case "賽車":
wheel="賽車的車輪為***";
break;
}
}
/// <summary>創建自行車的車架</summary>
public void CreateFrame(string bikeType)
{
switch(bikeType)
{
case "山地車":
frame="山地車的車架為^^^";
break;
case "賽車":
frame="賽車的車架為+++";
break;
}
}
/// <summary>這裡用於獲得自行車</summary>
public Bike GetBike()
{
return new Bike(wheel,frame);
}
}
/// <summary>用於組裝成不同樣式的自行車</summary>
public class CreateBike
{
private MainInterface objMain;
private string bikeType;
public CreateBike(MainInterface objMain,string bikeType)
{
this.objMain = objMain;
this.bikeType = bikeType;
}
public void Create()
{
objMain.CreateWheel(bikeType);
objMain.CreateFrame(bikeType);
}
}
public struct Bike
{
private string bikeWheel;
private string bikeFrame;
public Bike(string wheel,string frame)
{
this.bikeWheel = wheel;
this.bikeFrame = frame;
}
public string GetBike
{
get
{
return bikeWheel + bikeFrame;
}
}
}
/// <summary>定義接口</summary>
public interface MainInterface
{
void CreateWheel(string bikeType);
void CreateFrame(string bikeType);
Bike GetBike();
}
/// <summary>創建自行車的各個部件</summary>
public class ConcreteClass : MainInterface
{
string wheel;
string frame;
/// <summary>創建自行車的車輪</summary>
public void CreateWheel(string bikeType)
{
switch(bikeType)
{
case "山地車":
wheel="山地車的車輪為@@@";
break;
case "賽車":
wheel="賽車的車輪為***";
break;
}
}
/// <summary>創建自行車的車架</summary>
public void CreateFrame(string bikeType)
{
switch(bikeType)
{
case "山地車":
frame="山地車的車架為^^^";
break;
case "賽車":
frame="賽車的車架為+++";
break;
}
}
/// <summary>這裡用於獲得自行車</summary>
public Bike GetBike()
{
return new Bike(wheel,frame);
}
}
/// <summary>用於組裝成不同樣式的自行車</summary>
public class CreateBike
{
private MainInterface objMain;
private string bikeType;
public CreateBike(MainInterface objMain,string bikeType)
{
this.objMain = objMain;
this.bikeType = bikeType;
}
public void Create()
{
objMain.CreateWheel(bikeType);
objMain.CreateFrame(bikeType);
}
}
string bikeType="山地車";
ConcreteClass objCon = new ConcreteClass();
CreateBike objCB = new CreateBike(objCon,bikeType);
objCB.Create();
Bike objB = objCon.GetBike();
Response.Write(objB.GetBike);
ConcreteClass objCon = new ConcreteClass();
CreateBike objCB = new CreateBike(objCon,bikeType);
objCB.Create();
Bike objB = objCon.GetBike();
Response.Write(objB.GetBike);
以生产自行车为例,抽象工厂模式获得的是不同名牌的自行车而不管自行车是由什么部件组装而成的;而生成器模式获得是不同样式的自行车,所关心的是自行车组成部件的构成.所以看出抽象工厂模式获得是一系列相关的类(如凤凰牌自行车,XX牌自车等)而生成器模式获得的是提供给它数据(如上例中的“山地车“)一步一步构建的一个复杂的对象(即山地车)