示例#1
0
        private static void Prototype()
        {
            CloneFactory animalMaker = new CloneFactory();
            Sheep        sheep       = new Sheep("Scooby Doo");
            Sheep        clonedSheep = (Sheep)animalMaker.GetClone(sheep);

            //check the type of object created for both sheep and cloned sheep instances
            Console.WriteLine(sheep);
            Console.WriteLine(clonedSheep);

            //Check if sheep and its cloned object have the same property values
            Console.WriteLine("The sheep name is " + sheep.Name);
            Console.WriteLine("The cloned sheep name is " + clonedSheep.Name);

            // if the hashcodes are different, then these objects were stored in different memory addresses.
            // that will mean that, we have successfully created a copy or clone of the current sheep(scooby doo) object
            Console.WriteLine("Sheep Hashcode is: " + sheep.GetHashCode());
            Console.WriteLine("Cloned Sheep Hashcode is: " + clonedSheep.GetHashCode());
        }