Prototype Pattern
Create objects from configured prototypes with an explicit policy for copied and shared state.
· Last updated: 2026-11-07 · 9 min read
Overview
Instead of filling in a report preset from scratch, copy one that is already configured and change a few values. That configured original is the prototype. The awkward part is that the caller ends up doing the copying.
In the Prototype pattern, an object provides its own copying operation. Callers ask the prototype for a new copy instead of repeating its construction steps or copying each field themselves.
UML
The caller invokes IPreset.Copy(), and Preset decides what to copy. In this example, it creates a new mutable Tags list so the prototype and its copy do not share that list.
History
GoF cataloged Prototype as a creational pattern in Design Patterns in 1994. A prototype specifies the kind of object to create, and copying it produces the new instance.1
Creating new objects by cloning and delegating to concrete exemplars—without any class blueprint—was pioneered by Henry Lieberman in 1986 and realized by David Ungar and Randall B. Smith in their 1987 language Self.2 That lineage inspired JavaScript's prototype chain and Object.create(), while GoF adapted the cloning half into class-based languages so callers could spawn new instances from runtime-configured exemplars without coupling to concrete class names.
Why use it?
There are two payoffs. Choosing a prototype decides which configuration you start from. Because the copying logic sits in one place, callers write less code that walks internal fields one by one.
Copying does not end with creating a new object. You also have to decide whether a mutable list is shared or detached, and how to handle resources such as file handles that cannot simply be duplicated. That choice decides whether the copy and the prototype affect each other.
Example
Copy the prototype and change only the new preset name.
using System;
using System.Collections.Generic;
using System.Linq;
var original = new Preset("base", new[] { "draft" });
var copy = (Preset)original.Copy();
copy.Name = "custom";
Console.WriteLine(original.Name + " / " + copy.Name);
// Expose a copy operation so callers do not copy internal fields themselves.
interface IPreset { IPreset Copy(); }
// Create a new mutable tag list so edits to the copy do not affect the prototype.
sealed class Preset(string name, IEnumerable<string> tags) : IPreset
{
public string Name { get; set; } = name;
public List<string> Tags { get; } = new(tags);
public IPreset Copy() => new Preset(Name, Tags);
}
base / customIPreset, the return type of Copy(), does not expose a Name property. In this example, a caller that knows the copy is a Preset casts it before changing the name. The original keeps its name because the edit affects the new object. The TypeScript example returns the concrete type, so it needs no cast.
An extended example
This time a tag is added to the copy's Tags as well, and since the list was created anew, the prototype's list is left untouched. The two objects hold separate lists from the start.
using System;
using System.Collections.Generic;
using System.Linq;
var original = new Preset("base", new[] { "draft" });
var copy = (Preset)original.Copy();
copy.Tags.Add("reviewed");
Console.WriteLine(string.Join(",", original.Tags));
Console.WriteLine(string.Join(",", copy.Tags));
// Expose a copy operation so callers do not copy internal fields themselves.
interface IPreset { IPreset Copy(); }
// Create a new mutable tag list so edits to the copy do not affect the prototype.
sealed class Preset(string name, IEnumerable<string> tags) : IPreset
{
public string Name { get; set; } = name;
public List<string> Tags { get; } = new(tags);
public IPreset Copy() => new Preset(Name, Tags);
}
draft
draft,reviewedThe two examples together show how far the copy reaches. The extended example copies the list and shares immutable string values. Mutable objects nested inside that list would need their own copying policy if they must be independent. Prototype does not require indiscriminate deep copying.
TypeScript example
The TypeScript example splits the roles the same way. The caller chooses a prototype; Preset decides which state to copy. Keeping that policy with Preset avoids changing every caller when the copying rules change.
// Expose a copy operation so callers do not copy internal fields themselves.
interface IPreset { copy(): IPreset; }
class Preset implements IPreset {
readonly tags: string[];
constructor(public name: string, tags: readonly string[]) { this.tags = [...tags]; }
// Create a new mutable tag list so edits to the copy do not affect the prototype.
copy(): Preset { return new Preset(this.name, this.tags); }
}
const original = new Preset("base", ["draft"]);
const copy = original.copy();
copy.name = "custom";
copy.tags.push("reviewed");
console.log(original.name + " / " + copy.name);
console.log(original.tags.join(","));
console.log(copy.tags.join(","));base / custom
draft
draft,reviewedBecause the TypeScript version has copy() return Preset, changing the name and tags needs no cast, and the list is new while its immutable string values are shared. What the C# example did with a cast is gone.
Preserving the clone type with Prototype<T>
The return type gets in the way when the copy has to be used as its original type. A clone method returning object makes callers cast the result back; IPrototype<T> declares that result type, so cloning a Note directly produces a Note. The concrete Note also decides which state a clone shares and which state it copies.
Cloning a Note does two things: the title string is passed as a value, and the tags are copied into a new collection. Adding a tag to the clone therefore leaves the original list unchanged. If the list held mutable objects, those elements would need their own copying policy.
C#
using System;
using System.Collections.Generic;
var original = new Note("draft", new[] { "fp" });
Note copy = Prototype.Clone(original);
copy.Tags.Add("gof");
Console.WriteLine(original.Tags.Count);
Console.WriteLine(copy.Tags.Count);
Console.WriteLine(ReferenceEquals(original, copy));
// TYPES
interface IPrototype<T> { T Clone(); }
static class Prototype
{
public static T Clone<T>(IPrototype<T> source) => source.Clone();
}
sealed class Note : IPrototype<Note>
{
public string Title { get; }
public List<string> Tags { get; }
public Note(string title, IEnumerable<string> tags)
{ Title = title; Tags = new List<string>(tags); }
// The constructor creates a new list for the clone.
public Note Clone() => new(Title, Tags);
}1
2
FalseTypeScript
interface Prototype<T> { clone(): T; }
function clonePrototype<T>(source: Prototype<T>): T { return source.clone(); }
class Note implements Prototype<Note> {
readonly tags: string[];
constructor(readonly title: string, tags: readonly string[]) { this.tags = [...tags]; }
// Copy the array; its elements here are immutable strings.
clone(): Note { return new Note(this.title, this.tags); }
}
const original = new Note("draft", ["fp"]);
const copy: Note = clonePrototype(original);
copy.tags.push("gof");
console.log(original.tags.length);
console.log(copy.tags.length);
console.log(original === copy);1
2
falseGenerics preserve the clone type; they do not perform a deep copy. The operations inside Clone() determine the actual sharing policy. So the copying rules stay with each class.
The official references describe the type-parameter syntax and library contracts.3
Shallow vs. deep copy pitfalls and immutable record alternatives
The hardest part of Prototype in production is pinning down shallow versus deep copying across complex object graphs. In fact, .NET's framework design guidelines explicitly discourage implementing the legacy ICloneable interface on public APIs because its Clone() contract never specifies whether the returned copy is shallow or deep, leaving callers guessing which nested references are still aliased.4 Naive recursive deep cloning also loops infinitely on cyclic parent–child graphs and breaks whenever an object holds non-copyable OS handles, sockets, or unique entity IDs that must be reset rather than duplicated.
Modern C# and TypeScript codebases often sidestep deep-copy bugs by making presets immutable from the start. When a prototype is an immutable C# record holding an ImmutableArray<string> (or a TypeScript object whose shared nested values are also immutable), deriving a customized copy with preset with { Name = "Custom" } or { ...preset, name: "Custom" } safely shares unchanged nested structures without any risk of one instance mutating another.
Related reading
Footnotes
- Gamma, Helm, Johnson, Vlissides. Design Patterns (1994). Publisher record. Creational patterns, Prototype. ↩
- David Ungar and Randall B. Smith, "Self: The Power of Simplicity," OOPSLA '87 Proceedings, ACM, 1987, pp. 227–242. DOI. ↩
- C# generics; TypeScript generics ↩
- Microsoft Learn, "ICloneable Interface — Notes to Implementers." Official documentation. ↩