Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

ruby-patterns

> 编程语言
Open source

Examples of Patterns in Ruby

1.0K stars0 likes1 views
WebsiteGitHub

About

Examples of Patterns in Ruby

ruby-patterns

Examples of Patterns in Ruby

Table of Contents

  1. Adapter
  2. Builder
  3. Command
  4. Composite
  5. Decorator
  6. Facade
  7. Factory
  8. Interpreter
  9. Iterator
  10. Observer
  11. Proxy
  12. Singleton
  13. State
  14. Strategy
  15. Template

Adapter

  • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.[link]
…

Back to top

Builder

  • Separate the construction of a complex object from its representation so that the same construction process can create different representations.[link]
ruby
class BoardBuilder
  def initialize(width, height)
    @board = Board.new
    @board.width = width
    @board.height = height
    @board.tiles = []
    @board.monsters = []
  end

  def add_tiles(n)
    n.times{ @board.tiles  Board Object
board = builder.board
puts board.width
# => 2
builder.add_tiles(3)
builder.add_monsters(2)
puts board.tiles.size
# => 3
puts board.monsters.size
# => 2

Back to top

Command

  • Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.[link]
ruby
class Turn
  def initialize
    @commands = []
  end

  def run_command(command)
    command.execute
    @commands  110
turn.run_command(get_money)
puts hero.money
# => 10
turn.undo_command
puts hero.money
# => 0

Back to top

Composite

  • Composition over inheritance. Compose objects into tree structures to represent part-whole hierarchies.[link]
ruby
class CompositeQuest
  def initialize
    @tasks = []
  end

  def  300

quest2 = Quest.new
quest2  600

Back to top

Decorator

  • Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.[link]
ruby
class ItemDecorator
  def initialize(item)
    @item = item
  end
  # this needs to be delegated with other efective way
  def use
    @item.use
  end
end

class MagicItemDecorator  30
puts magic_item.description
# => Item Magic

masterpiece_item = MasterpieceItemDecorator.new(item)
puts masterpiece_item.price
# => 20
puts masterpiece_item.description
# => Item Masterpiece

# all next lines puts "do something"
item.use
magic_item.use
masterpiece_item.use

Back to top

Facade

  • The goal of the Facade Pattern is to provide a unified interface to a set of interfaces in a subsystem. This means you'd just have some object that can send back other objects.[link]
…

Back to top

Factory

  • Define an interface for creating an object, but let subclasses decide which class to instantiate.[link]
ruby
class Party
  attr_reader :members
  def initialize(factory)
    @members = []
    @factory = factory
  end

  def add_warriors(n)
    n.times{ @members  5
puts party.members.count{ |member| member.class == "Mage" }
# => 2

Back to top

Interpreter

  • This pattern provides an interpreter to deal with an abstract language. Using classes we can understand the inputs for parse them.[link]
…

Back to top

Iterator

  • Iterator helps you to iterate through a complex object using an iterator method.[link]
ruby
class Parent
  attr_reader :first_name

  def initialize(first_name, gender)
    @first_name = first_name
    @gender = gender
  end
end

class Child  Robert
#    Susan
#    Lucas
#    James
#    Rose

Back to top

Observer

  • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.[link]
ruby
module Observable
  attr_reader :observers

  def initialize(attrs = {})
    @observers = []
  end

  def add_observer(observer)
    @observers  6
puts hero.cursed?
# => true

Back to top

Proxy

  • Provide a surrogate or placeholder for another object to control access to it.[link]
…

Back to top

Singleton

  • Define a unique instance of an object.[link]
ruby
class HeroFactory
  @@instance = nil

  def self.instance
    @@instance ||= HeroFactory.send(:new)
  end

  def create_warrior
    Warrior.new
  end

  def create_mage
    Mage.new
  end

  private_class_method :new
end

# Usage
factory = HeroFactory.instance
another_factory = HeroFactory.instance
puts factory == another_factory
# => true
HeroFactory.new
# => Raise Exception

Back to top

State

  • This pattern tries to simplify complicated control flows changing an object's behavior dynamically.[link]
…

Back to top

Strategy

  • Define a family of algorithms, encapsulate each one, and make them interchangeable.[link]
…

Back to top

Template

  • Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template methods lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.[link]
…

Back to top

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言