原型模式(Prototype)

作者:追风剑情 发布于:2016-6-15 14:09 分类:设计模式

using System;
using System.Collections.Generic;
using System.Text;

namespace PrototypeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ConcretePrototype obj = new ConcretePrototype();
            obj.Name = "aaa";

            ConcretePrototype obj1 = (ConcretePrototype)obj.Clone();
            Console.WriteLine(obj1.Name);

            Console.Read();
        }
    }

    //Clone接口声明
    public interface ICloneable
    {
        Object Clone();
    }

    //原型类
    public abstract class Prototype : ICloneable
    {
        public abstract Object Clone();
    }

    //具体实现类
    public class ConcretePrototype : Prototype
    {
        public string Name;

        public override Object Clone()
        {
            ConcretePrototype obj = new ConcretePrototype();
            obj.Name = Name;
            return obj;
        }
    }
}

运行测试

111111.png

标签: 设计模式

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号