Voici un cheatsheet Ruby exhaustif en français, organisé par catégories de patterns:
Patterns Créationnels
Singleton Pattern
class Database
@@instance = nil
def self.instance
@instance ||= new
end
private_class_method :new
end
Utilisé pour contrôler l'accès à une instance unique d'une classe.
Factory Method Pattern
class Product
def initialize(name)
@name = name
end
def use
"Using #{name}"
end
end
class Creator
def factory_method(name)
raise NotImplementedError, "You have to implement the method 'factory_method'!"
end
def operation
"Creator: The same creator's code has just worked with #{factory_method("test").use}"
end
end
class ConcreteCreator < Creator
def factory_method(name)
Product.new(name)
end
end
Utilisé quand une classe ne veut pas décrire exactement les classes d'objets qu'elle doit créer.
Patterns Structurels
Adapter Pattern
class Target
def request
puts "Target: The default target's behavior."
end
end
class Adaptee
def specific_request
puts "Adaptee: The special behavior."
end
end
class Adapter < Target
def initialize(adaptee)
@adaptee = adaptee
end
def request
@adaptee.specific_request
end
end
Utilisé pour permettre à deux classes qui ne sont pas compatibles de travailler ensemble.
Bridge Pattern
class Abstraction
def initialize(implementation)
@implementation = implementation
end
def operation
"Abstraction: Base operation with:\n #{@implementation.operation_implementation}"
end
end
class RefinedAbstraction < Abstraction
def operation
"RefinedAbstraction: Extended operation with:\n #{@implementation.operation_implementation}"
end
end
module Implementation
class ConcreteImplementationA
def operation_implementation
"ConcreteImplementationA: Here's the result on platform A."
end
end
class ConcreteImplementationB
def operation_implementation
"ConcreteImplementationB: Here's the result on platform B."
end
end
end
Utilisé pour diviser une classe en deux parties qui peuvent évoluer indépendamment.
Patterns Comportementaux
Observer Pattern
class Subject
attr_accessor :observers, :state
def initialize
@observers = []
@state = nil
end
def attach(observer)
observers.push(observer) unless observers.include?(observer)
end
def detach(observer)
observers.delete(observer)
end
def notify
observers.each { |observer| observer.update(state) }
end
def change_state(state)
@state = state
notify
end
end
class Observer
def update(state)
puts "Observer: My subject just changed to #{state}."
end
end
Utilisé pour mettre en place une dépendance unidirectionnelle entre des objets, afin que lorsque l'objet sujet change de state, tous ses observateurs soient informés.
Strategy Pattern
module Strategies
class SortStrategy
def sort(array)
raise NotImplementedError, "You have to implement the method 'sort'!"
end
end
class BubbleSort < SortStrategy
def sort(array)
array.sort!
end
end
class QuickSort < SortStrategy
def sort(array)
array.sort_by(&:to_i)
end
end
end
class Context
def initialize(strategy = Strategies::BubbleSort.new)
@strategy = strategy
end
def execute_strategy(array)
@strategy.sort(array)
end
end
Utilisé pour définir une famille de algorithms, encapsuler chaque un d'entre eux et les rendre interchangeables.