| name | design-patterns-ruby |
| description | Comprehensive Ruby implementations of Gang of Four (GoF) design patterns. Use when implementing object-oriented design solutions, refactoring to reduce coupling, solving architecture problems, or when user mentions specific patterns (Factory, Singleton, Observer, Strategy, etc.) in Ruby context. |
Provide Ruby implementations and guidance for all 23 GoF design patterns. Help agents identify which pattern solves a given problem and implement it idiomatically in Ruby.
<quick_start>
<pattern_selection>
Object Creation Problems โ Creational Patterns
- Decouple creation from usage โ Factory Method
- Families of related objects โ Abstract Factory
- Complex objects with many params โ Builder
- Clone without concrete class dependency โ Prototype
- Single shared instance โ Singleton
Structural Problems โ Structural Patterns
- Incompatible interfaces โ Adapter
- Multiple independent dimensions โ Bridge
- Tree structures treated uniformly โ Composite
- Add behavior dynamically โ Decorator
- Simplify complex subsystems โ Facade
- Memory with many similar objects โ Flyweight
- Access control/logging/caching โ Proxy
Behavioral Problems โ Behavioral Patterns
- Multiple handlers in sequence โ Chain of Responsibility
- Decouple UI from business logic โ Command
- Traverse without exposing internals โ Iterator
- Reduce chaotic dependencies โ Mediator
- Undo/restore functionality โ Memento
- Notify about state changes โ Observer
- Behavior varies by state โ State
- Switch algorithms at runtime โ Strategy
- Algorithm skeleton with custom steps โ Template Method
- Operations on complex structures โ Visitor
</pattern_selection>
<ruby_abstract_method>
Ruby doesn't have built-in abstract methods. Use:
def abstract_method
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end
</ruby_abstract_method>
</quick_start>
<when_to_use>
Use this skill when encountering:
- "How do I create objects without specifying exact classes?" โ Factory Method/Abstract Factory
- "Constructor has too many parameters" โ Builder
- "Need to copy objects without knowing their concrete type" โ Prototype
- "Ensure only one instance exists" โ Singleton
- "Legacy class interface doesn't match what I need" โ Adapter
- "Class explosion from combining multiple features" โ Bridge
- "Work with tree/hierarchy uniformly" โ Composite
- "Add features without modifying class" โ Decorator
- "Simplify interaction with complex library" โ Facade
- "Too many similar objects consuming memory" โ Flyweight
- "Control access/add logging to object" โ Proxy
- "Request goes through chain of handlers" โ Chain of Responsibility
- "Need undo/redo or queue operations" โ Command
- "Custom iteration over collection" โ Iterator
- "Components too tightly coupled" โ Mediator
- "Save and restore object state" โ Memento
- "Notify multiple objects of changes" โ Observer
- "Object behavior depends on state" โ State
- "Swap algorithms at runtime" โ Strategy
- "Subclasses customize algorithm steps" โ Template Method
- "Add operations to class hierarchy" โ Visitor
</when_to_use>
<pattern_quick_reference>
Factory Method - Define interface for creation, let subclasses decide type
class Creator
def factory_method
raise NotImplementedError
end
def operation
product = factory_method
"Working with #{product.operation}"
end
end
class ConcreteCreator < Creator
def factory_method
ConcreteProduct.new
end
end
File: Ruby/src/factory_method/conceptual/main.rb
Singleton (thread-safe)
class Singleton
@instance_mutex = Mutex.new
private_class_method :new
def self.instance
return @instance if @instance
@instance_mutex.synchronize { @instance ||= new }
@instance
end
end
File: Ruby/src/singleton/conceptual/thread_safe/main.rb
See references/creational-patterns.md for Abstract Factory, Builder, Prototype.
**Decorator** - Wrap objects to add behavior dynamically
```ruby
class Decorator < Component
def initialize(component)
@component = component
end
def operation
@component.operation
end
end
class ConcreteDecorator < Decorator
def operation
"Decorated(#{@component.operation})"
end
end
Stack decorators
decorated = DecoratorB.new(DecoratorA.new(ConcreteComponent.new))
File: `Ruby/src/decorator/conceptual/main.rb`
**Adapter** - Convert interface to expected format
```ruby
class Adapter < Target
def initialize(adaptee)
@adaptee = adaptee
end
def request
"Adapted: #{@adaptee.specific_request}"
end
end
File: Ruby/src/adapter/conceptual/main.rb
See references/structural-patterns.md for Bridge, Composite, Facade, Flyweight, Proxy.
**Strategy** - Swap algorithms at runtime
```ruby
class Context
attr_writer :strategy
def initialize(strategy)
@strategy = strategy
end
def execute
@strategy.do_algorithm(data)
end
end
Switch strategy at runtime
context = Context.new(StrategyA.new)
context.strategy = StrategyB.new
File: `Ruby/src/strategy/conceptual/main.rb`
**Observer** - Notify subscribers of state changes
```ruby
class Subject
def initialize
@observers = []
end
def attach(observer)
@observers << observer
end
def detach(observer)
@observers.delete(observer)
end
def notify
@observers.each { |observer| observer.update(self) }
end
end
File: Ruby/src/observer/conceptual/main.rb
State - Object behavior changes based on internal state
class Context
attr_accessor :state
def transition_to(state)
@state = state
@state.context = self
end
def request
@state.handle
end
end
File: Ruby/src/state/conceptual/main.rb
See references/behavioral-patterns.md for Chain of Responsibility, Command, Iterator, Mediator, Memento, Template Method, Visitor.
</pattern_quick_reference>
<ruby_idioms>
<deep_copy>
For Prototype pattern, use Marshal for deep copying:
Marshal.load(Marshal.dump(object))
</deep_copy>
<thread_safety>
For Singleton and shared resources, use Mutex:
@mutex = Mutex.new
@mutex.synchronize { @instance ||= new }
</thread_safety>
<private_constructor>
For Singleton pattern:
private_class_method :new
</private_constructor>
- `attr_reader :name` - read-only
- `attr_writer :name` - write-only
- `attr_accessor :name` - read/write
<type_docs>
Use YARD-style documentation:
def method(value)
end
</type_docs>
</ruby_idioms>
<running_examples>
ruby Ruby/src/<pattern>/conceptual/main.rb
ruby Ruby/src/singleton/conceptual/thread_safe/main.rb
ruby Ruby/src/observer/conceptual/main.rb
ruby Ruby/src/strategy/conceptual/main.rb
ruby Ruby/src/decorator/conceptual/main.rb
Requires Ruby 3.2+.
</running_examples>
<detailed_references>
- references/creational-patterns.md - Factory Method, Abstract Factory, Builder, Prototype, Singleton
- references/structural-patterns.md - Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy
- references/behavioral-patterns.md - Chain of Responsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor
</detailed_references>
<success_criteria>
- Pattern correctly solves the identified design problem
- Ruby idioms used appropriately (NotImplementedError, Marshal, Mutex)
- Code follows Ruby conventions (snake_case, attr_* accessors)
- Example runs without errors via
ruby Ruby/src/<pattern>/conceptual/main.rb
</success_criteria>