public static StudentPrototype CreateInstance()
        {
            // MemberwiseClone() 内存复制
            // 内存拷贝时 引用属性是拷贝的引用地址
            StudentPrototype model = (StudentPrototype)_StudentPrototype.MemberwiseClone();

            return(model);
        }
 static StudentPrototype()
 {
     _StudentPrototype               = new StudentPrototype();
     _StudentPrototype.Id            = 1;
     _StudentPrototype.Name          = "Eva";
     _StudentPrototype.Class.ClassId = 1;
     _StudentPrototype.Class.Name    = "basic";
 }
Beispiel #3
0
        public static StudentPrototype CreateTance()
        {
            StudentPrototype student = (StudentPrototype)_studentPrototype.MemberwiseClone();//克隆一个全新的对象返回出去

            //student.Class = new Class()//克隆之后再实列化一下Class《深克隆》
            //{
            //    ClassId = 1,
            //    ClassName = "高级班"
            //};
            return(student);
        }
Beispiel #4
0
        public static StudentPrototype CreateInstance()
        {
            StudentPrototype studentPrototype = (StudentPrototype)_StudentPrototype.MemberwiseClone();//浅克隆一个对象

            studentPrototype.Class = new Class()
            {
                Num    = 1,
                Remark = "软谋高级班"
            };    //这样就是深clone

            return(studentPrototype);
        }
Beispiel #5
0
 static StudentPrototype()
 {
     _StudentPrototype = new StudentPrototype()
     {
         Id    = 337,
         Name  = "歌神",
         Class = new Class()
         {
             Num    = 1,
             Remark = "软谋高级班"
         }
     };
 }
