zoukankan      html  css  js  c++  java
  • C# 原型模式(Prototype)

    理解:通过在类中定义一个Clone方法克隆自己,分为深COPY 和 浅COPY; 现在NET中,继承了ICloneable接口的类都可以重写Clone()方法。

    代码:

    //原型类
        [Serializable]
        public abstract class PrototypeClass
        {
            public string _myValue;

            public string MyValue
            {
                get { return _myValue; }
                set { _myValue = value; }
            }
            public abstract PrototypeClass Clone();
        }

        //浅拷贝
        public class ShallowClone:PrototypeClass
        {
            public ShallowClone(string value)
            {
                this._myValue = value;
            }

            public override PrototypeClass Clone()
            {
                return (PrototypeClass)this.MemberwiseClone();
            }
        }

        //深拷贝
        [Serializable]
        public class DeepClone : PrototypeClass
        {
            public DeepClone(string value)
            {
                this._myValue = value;
            }

            public override PrototypeClass Clone()
            {
                PrototypeClass deepObject;
                MemoryStream memoryStream = new MemoryStream();
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(memoryStream, this);
                memoryStream.Position = 0;
                deepObject = (PrototypeClass)formatter.Deserialize(memoryStream);

                return deepObject;
            }
         }
  • 相关阅读:
    上海汉得面试:
    二叉树的遍历
    操作系统知识总结
    mysql单表查询&&多表查询(职员表14+9)
    数据库查询
    数据库设计三大范式及事务
    某硕笔试题mysql数据库部分(较为全面)
    java 读取excel 将数据插入到数据库
    java 读取excel 正常 xls
    java 读取excel(Map结构)xls
  • 原文地址:https://www.cnblogs.com/kavilee/p/2362429.html
Copyright © 2011-2022 走看看