木子屋 Dnawo's BLOG

C#设计模式-原型模式

👤 dnawo 📅 2010-03-15 👁 7106 👍 0 💬 0 🔄 本站原创
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)

暂无评论。

计算题
评论需审核通过后显示
← 上一篇 PKG使用UTF-8编码导致makesis生成sis失败 下一篇 → 江礼坤:网络创业不能随大流和炒冷饭