C#设计模式-原型模式

1.定义

用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。

2.UML图



3.代码

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Prototype p1 = new ConcretePrototype1();
            Prototype p2 = p1.Clone();
            Prototype p3 = p1.Clone();

            Console.ReadKey();
        }
    }

    /// <summary>
    /// 原型类
    /// </summary>
    abstract class Prototype
    {
        public abstract Prototype Clone();
    }

    /// <summary>
    /// 具体原型类
    /// </summary>
    class ConcretePrototype1 : Prototype
    {
        public override Prototype Clone()
        {
            Console.WriteLine("复制了一份" + this.GetType().Name);
            return (Prototype)this.MemberwiseClone();
        }
    }
}


评论: 0 | 引用: 0 | 查看次数: 5790
发表评论
登录后再发表评论!