Skip to content

Instantly share code, notes, and snippets.

@rbwestmoreland
Created January 27, 2012 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbwestmoreland/1686270 to your computer and use it in GitHub Desktop.
Save rbwestmoreland/1686270 to your computer and use it in GitHub Desktop.
Properly Implementing the Prototype Pattern
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)
}
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