设计模式:原型模式

原型模式(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

 

posted @ 2016-03-08 16:09  uptothesky  阅读(127)  评论(0编辑  收藏  举报