Beispiel #6
0
 static StudentPrototype()
 {
     _studentPrototype = new StudentPrototype()
     {
         Id    = 1,
         Name  = "test",
         Class = new Class()
         {
             ClassId   = 1,
             ClassName = "高级班"
         }
     };
 }
        //对象浅克隆
        public static StudentPrototype CreateInstance()
        {
            //MemberwiseClone创建当前object的浅表副本
            //浅克隆一个对象。对象里的值类型将被克隆一份新值,对象里的引用类型克隆的新值是相同的引用地址,两个相同的引用地址值是指向同一内存的。
            StudentPrototype studentPrototype = (StudentPrototype)_StudentPrototype.MemberwiseClone();

            //studentPrototype.Class = new Class()
            //{
            //    Num = 1,
            //    Remark = "高级班"
            //};//可以开辟内存实现深clone

            return(studentPrototype);
        }
        static void Main(string[] args)
        {
            //原型模式;浅克隆,深克隆
            Console.WriteLine("*****************浅克隆: MemberwiseClone创建当前object的浅表副本*****************");
            {
                //拷贝对象实例
                StudentPrototype student1 = StudentPrototype.CreateInstance();
                StudentPrototype student2 = StudentPrototype.CreateInstance();

                student1.Id   = 387;
                student1.Name = "天道无情";
                //修改拷贝对象实例值类型不会相互影响,不同的结果。
                Console.WriteLine("Id {0} Name {1}", student1.Id, student1.Name);
                Console.WriteLine("Id {0} Name {1}", student2.Id, student2.Name);

                //修改拷贝对象实例引用类型中的值类型会相互影响,因为拷贝的地址是相同的,通过相同的地址去修改同一块内存内容。相同的结果。
                student1.Class.Num    = 2;
                student1.Class.Remark = "特训班";
                Console.WriteLine("Class.Num {0} Class.Remark {1}", student1.Class.Num, student1.Class.Remark);
                Console.WriteLine("Class.Num {0} Class.Remark {1}", student2.Class.Num, student2.Class.Remark);
            }

            Console.WriteLine("****************通过序列化反序列话实现对象深克隆******************");
            {
                StudentPrototype student1 = StudentPrototype.CreateInstanceSerialize();
                StudentPrototype student2 = StudentPrototype.CreateInstanceSerialize();

                student1.Id   = 387;
                student1.Name = "天道无情";
                Console.WriteLine("Id {0} Name {1}", student1.Id, student1.Name);
                Console.WriteLine("Id {0} Name {1}", student2.Id, student2.Name);

                //两个互不相关的对象
                student1.Class.Num    = 3;
                student1.Class.Remark = "特训班3";
                Console.WriteLine("Class.Num {0} Class.Remark {1}", student1.Class.Num, student1.Class.Remark);
                Console.WriteLine("Class.Num {0} Class.Remark {1}", student2.Class.Num, student2.Class.Remark);
            }
            Console.ReadLine();
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            try
            {
                {
                    for (int i = 0; i < 5; i++)
                    {
                        Student student = new Student()
                        {
                            Id   = 1,
                            Name = "Ivan"
                        };
                        student.Study();
                    }
                }

                {
                    // 为了减少对象的创建, 重用对象,提升性能,所以用了单例
                    for (int i = 0; i < 5; i++)
                    {
                        StudentSingleton student = StudentSingleton.CreateInstance(); // new StudentSingleton()

                        student.Id   = 1;
                        student.Name = "Ivan";

                        student.Study();
                    }
                }

                {
                    // 单例会导致数据被覆盖,因为student 和studentNew都指向同一个对象

                    StudentSingleton student = StudentSingleton.CreateInstance(); // new StudentSingleton()

                    student.Id   = 1;
                    student.Name = "Ivan";

                    student.Study();

                    StudentSingleton studentNew = StudentSingleton.CreateInstance(); // new StudentSingleton()

                    studentNew.Id   = 12;
                    studentNew.Name = "Ivan123";

                    studentNew.Study();
                }

                {
                    // 1. 避免重复创建对象,重复调用构造函数问题 -- 内存拷贝
                    // 2. 避免同一个对象覆盖的问题 -- 不同对象


                    StudentPrototype student = StudentPrototype.CreateInstance(); // new StudentSingleton()

                    student.Id   = 1;
                    student.Name = "Ivan";

                    student.Study();

                    StudentPrototype studentNew = StudentPrototype.CreateInstance(); // new StudentSingleton()

                    studentNew.Id   = 12;
                    studentNew.Name = "Ivan123";

                    studentNew.Study();
                }

                {
                    StudentPrototype student = StudentPrototype.CreateInstance(); // new StudentSingleton()

                    student.Id   = 1;
                    student.Name = "Ivan";

                    student.Study();

                    StudentPrototype studentNew = StudentPrototype.CreateInstance(); // new StudentSingleton()

                    studentNew.Id   = 12;
                    studentNew.Name = "Ivan123";

                    studentNew.Study();

                    StudentPrototype studentDouble = StudentPrototype.CreateInstance(); // new StudentSingleton()

                    studentDouble.Id   = 123;
                    studentDouble.Name = "Ivan12342";
                    // 会覆盖StudentPrototype中初始化的班级信息,因为浅拷贝只拷贝了引用地址
                    studentDouble.Class.ClassId = 2;
                    studentDouble.Class.Name    = "Advanced";

                    studentDouble.Study();
                    // 内存拷贝时 引用属性是拷贝的引用地址, 多个student的class指向的都是同一块内存
                    // 所以一个变化,全部都会变化
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Beispiel #10
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班公开课之设计模式特训,今天是Eleven老师为大家带来的原型模式PrototypePattern");
                //Console.WriteLine("***************************");
                //for (int i = 0; i < 5; i++)
                //{
                //    Student student = new Student()
                //    {
                //        Id = 530,
                //        Name = "530-小昶-女-海口"
                //    };
                //    student.Study();
                //}
                //Console.WriteLine("***************************");
                //for (int i = 0; i < 5; i++)
                //{
                //    StudentSingleton student = StudentSingleton.CreateInstance();
                //    student.Study();
                //}
                //Console.WriteLine("***************************");
                //for (int i = 0; i < 5; i++)
                //{
                //    StudentPrototype student = StudentPrototype.CreateInstance();
                //    student.Study();
                //}
                Console.WriteLine("**********************************");
                {
                    StudentSingleton student1 = StudentSingleton.CreateInstance();
                    StudentSingleton student2 = StudentSingleton.CreateInstance();

                    student1.Id   = 506;
                    student1.Name = "yoyo";
                    Console.WriteLine("Id {0} Name {1}", student1.Id, student1.Name);
                    Console.WriteLine("Id {0} Name {1}", student2.Id, student2.Name);
                }

                Console.WriteLine("**********************************");
                {
                    StudentPrototype student1 = StudentPrototype.CreateInstance();
                    StudentPrototype student2 = StudentPrototype.CreateInstance();

                    student1.Id   = 387;
                    student1.Name = "天道无情";//==new String("天道无情")
                    Console.WriteLine("Id {0} Name {1}", student1.Id, student1.Name);
                    Console.WriteLine("Id {0} Name {1}", student2.Id, student2.Name);

                    student1.Class.Num    = 2;
                    student1.Class.Remark = "特训班";
                    //student1.Class = new Class()
                    //{
                    //    Num = 2,
                    //    Remark = "特训班"
                    //};

                    Console.WriteLine("Class.Num {0} Class.Remark {1}", student1.Class.Num, student1.Class.Remark);
                    Console.WriteLine("Class.Num {0} Class.Remark {1}", student2.Class.Num, student2.Class.Remark);
                }

                Console.WriteLine("**********************************");
                {
                    StudentPrototype student1 = StudentPrototype.CreateInstanceSerialize();
                    StudentPrototype student2 = StudentPrototype.CreateInstanceSerialize();

                    student1.Id   = 387;
                    student1.Name = "天道无情";//==new String("天道无情")
                    Console.WriteLine("Id {0} Name {1}", student1.Id, student1.Name);
                    Console.WriteLine("Id {0} Name {1}", student2.Id, student2.Name);

                    student1.Class.Num    = 2;
                    student1.Class.Remark = "特训班";

                    Console.WriteLine("Class.Num {0} Class.Remark {1}", student1.Class.Num, student1.Class.Remark);
                    Console.WriteLine("Class.Num {0} Class.Remark {1}", student2.Class.Num, student2.Class.Remark);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }