Skip to content

Commit 39b44f6

Browse files
committed
Prototype Pattern example2
Prototype Pattern example2
1 parent 6db7da5 commit 39b44f6

16 files changed

+773
-0
lines changed
Binary file not shown.

Assets/Creational Patterns/Prototype Pattern/Example2.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//-------------------------------------------------------------------------------------
2+
// PrototypePatternExample2.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
using System;
8+
9+
namespace PrototypePatternExample2
10+
{
11+
public class PrototypePatternExample2 : MonoBehaviour
12+
{
13+
void Start()
14+
{
15+
CloneFactory factory = new CloneFactory();
16+
17+
Sheep sally = new Sheep();
18+
19+
Sheep clonedSheep = (Sheep)factory.GetClone(sally);
20+
21+
Debug.Log("Sally: " + sally.ToString());
22+
Debug.Log("Clone of Sally: " + clonedSheep.ToString());
23+
Debug.Log("Sally Hash: " + sally.GetHashCode() + " - Cloned Sheep Hash: " + clonedSheep.GetHashCode());
24+
}
25+
26+
}
27+
28+
public class CloneFactory
29+
{
30+
public IAnimal GetClone(IAnimal animalSample)
31+
{
32+
return (IAnimal)animalSample.Clone();
33+
}
34+
}
35+
36+
public interface IAnimal : ICloneable
37+
{
38+
object Clone();
39+
}
40+
41+
public class Sheep : IAnimal
42+
{
43+
public Sheep()
44+
{
45+
Debug.Log("Made Sheep");
46+
}
47+
48+
public object Clone()
49+
{
50+
Sheep sheep = null;
51+
52+
try
53+
{
54+
sheep = (Sheep)base.MemberwiseClone();
55+
}
56+
catch (Exception e)
57+
{
58+
Debug.LogError("Error cloning Sheep");
59+
}
60+
61+
return sheep;
62+
}
63+
64+
public string ToString()
65+
{
66+
return "Hello I'm a Sheep";
67+
}
68+
}
69+
70+
}
71+

Assets/Creational Patterns/Prototype Pattern/Example2/PrototypePatternExample2.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Creational Patterns/Prototype Pattern/Example2/PrototypePatternExample2.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
4.23 KB
Binary file not shown.
611 Bytes
Binary file not shown.
4.28 KB
Binary file not shown.
658 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)