Modeling the World with Prototypes

This post started out as the second half of an article on prototype-based OO design for Gregory Brown’s Practicing Ruby. For an introduction to the prototyped view of the object world, and to follow along on an adventure in making Ruby act like a prototype-based language instead of a class-based one, you should go check out that article. Unlike the Practicing Ruby article, which is more about experimentation, this post focuses on the Prototype Pattern—a practical way to simplify your object models while still using classes.

A monster menagerie

Let’s say we’re writing a dungeon-crawl-style game in the vein of Nethack. So along with the  rooms and items that we’d expect to find in a text adventure game, there are also various semi-randomly-generated monsters who periodically confront the hero. Different types of monster have different stats, such as health, speed, and strength. They each have their own types of attack as well. We’d also like to be able to load up the list of monster types at run-time, from a user-editable file like this:

One way to model different monster types would be like this:

Here there is a Monster base class, and a subclass for each type of monster. But this doesn’t really lend itself to dynamically loading arbitrary monster types from a file, so we look for other approaches.

Definitions and instances

We experiment with one design that uses MonsterDefinition classes to hold the static attributes of different monsters. A MonsterDefinition can be told to #spawn a Monster instance. The Monster instance has a reference back to its definition, as well as an instance-specific health meter (initialized based on the max_hit_points of the MonsterDefinition).

This approach seems promising. But as we reflect on it, we realize that we’re probably going to keep adding more of these definition/instance pairs of classes. RoomDefinition / Room, ItemDefinition / Item, etc. This feels like an awful lot of ceremony.

Cloning creatures

Finally, we hit upon using the Prototype Pattern. In this version, there is only one class: Monster. It has slots for both static attributes (like name, and strength), and dynamic attributes like health.

To initialize our game’s bestiary of possible monster types, we load up the YAML-formatted monster file and initialize a Monster for each entry. Dynamic attributes like health are left blank for now. These are our prototypes.

When we want to set up a player encounter with a monster, we simply find the appropriate prototype monster, and duplicate it. The customized #initialize_dup method in Monster takes care of setting up an initial health meter for the cloned monster.

we can easily generate random monsters:

This solution is both shorter and simpler than any of the others we tried.

Representing Forms

The Prototype Pattern is, in my experience, one of the more overlooked of the Gang of Four patterns. It is useful in many situations. As another example, consider a web application where administrators build form templates and then users fill out the forms. One way to model this is to populate the form builder interface FormDefinition objects, containing instances of TextFieldDefinition, CheckboxFieldDefinition, DateFieldDefinition, and so on. Then, when the form definition is complete and ready for user input, a new Form object is created, using the FormDefinition as a guide, with TextField, CheckboxField, DateField, etc. objects “inside” of it.

If we apply the Prototype Pattern to this problem, we once again do away with the definition/instance dichotomy. Instead, building a new form simply means assembling a Form object, where all of the form fields have empty or placeholder values. (This makes it exceptionally easy to show a live preview of the form as it is being built). The form is published by turning on a flag marking it as a “master” form. Whenever a user fills out the form, they are really filling out a duplicate of the master.


Design Patterns says that the Prototype Pattern is appropriate:

when a system should be should be independent of how its products are created, composed, and represented, and

  • when the classes to instantiate are specified at run-time, for example, by dynamic loading; or
  • to avoid building a class hierarchy of factories that parallels the class hierarchy of products; or
  • when instances of a class can have only one of a few different combinations of state. It may be more convenient to install a corresponding number of prototypes and clone them rather than instantiating the class manually, each time with the appropriate state.

The class-based view vs. the prototype-based view of OO represent a philosophical divide in how we model the world in software. We can learn from both points of view. But the lessons of the prototype-oriented mindset are not merely philosophical ones for users of class-based languages. The Prototype Pattern is a way to apply prototypes in a class-based system, one that can slash through complicated parallel inheritance hierarchies and provide a simple, flexible, and dynamic alternative. Questions? Comments? Have experience implementing the prototype pattern in your own applications? Pipe up in the comments! Oh, and do check out the article I wrote for Practicing Ruby. I had a blast metaprograming Ruby into behaving more like Self :-)