Object-Oriented Design Libraries
One of the interesting things about Ruby is the way it blurs the
distinction between design and implementation. Ideas that have to be
expressed at the design level in other languages can be implemented
directly in Ruby.
To help in this process, Ruby has support for some design-level strategies.
- The Visitor pattern (Design Patterns,
) is a way of traversing a collection
without having to know the internal organization of that collection.
- Delegation is a way of composing classes more flexibly and
dynamically than can be done using standard inheritance.
- The Singleton pattern is a way of ensuring that only
one instantiation of a particular class exists at a time.
- The Observer pattern implements a protocol allowing one
object to notify a set of interested objects when certain changes
have occurred.
Normally, all four of these strategies require explicit code each time
they're implemented. With Ruby, they can be abstracted into a library
and reused freely and transparently.
Before we get into the proper library descriptions, let's get the
simplest strategy out of the way.