Design patterns · in Ruby

Small Objects, Big Systems

A reference for design patterns in Ruby. Each page gives the pattern's intent, lists its parts, and implements it in plain Ruby that you can copy and run. The examples come from a small tower defence game: towers, goblins, waves and gold.

Press start

Creational 5 patterns

How objects get made — hiding which classes are instantiated, and how.

  1. Abstract Factory Make whole families of related objects that are guaranteed to work together.
  2. Builder Assemble a complex object step by step, keeping the recipe apart from the parts.
  3. Factory Method Let subclasses decide which class to instantiate, behind one overridable method.
  4. Prototype Make new objects by copying a configured example rather than building from scratch.
  5. Singleton Guarantee a class has exactly one instance, and give everyone the same way to reach it.

Structural 7 patterns

How objects are put together — composing small parts into larger structures.

  1. Adapter Wrap an object so its interface matches the one your code expects.
  2. Bridge Split what a thing is from how it's done, so both can vary independently.
  3. Composite Treat a single object and a whole tree of objects through the same interface.
  4. Decorator Add behaviour to one object by wrapping it, instead of subclassing.
  5. Facade Put one simple front desk in front of a complicated subsystem.
  6. Flyweight Share the heavy, unchanging part of many similar objects instead of copying it.
  7. Proxy Stand in for another object to control when and how it's reached.

Behavioral 11 patterns

How objects talk — assigning responsibilities and routing requests between them.

  1. Chain of Responsibility Pass a request along a line of handlers until one of them deals with it.
  2. Command Turn a request into an object so it can be queued, logged and undone.
  3. Interpreter Represent a small language as a tree of objects that can evaluate themselves.
  4. Iterator Walk through a collection's elements without exposing how it stores them.
  5. Mediator Route all communication between objects through one coordinator.
  6. Memento Capture an object's state in a sealed snapshot so it can be restored later.
  7. Observer Let many objects be notified automatically when one object changes.
  8. State Let an object change its behaviour when its internal state changes, as if it changed class.
  9. Strategy Swap one algorithm for another behind a single, shared interface.
  10. Template Method Fix the steps of an algorithm in a base class, and let subclasses fill in the steps.
  11. Visitor Add new operations to a fixed set of classes without changing those classes.

On each pattern page

Participants
The roles in the pattern, and the Ruby class that plays each one.
Ruby implementation
A complete, runnable example. Every example is checked by the test suite.
In use
How a client wires the parts together.
Notes
Where Ruby already gives you the pattern, and when to reach for it.

The collection starts with the patterns catalogued in Design Patterns by Gamma, Helm, Johnson and Vlissides (1994), often called the "Gang of Four" book.