static void Main(string[] args) { AnimalPrototype Sheep = new Sheep("Dolly"); Sheep Dolly = Sheep.Clone() as Sheep; Dolly.MakeSound(); }
void OnEnable() { Debug.Log ("------------------"); Debug.Log ("PROTOTYPE DESIGN PATTERN"); CloneFactory factory = new CloneFactory(); Sheep sally = new Sheep(); Sheep clonedSheep = (Sheep) factory.GetClone(sally); Debug.Log ("Sally: " + sally.ToString()); Debug.Log ("Clone of Sally: " + clonedSheep.ToString()); Debug.Log ("Sally Hash: " + sally.GetHashCode() + " - Cloned Sheep Hash: " + clonedSheep.GetHashCode()); }
public object Clone() { Sheep sheep = null; try { sheep = (Sheep)base.MemberwiseClone(); } catch (Exception e) { Debug.LogError("Error cloning Sheep"); } return(sheep); }
void OnEnable() { Debug.Log("------------------"); Debug.Log("PROTOTYPE DESIGN PATTERN"); CloneFactory factory = new CloneFactory(); Sheep sally = new Sheep(); Sheep clonedSheep = (Sheep)factory.GetClone(sally); Debug.Log("Sally: " + sally.ToString()); Debug.Log("Clone of Sally: " + clonedSheep.ToString()); Debug.Log("Sally Hash: " + sally.GetHashCode() + " - Cloned Sheep Hash: " + clonedSheep.GetHashCode()); }
static void Main(string[] args) { var original = new Sheep("Jolly", "Mountain Sheep"); Console.WriteLine(original.Name); // Jolly Console.WriteLine(original.Category); // Mountain Sheep var cloned = original.Clone(); cloned.Name = "Dolly"; Console.WriteLine(cloned.Name); // Dolly Console.WriteLine(cloned.Category); // Mountain Sheep Console.WriteLine(original.Name); // Dolly Console.ReadLine(); }
public Animal MakeCopy() { Console.WriteLine("Sheep is Being Made"); Sheep sheepObject = null; try { sheepObject = (Sheep)MemberwiseClone(); } catch (Exception) { throw; } return(sheepObject); }
static void Main(string[] args) { CloneFactory animalMaker = new CloneFactory(); Sheep sally = new Sheep(); Sheep clonedSheep = (Sheep)animalMaker.GetClone(sally); Console.WriteLine(sally); Console.WriteLine(clonedSheep); Console.WriteLine($"Sally Hashcode {sally.GetHashCode()}"); Console.WriteLine($"Cloned Hashcode {clonedSheep.GetHashCode()}"); Console.ReadKey(); }