Created
January 27, 2012 01:08
-
-
Save rbwestmoreland/1686270 to your computer and use it in GitHub Desktop.
Properly Implementing the Prototype Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/// <summary> | |
/// An example prototype interface. | |
/// <para>The Prototype pattern is a creational | |
/// pattern, whose purpose is to specify the kinds | |
/// of objects to create using a prototypical | |
/// instance, and create new objects by copying | |
/// this prototype.</para> | |
/// </summary> | |
public interface IPrototype | |
{ | |
#region Member(s) | |
Object ShallowCopy(); | |
Object DeepCopy(); | |
#endregion Member(s) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Prototype : IPrototype | |
{ | |
#region Event(s) | |
public event EventHandler Event; | |
#endregion Event(s) | |
#region Property(s) | |
public String ValueTypeProperty { get; set; } | |
public IPrototype ReferenceTypeProperty { get; set; } | |
#endregion Property(s) | |
#region Member(s) | |
public Object ShallowCopy() | |
{ | |
return this.MemberwiseClone(); | |
} | |
public Object DeepCopy() | |
{ | |
Prototype clone = (Prototype)this.MemberwiseClone(); | |
clone.ReferenceTypeProperty = (IPrototype)ReferenceTypeProperty.DeepCopy(); | |
return clone; | |
} | |
#endregion Member(s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment