原型模式(Prototype):用原型實例指定創建對象的種類,並且通過拷貝這些原型創建新的對象。
namespace Prototype { public abstract class Prototype { private string id; public Prototype(string id) { this.id = id; } public string Id { get { return id; } } public abstract Prototype Clone(); } public class ConcretePrototypeA:Prototype { public ConcretePrototypeA(string id):base(id) { } public override Prototype Clone() { return (Prototype)this.MemberwiseClone(); } } }View Code
測試代碼:
ConcretePrototypeA p1 = new ConcretePrototypeA("1"); ConcretePrototypeA p2 = (ConcretePrototypeA)p1.Clone(); Assert.AreEqual(p2.Id, "1");View Code