zoukankan      html  css  js  c++  java
  • 设计模式-原型模式

    前言

    点击查看:设计模式系列 

    原型模式(Prototype)

    理解:原型模式属于创建型模式,与工厂,单件,生成器模式有相似点,就是创建对象,而原型模式最大的特点就是对一个基类对象进行克隆复制创建出模型一样的副本,进行操作。

    举例:

    即将开学啦,就来个入学考试吧

    基对象(一般为接口,抽象类):考试题(样卷)

    原型模式的复职克隆:根据需要印刷考卷,这里的考卷都是复制考试题样卷

    客户端:学生答卷,同一套试卷,学生做题不可能一模一样

    类图:

    接口:试卷样例代码

       /// <summary>
        /// 选答题
        /// </summary>
        public class SelectTest
        {
            private string other;
            public string 你老婆多大
            {
                get
                {
                    return this.other;
                }
                set
                {
                    this.other = value;
                }
            }
        }
        /// <summary>
        /// 面试题
        /// </summary>
        public interface Itest
        {
            Itest Clone();
    
            string 知道设计模式吗
            {
                get;
                set;
            }
            string 设计模式有几种
            {
                get;
                set;
            }
            string 你知道那些
            {
                get;
                set;
            }
            SelectTest 附加题
            {
                get;
                set;
            }
    
            Test Test
            {
                get;
                set;
            }
    
            Test Test1
            {
                get;
                set;
            }
        }

    复制克隆:复印机

     /// <summary>
        /// 继承Itest接口
        /// </summary>
        public class Test : Itest
        {
            private string one;
            private string two;
            private string three;
            private SelectTest other=new SelectTest();
            public string 知道设计模式吗
            {
                get
                {
                    return this.one;
                }
                set
                {
                    this.one = value;
                }
            }
            public string 设计模式有几种
            {
                get
                {
                    return this.two;
                }
                set
                {
                    this.two = value;
                }
            }
            public string 你知道那些
            {
                get
                {
                    return this.three;
                }
                set
                {
                    this.three = value;
                }
            }
            public SelectTest 附加题
            {
                get
                {
                    return this.other;
                }
                set
                {
                    this.other = value;
                }
            }
            #region IColorDemo 成员
    
            public Itest Clone()
            {
                //克隆当前类
                return (Itest)this.MemberwiseClone();
            }
            #endregion
        }

    客户端,发卷做题

      static void Main()
            {
                //印刷试卷
                Itest test = new Test();
                //复制样本试卷
                Itest test1 = test.Clone();
                
                //考生1
                test.设计模式有几种 = "23";
                test.附加题.你老婆多大 = "18";
    
                //考生2
                test1.设计模式有几种 = "24";
                test1.附加题.你老婆多大 = "20";
    
                //显示考生答卷内容
                Console.WriteLine("test设计模式有几种:" + test.设计模式有几种);    //23
                Console.WriteLine("test附加题.你老婆多大:" + test.附加题.你老婆多大);    //20
                Console.WriteLine("test1设计模式有几种:" + test1.设计模式有几种);   //24
                Console.WriteLine("test1附加题.你老婆多大:" + test1.附加题.你老婆多大);  //20
    
                Console.ReadKey();
            }

    注意:这里两个人答得不一样,为什么附加题中,老婆年龄都为20?

    这里涉及到深拷贝,浅拷贝问题,值类型是放在栈上的,拷贝之后,会自会在站上重新add一个,而class属于引用类型,拷贝之后,栈上重新分配啦一个指针,可指针却指向同一个位置的资源。浅拷贝,只拷贝值类型,深拷贝,引用类型也拷贝复制。

    解决方案: 

      public Itest Clone()
            {
                //克隆当前类
                Itest itst= (Itest)this.MemberwiseClone();
                SelectTest st = new SelectTest();
                st.你老婆多大 = this.other.你老婆多大;
                itst.附加题 = st;
                return itst;
    
            } 

    使用序列化解决

    /// <summary>
        /// 选答题
        /// </summary>
        [Serializable] 
        public class SelectTest
        {
            private string other;
            public string 你老婆多大
            {
                get
                {
                    return this.other;
                }
                set
                {
                    this.other = value;
                }
            }
        }
        /// <summary>
        /// 面试题
        /// </summary>
        public interface Itest
        {
            Itest Clone();
    
            string 知道设计模式吗
            {
                get;
                set;
            }
            string 设计模式有几种
            {
                get;
                set;
            }
            string 你知道那些
            {
                get;
                set;
            }
            SelectTest 附加题
            {
                get;
                set;
            }
         
        }
    
        /// <summary>
        /// 继承Itest接口
        /// </summary>
        [Serializable] 
        public class Test : Itest
        {
            private string one;
            private string two;
            private string three;
            private SelectTest other=new SelectTest();
            public string 知道设计模式吗
            {
                get
                {
                    return this.one;
                }
                set
                {
                    this.one = value;
                }
            }
            public string 设计模式有几种
            {
                get
                {
                    return this.two;
                }
                set
                {
                    this.two = value;
                }
            }
            public string 你知道那些
            {
                get
                {
                    return this.three;
                }
                set
                {
                    this.three = value;
                }
            }
            public SelectTest 附加题
            {
                get
                {
                    return this.other;
                }
                set
                {
                    this.other = value;
                }
            }
    
            
            public Itest Clone()
            {
                SerializableHelper SerializableHelper = new 原型模式.SerializableHelper();
                string target = SerializableHelper.Serializable(this);
                return SerializableHelper.Derializable<Itest>(target); 
    
            }
    
        }
    View Code
      public class SerializableHelper
        {
            public string Serializable(object target)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    new BinaryFormatter().Serialize(stream, target);
    
                    return Convert.ToBase64String(stream.ToArray());
                }
            }
    
            public object Derializable(string target)
            {
                byte[] targetArray = Convert.FromBase64String(target);
    
                using (MemoryStream stream = new MemoryStream(targetArray))
                {
                    return new BinaryFormatter().Deserialize(stream);
                }
            }
    
            public T Derializable<T>(string target)
            {
                return (T)Derializable(target);
            }
        }
    View Code

    答卷结束,总结原型模式

    原型模式的用处很多,比如现在比较流行的orm技术,修改,编辑前一个原型带数据的,克隆原型样例然后对其复制,然后再让这两个编辑前后的对象做对比,如果相同就不修改,不同则生成sql语句经行update。原型模式的身形还是很常见的,因为他创建新对象方便快捷,而且可在运行时根据需要通过克隆来添加和去除他们,也可在程序运行是根据情况来修改类内部的数据,而且运行时无需创建类就能够得到你想要的新生对象。

    欢迎加入博客左上方群,交流探讨,设计模式,数据库,c#.net,数据结构。

  • 相关阅读:
    Unity 3D第三人称视角、用途广泛限定角度(视角不能360度翻转)
    Android Studio安卓导出aar包与Unity 3D交互
    Unity 3D调用Windows打开、保存窗口、文件浏览器
    安卓与Unity交互之-Android Studio创建Module库模块教程
    Unity 3D与Android Studio安卓交互之-导出jar包
    C#字符串string以及相关内置函数
    Unity 3D委托entrust
    数据结构与算法学习一
    .NET Core学习一--Powered by .NET Core on Kubernetes
    easyui.form
  • 原文地址:https://www.cnblogs.com/knowledgesea/p/3930009.html
Copyright © 2011-2022 走看看