设计模式:原型模式
原型模式(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(); } } }
测试代码:
ConcretePrototypeA p1 = new ConcretePrototypeA("1"); ConcretePrototypeA p2 = (ConcretePrototypeA)p1.Clone(); Assert.AreEqual(p2.Id, "1");