diff --git a/Assets/Creational Patterns/Prototype Pattern/Example1/PrototypePatternExample1.cs b/Assets/Creational Patterns/Prototype Pattern/Example1/PrototypePatternExample1.cs
index 2cda3ed..047a3e0 100644
--- a/Assets/Creational Patterns/Prototype Pattern/Example1/PrototypePatternExample1.cs
+++ b/Assets/Creational Patterns/Prototype Pattern/Example1/PrototypePatternExample1.cs
@@ -15,72 +15,62 @@ public class PrototypePatternExample1 : MonoBehaviour
{
void Start()
{
- ColorManager colormanager = new ColorManager();
+ var colorManager = new ColorManager();
- // Initialize with standard colors
- colormanager["red"] = new Color(255, 0, 0);
- colormanager["green"] = new Color(0, 255, 0);
- colormanager["blue"] = new Color(0, 0, 255);
+ // Initialize standard colors
+ colorManager.SetColor("red", new Color(255, 0, 0));
+ colorManager.SetColor("green", new Color(0, 255, 0));
+ colorManager.SetColor("blue", new Color(0, 0, 255));
- // User adds personalized colors
- colormanager["angry"] = new Color(255, 54, 0);
- colormanager["peace"] = new Color(128, 211, 128);
- colormanager["flame"] = new Color(211, 34, 20);
-
- // User clones selected colors
- Color color1 = colormanager["red"].Clone() as Color;
- Color color2 = colormanager["peace"].Clone() as Color;
- Color color3 = colormanager["flame"].Clone() as Color;
+ // User-defined colors
+ colorManager.SetColor("angry", new Color(255, 54, 0));
+ colorManager.SetColor("peace", new Color(128, 211, 128));
+ colorManager.SetColor("flame", new Color(211, 34, 20));
+ // Clone colors
+ var color1 = (Color)colorManager.GetColor("red").Clone();
+ var color2 = (Color)colorManager.GetColor("peace").Clone();
+ var color3 = (Color)colorManager.GetColor("flame").Clone();
}
}
- ///
- /// The 'Prototype' abstract class
- ///
abstract class ColorPrototype
{
public abstract ColorPrototype Clone();
}
- ///
- /// The 'ConcretePrototype' class
- ///
class Color : ColorPrototype
{
private int _red;
private int _green;
private int _blue;
- // Constructor
public Color(int red, int green, int blue)
{
- this._red = red;
- this._green = green;
- this._blue = blue;
+ _red = red;
+ _green = green;
+ _blue = blue;
}
- // Create a shallow copy
public override ColorPrototype Clone()
{
- Debug.Log("Cloning color RGB: (" + _red + " ," + _green + "," + _blue + ")");
-
- return this.MemberwiseClone() as ColorPrototype;
+ Debug.Log("Cloning color RGB: (" + _red + ", " + _green + ", " + _blue + ")");
+ return (ColorPrototype)MemberwiseClone();
}
}
- ///
- /// Prototype manager
- ///
class ColorManager
{
private Dictionary _colors = new Dictionary();
- // Indexer
- public ColorPrototype this[string key]
+ public void SetColor(string key, ColorPrototype color)
+ {
+ _colors[key] = color;
+ }
+
+ public ColorPrototype GetColor(string key)
{
- get { return _colors[key]; }
- set { _colors.Add(key, value); }
+ return _colors[key];
}
}
-}
+}
\ No newline at end of file
diff --git a/Assets/Creational Patterns/Prototype Pattern/Example2/PrototypePatternExample2.cs b/Assets/Creational Patterns/Prototype Pattern/Example2/PrototypePatternExample2.cs
index 50a41b7..538f6d8 100644
--- a/Assets/Creational Patterns/Prototype Pattern/Example2/PrototypePatternExample2.cs
+++ b/Assets/Creational Patterns/Prototype Pattern/Example2/PrototypePatternExample2.cs
@@ -12,17 +12,15 @@ public class PrototypePatternExample2 : MonoBehaviour
{
void Start()
{
- CloneFactory factory = new CloneFactory();
-
- Sheep sally = new Sheep();
+ var factory = new CloneFactory();
+ var sally = new Sheep();
- Sheep clonedSheep = (Sheep)factory.GetClone(sally);
+ var clonedSheep = (Sheep)factory.GetClone(sally);
Debug.Log("Sally: " + sally.ToStringEX());
Debug.Log("Clone of Sally: " + clonedSheep.ToStringEX());
Debug.Log("Sally Hash: " + sally.GetHashCode() + " - Cloned Sheep Hash: " + clonedSheep.GetHashCode());
}
-
}
public class CloneFactory
@@ -47,25 +45,20 @@ public Sheep()
public object Clone()
{
- Sheep sheep = null;
-
try
{
- sheep = (Sheep)base.MemberwiseClone();
+ return MemberwiseClone();
}
catch (Exception e)
{
- Debug.LogError("Error cloning Sheep");
+ Debug.LogError("Error cloning Sheep: " + e.Message);
+ return null;
}
-
- return sheep;
}
public string ToStringEX()
{
- return "Hello I'm a Sheep";
+ return "Hello, I'm a Sheep";
}
}
-
}
-
diff --git a/Assets/Creational Patterns/Prototype Pattern/Structure/PrototypeStructure.cs b/Assets/Creational Patterns/Prototype Pattern/Structure/PrototypeStructure.cs
index 0ce3bf2..d758607 100644
--- a/Assets/Creational Patterns/Prototype Pattern/Structure/PrototypeStructure.cs
+++ b/Assets/Creational Patterns/Prototype Pattern/Structure/PrototypeStructure.cs
@@ -7,19 +7,16 @@
public class PrototypeStructure : MonoBehaviour
{
-
- void Start( )
+ void Start()
{
// Create two instances and clone each
-
ConcretePrototype1 p1 = new ConcretePrototype1("I");
ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
- Debug.Log("Cloned: "+c1.Id);
+ Debug.Log("Cloned: " + c1.GetId());
ConcretePrototype2 p2 = new ConcretePrototype2("II");
ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
- Debug.Log("Cloned: "+c2.Id);
-
+ Debug.Log("Cloned: " + c2.GetId());
}
}
@@ -31,52 +28,42 @@ abstract class Prototype
private string _id;
// Constructor
- public Prototype(string id)
+ protected Prototype(string id)
{
- this._id = id;
+ _id = id;
}
// Gets id
- public string Id
+ public string GetId()
{
- get { return _id; }
+ return _id;
}
public abstract Prototype Clone();
}
///
-/// A 'ConcretePrototype' class
+/// A 'ConcretePrototype' class
///
class ConcretePrototype1 : Prototype
{
- // Constructor
- public ConcretePrototype1(string id)
- : base(id)
- {
- }
+ public ConcretePrototype1(string id) : base(id) { }
- // Returns a shallow copy
public override Prototype Clone()
{
- return (Prototype)this.MemberwiseClone();
+ return (Prototype)MemberwiseClone();
}
}
///
-/// A 'ConcretePrototype' class
+/// A 'ConcretePrototype' class
///
class ConcretePrototype2 : Prototype
{
- // Constructor
- public ConcretePrototype2(string id)
- : base(id)
- {
- }
+ public ConcretePrototype2(string id) : base(id) { }
- // Returns a shallow copy
public override Prototype Clone()
{
- return (Prototype)this.MemberwiseClone();
+ return (Prototype)MemberwiseClone();
}
-}
+}
\ No newline at end of file