鸟语天空
原型模式(Prototype)
post by:追风剑情 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

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容