Beispiel #1
0
        // Паттерн Прототип
        // Позволяет копировать объекты не вдаваясь в подробности их реализации
        static void StartPrototype()
        {
            Prototype.Person p1 = new Prototype.Person();
            p1.Age       = 42;
            p1.BirthDate = Convert.ToDateTime("1977-01-01");
            p1.Name      = "Jack Daniels";
            p1.IdInfo    = new Prototype.IdInfo(666);

            Prototype.Person p2 = p1.ShallowCopy();
            Prototype.Person p3 = p1.DeepCopy();

            Console.WriteLine("Original values of p1, p2, p3:");
            Console.WriteLine("   p1 instance values: ");
            DisplayValues(p1);
            Console.WriteLine("   p2 instance values:");
            DisplayValues(p2);
            Console.WriteLine("   p3 instance values:");
            DisplayValues(p3);

            p1.Age             = 32;
            p1.BirthDate       = Convert.ToDateTime("1900-01-01");
            p1.Name            = "Frank";
            p1.IdInfo.IdNumber = 7878;
            Console.WriteLine("\nValues of p1, p2 and p3 after changes to p1:");
            Console.WriteLine("   p1 instance values: ");
            DisplayValues(p1);
            Console.WriteLine("   p2 instance values (reference values have changed):");
            DisplayValues(p2);
            Console.WriteLine("   p3 instance values (everything was kept the same):");
            DisplayValues(p3);
Beispiel #2
0
 static void DisplayValues(Prototype.Person p)
 {
     Console.WriteLine("      Name: {0:s}, Age: {1:d}, BirthDate: {2:MM/dd/yy}",
                       p.Name, p.Age, p.BirthDate);
     Console.WriteLine("      ID#: {0:d}", p.IdInfo.IdNumber);
 }