| 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.
public interface IClonable<T> {
T Clone();
}
public class ConcretePrototype : IClonable<ConcretePrototype> {
public ConcretePrototype Clone() {
return (ConcretePrototype)this.MemberwiseClone();
}
}
#endregion
[TestFixture]
public class ConcretePrototypeTests {
ConcretePrototype concretePrototype;
[SetUp]
public void SetUp() {
concretePrototype = new ConcretePrototype();
}
[Test]
public void CloneCreatesInitializedInstanceOfConcretePrototype() {
ConcretePrototype clone = concretePrototype.Clone();
Assert.IsNotNull(clone, );
Assert.AreNotSame(concretePrototype, clone,
);
}
}
#endregion
|
|
|
|