Template method
The template method pattern in Ruby is a behavioral design pattern. It is used to define an algorithm in a base class but to leave some steps for subclasses to implement.
Why? Because some steps of an algorithm may be different for different subclasses. And this is exactly where the template method pattern shines.
The template…
Strategy Pattern in Ruby
The strategy pattern in Ruby is one of the most common design patterns. It's a very simple concept, yet it's very powerful.
Basically, the strategy pattern is about encapsulating an algorithm in a class and making it interchangeable.
Sorting Algorithms As an Example
A sorting algorithm is a great example of something that can…
Singleton Pattern in Ruby
The singleton pattern in Ruby ensures that only one object (the singleton) can be instantiated from a class.
It is useful to ensure that only one object has a certain state or represents a certain concept in your application.
For example, the Database class could be a singleton to ensure only one database connection.
The…
Proxy Pattern in Ruby
If you want to learn how to use the proxy pattern in ruby, this tutorial is for you.
The proxy pattern is a software design pattern that provides a surrogate object that controls access to another object.
A proxy can be used to control access to an object, delay the creation…
Observer Pattern in Ruby
In this tutorial, we will examine the Observer pattern in Ruby.
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
It's often…
Interpreter Pattern in Ruby
Implementing the Interpreter pattern in Ruby is pretty straightforward. Let's take a look at a simple example.
In this example, we have a simple DSL for creating a list of todos.
class Todo
def initialize(description)
@description = description
end
def to_s
"#{description}"
end
…
Factory Pattern in Ruby
This tutorial will show how to integrate the Factory Pattern in Ruby.
Factory Pattern is the creational design pattern in Ruby that defines an interface for creating objects and lets the classes that implement the interface decide which classes to instantiate.
Factory Pattern is also known as Virtual Constructor.
The Factory Pattern is helpful in…
DSL in Ruby
A DSL in Ruby or "Domain Specific Language" is a small language that is embedded in the language.
It has a syntax that is specific to a particular problem domain.
For example, Rake is a DSL for defining and running tasks.
Why Use a DSL?
The main reason for using a DSL is that it makes…
Composite Pattern in Ruby
Let's explore the Composite Pattern in Ruby:
A composite is a structural design pattern that allows you to compose objects into a tree-like structure and then work with this structure as if it was a single object.
The composite pattern is often used in UI applications to represent objects in a tree-like structure (for…
Command Pattern in Ruby
This article will show how to implement the Command Pattern in ruby.
The command pattern is a behavioral design pattern that encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
Creating the Command Pattern in Ruby
We first need to create…