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

     原型模式核心就是,深拷贝和浅拷贝。

           原型模式是创建模式中的一种,用于特点是通过一个已存在的实例复制一个新实例,而不是创建新实例(值类型用浅拷贝,而引用类型必须是深拷贝)。被复制的对象我们称作原型,这个原型是可定制的。

    .net实现深拷贝

      public class DeepCope
        {
            /// <summary>
            /// 对象拷贝
            /// </summary>
            /// <param name="obj">被复制对象</param>
            /// <returns>新对象</returns>
            private object CopyOjbect(object obj)
            {
                if (obj == null)
                {
                    return null;
                }
                Object targetDeepCopyObj;
                Type targetType = obj.GetType();
                //值类型  
                if (targetType.IsValueType == true)
                {
                    targetDeepCopyObj = obj;
                }
                //引用类型   
                else
                {
                    targetDeepCopyObj = System.Activator.CreateInstance(targetType);   //创建引用对象   
                    System.Reflection.MemberInfo[] memberCollection = obj.GetType().GetMembers();
    
                    foreach (System.Reflection.MemberInfo member in memberCollection)
                    {
                        //拷贝字段
                        if (member.MemberType == System.Reflection.MemberTypes.Field)
                        {
                            System.Reflection.FieldInfo field = (System.Reflection.FieldInfo)member;
                            Object fieldValue = field.GetValue(obj);
                            if (fieldValue is ICloneable)
                            {
                                field.SetValue(targetDeepCopyObj, (fieldValue as ICloneable).Clone());
                            }
                            else
                            {
                                field.SetValue(targetDeepCopyObj, CopyOjbect(fieldValue));
                            }
    
                        }//拷贝属性
                        else if (member.MemberType == System.Reflection.MemberTypes.Property)
                        {
                            System.Reflection.PropertyInfo myProperty = (System.Reflection.PropertyInfo)member;
    
                            MethodInfo info = myProperty.GetSetMethod(false);
                            if (info != null)
                            {
                                try
                                {
                                    object propertyValue = myProperty.GetValue(obj, null);
                                    if (propertyValue is ICloneable)
                                    {
                                        myProperty.SetValue(targetDeepCopyObj, (propertyValue as ICloneable).Clone(), null);
                                    }
                                    else
                                    {
                                        myProperty.SetValue(targetDeepCopyObj, CopyOjbect(propertyValue), null);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    // ignored
                                }
                            }
                        }
                    }
                }
                return targetDeepCopyObj;
            }
        }
    View Code
  • 相关阅读:
    Ubuntu ctrl+alt会导致窗口还原的问题
    Ubuntu设置显示桌面快捷键
    ubuntu鼠标和触摸板的禁用
    Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
    友盟社会化分享
    ZOJ 3890 Wumpus
    九种迹象表明你该用Linux了
    Java集合源代码剖析(二)【HashMap、Hashtable】
    01_GIT基础、安装
    Launcher知识的demo,手机管家小伙家与悬浮窗
  • 原文地址:https://www.cnblogs.com/Blogs-Wang/p/6426802.html
Copyright © 2011-2022 走看看