Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Persona p1 = new Persona("Antonio", "Pelleriti");

            p1.Contatti.Add(new Contatto(ContactType.Email, "*****@*****.**"));

            Console.WriteLine(p1);

            Persona p2 = p1;

            p2.SetNome("Anto");
            Console.WriteLine(p2);
            Console.WriteLine(p1);


            Persona p3 = (Persona)p1.Clone();

            p3.Contatti[0].Text = "*****@*****.**";
            Console.WriteLine(p1);
            Console.WriteLine(p3);


            Bullet           b       = new Bullet("#ff0000", 1);
            PrototypeManager manager = new PrototypeManager();

            manager.AddPrototype("red", b);

            Bullet clone1 = (Bullet)manager.GetPrototype("red");
            Bullet clone2 = (Bullet)manager.GetPrototype("red");
            Bullet clone3 = (Bullet)manager.GetPrototype("red");

            Console.Read();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            PrototypeManager manager = new PrototypeManager();
            Prototype        c2, c3;

            c2 = manager.prototypes["Australia"].Clone();
            Report("Shallow cloning Australia\n===============", manager.prototypes["Australia"], c2);

            c2.Capital = "Sydney";
            Report("Altered Clone's shallow state: prototype unaffected", manager.prototypes["Australia"], c2);

            c2.Language.Data = "Chinese";
            Report("Altering Clone deep state: prototype affected ******", manager.prototypes["Australia"], c2);

            c3 = manager.prototypes["Germany"].DeepCopy();
            Report("Deep cloning Germany\n===============", manager.prototypes["Germany"], c3);

            c3.Capital = "Munich";
            Report("Altering Clone shallow state: prototype unaffected", manager.prototypes["Germany"], c3);

            c3.Language.Data = "Turkish";
            Report("Altering Clone deep state: prototype unaffected", manager.prototypes["Germany"], c3);

            Console.ReadLine();
        }
Exemplo n.º 3
0
        static void Main()
        {
            PrototypeManager manager = new PrototypeManager();
            Prototype        c2, c3;

            // Make a copy of Australia's data
            c2 = manager["Australia"].Clone();
            Report("Shallow cloning Australia\n===============",
                   manager["Australia"], c2);

            // Change the capital of Australia to Sydney
            c2.Capital = "Sydney";
            Report("Altered Clone's shallow state, prototype unaffected",
                   manager["Australia"], c2);

            // Change the language of Australia (deep data)
            c2.Language.Data = "Chinese";
            Report("Altering Clone deep state: prototype affected *****",
                   manager["Australia"], c2);

            // Make a copy of Germany's data
            c3 = manager.prototypes["Germany"].DeepCopy();
            Report("Deep cloning Germany\n============",
                   manager["Germany"], c3);

            // Change the capital of Germany
            c3.Capital = "Munich";
            Report("Altering Clone shallow state, prototype unaffected",
                   manager["Germany"], c3);

            // Change the language of Germany (deep data)
            c3.Language.Data = "Turkish";
            Report("Altering Clone deep state, prototype unaffected",
                   manager["Germany"], c3);


            const uint NoOfIteration = 1_000;

            Profiler.Profile("Deep Copy via Serialization", NoOfIteration, () =>
            {
                var x = manager["Australia"].DeepCopy();
            });

            Profiler.Profile("Deep Copy via Constructor", NoOfIteration, () =>
            {
                var x = new Prototype(manager["Australia"], true);
            });

            Profiler.Profile("Shallow Copy via Serialization", NoOfIteration, () =>
            {
                var x = manager["Australia"].Clone();
            });

            Profiler.Profile("Shallow Copy via Constructor", NoOfIteration, () =>
            {
                var x = new Prototype(manager["Australia"], false);
            });
        }
Exemplo n.º 4
0
        public static void TestClone()
        {
            PrototypeManager mgr = new PrototypeManager();
            Prototypes       c2, c3;

            c2 = mgr.lstPrototype["Australia"].clone();
            Report("Shallow Cloning \n", mgr.lstPrototype["Australia"], c2);

            c2.Capital = "Sydney";
            Report("Shallow Cloning Unaffected \n", mgr.lstPrototype["Australia"], c2);

            c2.Language.Data = "Chinese";
            Report("Shallow Cloning Unaffected \n", mgr.lstPrototype["Australia"], c2);

            c3 = mgr.lstPrototype["German"].DeepCopy();

            Report("Deep Cloning \n", mgr.lstPrototype["German"], c3);

            c3.Capital = "Munich";
            Report("Deep Cloning Unaffected \n", mgr.lstPrototype["German"], c3);

            c3.Language.Data = "Turkish";
            Report("Deep Cloning Unaffected \n", mgr.lstPrototype["German"], c3);
        }