Mediator Pattern in Ruby
The Mediator Pattern in Ruby allows communication between two or more classes by encapsulating how these classes interact.
This means that you should use a mediator class when you have a group of classes that need to interact, and you want to remove the dependency between those classes.
For example, if you have several…
Advanced Tutorials
Chain of Responsibility Pattern in Ruby
The chain of responsibility pattern is used to process varied requests, each of which may be handled by a different handler.
A handler is responsible for completing a request, but they can delegate that responsibility to other handlers, making it a chain of responsibility.
Benefits and drawbacks
There are many benefits to…
Bridge Pattern in Ruby
The bridge pattern in Ruby is a design pattern used in software engineering. The bridge pattern is useful for situations where you want to:
- Decouple an abstraction from its implementation so that they can vary independently.
- Create a new abstraction similar to an existing one but with different implementations.
- Keep an abstraction…
Abstract Factory in Ruby
The abstract factory in Ruby is an interface or class that provides a way to create families of related or dependent objects without specifying their concrete classes.
Abstract Factory is a creational, structural design pattern that provides one of the best ways to create an object. This pattern is useful when your code…
Policy Pattern in Ruby
The policy pattern in Ruby is a widely used software design pattern that allows you to encapsulate different behavior in separate classes, aka policies.
In general, you would use this pattern when you have a different behavior that needs to be applied in different circumstances.
For example, you may want to apply a discount…
Iterator Pattern in Ruby
If you want to discover how to implement the iterator pattern in Ruby, this tutorial is for you!
It's common to need to iterate over a collection of objects in Ruby. For example, you have an array of User objects, and you need to go over each one of them…
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…