Design Patterns in Ruby | ADAPTER

rubyhub.io
2 min readOct 29, 2022

--

Overview

The adapter is a structural design pattern that allows two objects with incompatible interfaces to work together.

Real World Example

Before I explain what it is and how to use the adapter pattern, think about what this name associates you with. If you’ve ever been to a country that uses other electrical sockets, you must have used an adapter similar to the one in the picture below.

Photo by Call Me Fred on Unsplash

So what is the purpose of using an adapter? The adapter allows you to connect a plug(Adaptee) to an electrical socket(Target) that do not fit together.

Coding Problem

First, we will create a simple User class that has two attributes, first_name and last_name, and a full_name method that returns the full name of the user.

Next, we will create a Greeter class, with a hello method that will allow us to greet the user.

As we can see, everything in the example above works fine. Now let’s assume that we also want to say hello to every team. So let’s create a Team class.

The method will of course raise an error because the Team class doesn’t have a method that returns the full name.

Solution

Before we create a solution to this problem, we need to define three objects that we need in the Adapter pattern.

Target

The object we want to use. In this case, it will be the Greeter class.

Adaptee

The object we want to adapt to work with the target. In this case, it will be the Team class.

Adapter

An object that allows Adaptee to connect to a Target.

If we have already defined Target and Adaptee objects, we can prepare our Adapter class. To create it, just wrap Adaptee(Team) with a new class(Team Adapter) and then create a method(full_name) that will be compatible with the Greeter class interface.

An adapter written in such a way can easily be used to work with the Greeter class.

I am currently working on a book about design patterns in Ruby. If you are interested in this topic, you can sign up for my newsletter 📪️: https://mailchi.mp/e3dd49dfada1/medium. My subscribers will receive a link to my e-book for free immediately after its publication. 🆓

--

--

rubyhub.io

[🔴 100% Follow-Back 🔴] I'm Full Stack Developer with 5-years of experience working with Ruby on Rails, trying to share my knowledge with others.