Design
Pattern : Prototype
1.
IPrototype
介面
public interface IPrototype
{
IPrototype Clone();
}
public interface IPrototype
{
IPrototype Clone();
}
2. PrototypeManager 類別:存放Prototype的地方
/// <summary>
/// 存放及使用Prototype的PrototypeManager
/// </summary>
public class PrototypeManager
{
/// <summary>
/// Prototype容器
/// </summary>
private Dictionary<String, IPrototype> prototypes = null;
public PrototypeManager()
{
prototypes = new Dictionary<String, IPrototype>();
}
/// 加入新的Prototype
public void Add(String id, IPrototype pt)
{
prototypes.Add(id, pt);
}
/// 由ID取得特定Prototype
public IPrototype Get(String id)
{
return this.prototypes[id];
}
}
/// <summary>
/// 存放及使用Prototype的PrototypeManager
/// </summary>
public class PrototypeManager
{
/// <summary>
/// Prototype容器
/// </summary>
private Dictionary<String, IPrototype> prototypes = null;
public PrototypeManager()
{
prototypes = new Dictionary<String, IPrototype>();
}
/// 加入新的Prototype
public void Add(String id, IPrototype pt)
{
prototypes.Add(id, pt);
}
/// 由ID取得特定Prototype
public IPrototype Get(String id)
{
return this.prototypes[id];
}
}
3. 接下來我們實際實作ICar的兩個車子,並實作IPrototype。
public class Wish : ICar , IPrototype
{
private const String BRAND = "TOYOTA";
public Wish()
{
//取得當前類別名稱
this.Name = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
}
/// Clone
public IPrototype Clone()
{
///建立一台新的Wish的Prototype
Wish myWish = new Wish();
myWish.Color = Color.Black;
myWish.Name = "Wish (prototype)";
return myWish;
}
public int index { get; set; }
public Color Color { get; set; }
public string Name { get; set; }
public string Equipment { get; set; }
public void Drive()
{
Console.WriteLine("I am driving {0} {1}({2}) {3}",
this.Color.ToString(), this.Name, BRAND, (this.Equipment ?? ""));
}
}
=>另外一個Class : Camry.cs 就省略了,這邊可以看出Prototype pattern的好處了,直接在Clone()函式建立好車子出廠的顏色和名稱…等等。
public class Wish : ICar , IPrototype
{
private const String BRAND = "TOYOTA";
public Wish()
{
//取得當前類別名稱
this.Name = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
}
/// Clone
public IPrototype Clone()
{
///建立一台新的Wish的Prototype
Wish myWish = new Wish();
myWish.Color = Color.Black;
myWish.Name = "Wish (prototype)";
return myWish;
}
public int index { get; set; }
public Color Color { get; set; }
public string Name { get; set; }
public string Equipment { get; set; }
public void Drive()
{
Console.WriteLine("I am driving {0} {1}({2}) {3}",
this.Color.ToString(), this.Name, BRAND, (this.Equipment ?? ""));
}
}
=>另外一個Class : Camry.cs 就省略了,這邊可以看出Prototype pattern的好處了,直接在Clone()函式建立好車子出廠的顏色和名稱…等等。
4. 主程式:
ICar wish = new Wish();
ICar camry = new Camry();
PrototypeManager ptManager = new PrototypeManager();
ptManager.Add("Wish", wish as IPrototype);
ptManager.Add("Camry", camry as IPrototype);
ICar wish2 = ptManager.Get("Wish").Clone() as ICar;
ICar wish3 = ptManager.Get("Wish").Clone() as ICar;
wish.Color = Color.White;
wish.Drive();
wish2.Drive();
wish3.Drive();
ICar camry2 = ptManager.Get("Camry").Clone() as ICar;
ICar camry3 = ptManager.Get("Camry").Clone() as ICar;
camry.Color = Color.White;
camry.Drive();
camry2.Drive();
camry3.Drive();
結果:
I am driving White Wish(TOYOTA)
I am driving Black Wish (prototype)(TOYOTA)
I am driving Black Wish (prototype)(TOYOTA)
I am driving White Camry(TOYOTA)
I am driving Blue Camry (prototype)(TOYOTA)
I am driving Blue Camry (prototype)(TOYOTA)
ICar wish = new Wish();
ICar camry = new Camry();
PrototypeManager ptManager = new PrototypeManager();
ptManager.Add("Wish", wish as IPrototype);
ptManager.Add("Camry", camry as IPrototype);
ICar wish2 = ptManager.Get("Wish").Clone() as ICar;
ICar wish3 = ptManager.Get("Wish").Clone() as ICar;
wish.Color = Color.White;
wish.Drive();
wish2.Drive();
wish3.Drive();
ICar camry2 = ptManager.Get("Camry").Clone() as ICar;
ICar camry3 = ptManager.Get("Camry").Clone() as ICar;
camry.Color = Color.White;
camry.Drive();
camry2.Drive();
camry3.Drive();
結果:
I am driving White Wish(TOYOTA)
I am driving Black Wish (prototype)(TOYOTA)
I am driving Black Wish (prototype)(TOYOTA)
I am driving White Camry(TOYOTA)
I am driving Blue Camry (prototype)(TOYOTA)
I am driving Blue Camry (prototype)(TOYOTA)
5.
結論:
如果new一個物件時, 需要做很多細節的設定,可以使用Prototype 快速的Clone出一個原型。 針對需求再做調整即可。
如果new一個物件時, 需要做很多細節的設定,可以使用Prototype 快速的Clone出一個原型。 針對需求再做調整即可。
沒有留言:
張貼留言