Design Patterns in life and Ruby — gain an intuitive understanding of OO design patterns by linking them with real-life examples.
The Adapter Pattern is very easy to understand because we interact with it every single day.
If we search for adapter images online, we will see lots of real-life examples using the Adapter Pattern.
Type C to Type A Power Adapter
Let’s take a closer look at one of these real-life examples — Type C to Type A Power Adapter.
pc: http://www.worldstandards.eu
The Type A electrical outlet plug is widely used in the US, whereas Type C is widely used in Europe.
I live in the US, and my MacBook charger has a Type A plug.
We can write a simple TypeAPlug
class like this:
class TypeAPlug attr_accessor :electricity, :voltage def initialize @electricity = nil @voltage = nil end def plug_into(outlet) self.electricity = outlet.type_a_elec self.voltage = outlet.type_a_voltage end end
The plug_into
method expects a passed-in outlet that has type_a_elec
and type_a_voltage
.
Let’s say now I travel to Europe. And there are only Type C outlets:
class TypeCOutlet def type_c_elec '2.5A electricity' end def type_c_voltage '220-240 voltages' end end
And a Type C outlet provides type_c_elec
and type_c_voltage
.
I can’t plug my MacBook charger into a Type C outlet because the interface a Type C outlet provides is not compatible with the interface needed by my MacBook charger.
My MacBook charger expects the outlet it plugs into to have type_a_elec
and type_a_voltage
. An error is raised when the outlet is missing these two methods.
In order to plug my MacBook charger into a Type C outlet, I need an adapter that converts a Type C outlet into a Type A outlet: a Type C to Type A Power Adapter.
class C2AAdapter attr_reader :type_c_outlet def initialize(type_c_outlet) @type_c_outlet = type_c_outlet end def type_a_elec "converting #{type_c_outlet.type_c_elec}" # some electric circuit conversion '15A electricity' end def type_a_voltage "converting #{type_c_outlet.type_c_voltage}" # some electric circuit conversion '100 - 127 voltages' end end
The C2AAdapter
takes a type_c_outlet
and provides two additional methods, type_a_elec
and type_a_voltage
, to make it compatible with the interface needed by a type_a_plug
.
Now I can plug my MacBook charger into a Type C outlet through a Type C to Type A power adapter.
The C2AAdapter
is an example of the Adapter pattern.
Definition of the Adapter Pattern
The Adapter Pattern converts the interface of a class into another interface the client expects.
Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
The C2AAdapter
converts the interface of a type_c_outlet
into the interface of a type_a_outlet
so a type_a_plug
can plug into a type_c_outlet
through a C2AAdapter
.
We can also wrap more than one existing interface into one target interface that a client is expecting:
Woohoo!
We just learned the Adapter Pattern together.
It’s time to charge my MacBook before it dies 😉
Takeaways
The Adapter Pattern converts the interface of a class into another interface the client expects.
Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
Enjoyed the article?
My best content on Software Design, Rails, and Career in Dev. Delivered weekly.
Next time we will take a look at 1-click-ordering.