際際滷

際際滷Share a Scribd company logo
Object Oriented Imposters
The Adapter Pattern
Adapters in Ruby
An adapter is an object that bridges the
interface you have and the interface you need
Ruby lets us modify objects and classes
on the fly at runtime.
Encrypter Example
Implement the Encrypter Class
StringIOAdapter
Using our Adapter
Renderer Example
TextObject
BritishTextObject
BritishTextObjectAdapter
Modify a Class on the Fly
Modify a Single Instance
Modify a Single Instance
When to Adapt or Modify
When modifications are simple and clear
You understand the class youre modifying
and the way in which it is used.
The interface mismatch is really complex.
You have no idea how the class works.
When to Adapt or Modify

Adapters preserve encapsulation at the cost of
some complexity. Modifying a class may buy
you some simplification, but at the cost
tinkering with the plumbing
Wrap Up
Adapters are part of the Object Oriented
Impostor patterns.
Not just about code, intent is critical.

More Related Content

Object Oriented Impostor - Adapter Pattern