Login using System.Noticeably.Different.WebSite;
November 19, 2008 NoticeablyDifferent
Search for
 
Testing The Prototype Pattern 
It is typical, in .NET, to conform to the prototype patten by simply implementing the System.IClonable interface and perform a shallow copy of the object within the Clone method. The Copy method is typically reserved for deep copies of an object.
 
There is no generic implementation of the IClonable interface in .NET 2.0. Below I provide that generic interface.
 
    #region Code
public interface IClonable<T> {
    T Clone();
}
 
public class ConcretePrototype : IClonable<ConcretePrototype> {
    public ConcretePrototype Clone() {
        return (ConcretePrototype)this.MemberwiseClone();
    }
}
    #endregion
 
    #region Unit Tests
[TestFixture]
public class ConcretePrototypeTests {
    ConcretePrototype concretePrototype;
 
    [SetUp]
    public void SetUp() {
        concretePrototype = new ConcretePrototype();
    }
 
    [Test]
    public void CloneCreatesInitializedInstanceOfConcretePrototype() {
        ConcretePrototype clone = concretePrototype.Clone();
        Assert.IsNotNull(clone, "Clone is not null");
        Assert.AreNotSame(concretePrototype, clone,
            "Clone not same as original ConcretePrototype");
        //TODO: Assert the public properties of the clone are shallow copies of the Concrete Prototype
    }
}
    #endregion