用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。
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)
暂无评论。