Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored Prototype Pattern examples for improved clarity and compat… #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/// <summary>
/// The 'Prototype' abstract class
/// </summary>
abstract class ColorPrototype
{
public abstract ColorPrototype Clone();
}

/// <summary>
/// The 'ConcretePrototype' class
/// </summary>
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();
}
}

/// <summary>
/// Prototype manager
/// </summary>
class ColorManager
{
private Dictionary<string, ColorPrototype> _colors = new Dictionary<string, ColorPrototype>();

// 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];
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand All @@ -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();
}

/// <summary>
/// A 'ConcretePrototype' class
/// A 'ConcretePrototype' class
/// </summary>
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();
}
}

/// <summary>
/// A 'ConcretePrototype' class
/// A 'ConcretePrototype' class
/// </summary>
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();
}
}
}