Abstract
Factory 與Singleton、Builder的結合
延伸底下兩篇文章的範例
我們在Abstract Factory加入Singleton及Builder pattern,來加入他們的優點。
步驟為:
1.
修改Abstract Factory為Singleton
2.
在實作Abstract Factory的factory Class,更改建立方法改使用Buider方式建立物件。
程式架構:
1.
Abstract Factory (interface)
public interface ICarFactory
{
F Create<F, T>(String _name, Color _color) where T:F, new();
}
public interface ICarFactory
{
F Create<F, T>(String _name, Color _color) where T:F, new();
}
2.
Abstract Factory (實作), 在這邊加入Singleton (Eager type)。
並在Create的時候,改使用Builder方式。
public class LexxxFactory:ICarFactory
{
#region Singleton
static ICarFactory instance = new LexxxFactory();
public static ICarFactory GetInstance
{
get
{ return instance; }
}
#endregion
public F Create<F, T>(String _name, Color _color) where T : F, new()
{
///宣告Builder
ICarBuilder carBuilder = new LexxxBuilder(_name, _color);
///宣告Director
using (BuildCarDirector director = new BuildCarDirector(carBuilder))
{
///取得ICarMidRange
dynamic myCar = director.GetCar();
Console.WriteLine(
String.Format("車體序號 {0},客戶 {1},業務 {2}",
myCar.CarUid, myCar.CustomerName, myCar.SalesName));
return myCar;
}
}
並在Create的時候,改使用Builder方式。
public class LexxxFactory:ICarFactory
{
#region Singleton
static ICarFactory instance = new LexxxFactory();
public static ICarFactory GetInstance
{
get
{ return instance; }
}
#endregion
public F Create<F, T>(String _name, Color _color) where T : F, new()
{
///宣告Builder
ICarBuilder carBuilder = new LexxxBuilder(_name, _color);
///宣告Director
using (BuildCarDirector director = new BuildCarDirector(carBuilder))
{
///取得ICarMidRange
dynamic myCar = director.GetCar();
Console.WriteLine(
String.Format("車體序號 {0},客戶 {1},業務 {2}",
myCar.CarUid, myCar.CustomerName, myCar.SalesName));
return myCar;
}
}
3.
至於Builder的部分,請直接參考原文章了,幾乎全部不變。
只有因為Abstract factory建立物件時有帶入一些車子基本的參數(String _name, Color _color), 所以Builder實作類別稍作調整:
public class LexxxBuilder:ICarBuilder
{
private String name;
private Color color;
public LexxxBuilder(String _name, Color _color)
{
this.name = _name;
this.color = _color;
}
public F Create<F, T>() where T : F, new()
{
dynamic car = new T();
car.Name = this.name;
car.Color = this.color;
return car;
}
//Skip other implementations...
}
只有因為Abstract factory建立物件時有帶入一些車子基本的參數(String _name, Color _color), 所以Builder實作類別稍作調整:
public class LexxxBuilder:ICarBuilder
{
private String name;
private Color color;
public LexxxBuilder(String _name, Color _color)
{
this.name = _name;
this.color = _color;
}
public F Create<F, T>() where T : F, new()
{
dynamic car = new T();
car.Name = this.name;
car.Color = this.color;
return car;
}
//Skip other implementations...
}
4.
主程式:
ICarMidRange car =
LexxxFactory.GetInstance.Create<ICarMidRange, Is300h>("IS300H", Color.Yellow);
car.Drive();
結果
車體序號 75780771,客戶 Darth Vader,業務 Luke Skywalker
I am driving Yellow IS300H(Lexxx)
ICarMidRange car =
LexxxFactory.GetInstance.Create<ICarMidRange, Is300h>("IS300H", Color.Yellow);
car.Drive();
結果
車體序號 75780771,客戶 Darth Vader,業務 Luke Skywalker
I am driving Yellow IS300H(Lexxx)
5.
結束
沒有留言:
張貼留言