Learn

Architecture & pattern guides

Read the idea, build it on a Grid, prove it in a challenge. Short, practical guides — illustrated with RodGrid's own nodes — on system design, design patterns, flow modeling, and how to get the most out of the app.

All articles

Prototype

Build one fully configured object, then make new ones by asking it to clone itself: what to copy, what to share, what to reset, and how a registry lets new types arrive without changing the caller.

Fifty goblins, built fifty times

In a tower-defense game, one line starts the next wave: spawnWave("goblin", 50). For each goblin, the spawner reads the goblin's entry from a config file, parses it, validates the stats and wires up its movement settings. Fifty times over, to produce fifty objects that start out identical.

The fiftieth goblin needs nothing the first one did not already have, so the cheaper move is to build one goblin properly, keep it aside, and copy it fifty times. Making new objects by copying a fully initialized example is the Prototype pattern, and the goblin built properly is the prototype. It pays off when construction is expensive, as it is here, or when the set of concrete types is only known at runtime, which this game runs into before long. If the trouble were a long, step-by-step assembly rather than an expensive one, Builder would be the better fit.

The goblin copies itself

A helper outside the class could read a goblin's fields and set them on a new object, but it can only reach what the class exposes. In Prototype the object copies itself, through a clone() method declared on a shared interface, and inside its own class the goblin can see every field, private ones included. The spawner's loop shrinks to four moves: take the prototype, call clone(), set a starting position, drop the clone into the level.

Keep the shared interface in mind. It matters again once there is more than one kind of enemy.

Wave 13 is poisoned before it arrives

The next playtest produces a strange bug report. In wave 12, a poison tower hits one goblin, and the entire wave starts losing health. Then wave 13 walks onto the field already poisoned, before any tower has fired.

Each goblin keeps its active status effects in a list, and clone() copied the reference to that list instead of the list. Every clone, and the prototype, pointed at one shared list. When the tower added poison to one goblin's effects, it added it to everyone's, the prototype's included, so every goblin cloned afterwards was born poisoned.

The bug comes down to two kinds of copy. A shallow copy creates a new object but copies only the references to whatever is nested inside it, so the original and the clone point at the same inner objects. A deep copy copies those nested objects too, recursively. Built-in copies are often shallow (Java's Object.clone() and Python's copy.copy() both stop one level down), which is why this bug is so easy to write.

Deep-copying everything is not the fix either. The goblin's sprite sheet is large and never changes during play, so every clone can safely point at the same one. The status list changes per goblin, so each clone needs its own. Sharing is safe for state that never changes and dangerous for state that does. Sharing a heavy, unchanging part across many objects on purpose is the idea behind Flyweight, and the two patterns combine well.

What a goblin must not inherit

With clone() open to fix the status list, every other field deserves the same look, because a clone can also copy things that should belong to one object only:

  • Identity. The game tracks goblins by ID. Fifty clones carrying the prototype's ID 7 means a tower locked onto goblin 7 has fifty candidates, and the kill counter cannot tell them apart. The clone needs a fresh ID.
  • Resources. If each goblin holds a handle to an audio channel for its footsteps, a copied handle leaves two goblins owning one channel, and when the first one dies and releases it, the second loses its sound or fails.
  • Back-references. A goblin points at its wave, and the wave points at its goblins. A naive recursive deep copy follows that loop forever. The wave reference should be set by the spawner, not copied.

So a good clone() decides field by field: copy it, share it, or reset it. For the goblin, that means copying the stats, sharing the sprite sheet, giving the clone its own empty status list and a fresh ID, and leaving the position and the wave for the spawner to set.

Armored goblins, bats and a registry

Then the designer adds an armored goblin, and a bat for the flying waves. The spawner was written around one prototype, and the quick edit is a branch per type, a new ArmoredGoblin(...) here and a new Bat(...) there, which means changing the spawner every time the bestiary grows.

The shared interface avoids that. Because every enemy implements clone(), the spawner can clone a goblin, an armored goblin or a bat without knowing which concrete class it holds. All it needs is a way to find the right prototype by name: a prototype registry, a map from a name to a ready-made instance. Spawning one goblin through it takes four steps:

  1. At startup, the game builds one instance of each enemy type from config and registers it under a name such as "goblin".
  2. The spawner (the Client) asks the registry for a copy of "goblin".
  3. The registry finds the stored prototype and calls its clone().
  4. A separate object comes back, and the spawner sets its starting position and drops it into the level.
Clone the prototype

The Client never constructs an enemy; it asks the registry, and the prototype copies itself.

Adding an enemy type now means registering one more prototype, and the spawner does not change. The types can even come from data, a plugin or a level file, which is the runtime case from the start: the spawner can ship before anyone decides that bats exist. A Factory Method per type would also hide the concrete classes, but each new type would need a new creator; the registry adds a type by adding an instance.

Two habits undo the registry. The first is configuring the stored prototype instead of the clone: give the registered goblin a speed boost for one wave, and every goblin cloned afterwards inherits it. The registry entry is a template, so changes belong on the copy. The second is calling new for some types beside clone() for others. Then adding a type still means editing the spawner, and the pattern's main benefit is gone.

One neighbour is worth knowing for the late waves. When goblins die by the hundred and new ones arrive every second, recycling dead goblins instead of cloning fresh ones is Object Pool, a different answer to the same cost.

Spawn only by cloning

Open the Prototype Pattern challenge and create new objects only through clone(), never new, working from a registry of pre-configured prototypes. The challenge allows six nodes; one build that fits is a Port / Interface node declaring clone(), two Prototype nodes for concrete types, a Client, and a Component node titled as the registry. Whatever you build, check the arrows: the Client should reach new objects only through the registry and clone(), never by constructing a concrete type.

Practice this