Skip to content

Prototype

Context: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

public abstract class Prototype
{
public abstract Prototype Clone();
}
public class ConcretePrototype : Prototype
{
public int Data { get; set; }
public override Prototype Clone() => (ConcretePrototype)MemberwiseClone();
}
// Usage
var original = new ConcretePrototype { Data = 42 };
var clone = (ConcretePrototype)original.Clone();
Console.WriteLine(clone.Data); // 42

Copying complex documents: A document editor clones a template document (headers, footers, styles) and then modifies the copy for a new client.

Example: In .NET, the ICloneable interface (though not recommended) is a prototype pattern. More practically, MemberwiseClone is used for shallow copying. In game engines, prefabs are prototypes.