Exemple #1
0
 static StudentSingleton()
 {
     _StudentSingleton = new StudentSingleton()
     {
         Id   = 479,
         Name = "悠悠吾心"
     };
 }
Exemple #2
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);
            }
        }
        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();
        }
Exemple #4
0
 static StudentSingleton()
 {
     _studentSingleton = new StudentSingleton();
 }