Design Pattern Adapter, in a nutshell
Let’s play on my brand new video game? I can’t because my old television doesn’t support the video game interface. May you already face this situation someday.
Motivation
Excerpt from GOF book
How can existing and unrelated classes work in an application that expects classes with a different and incompatible interface? [1]
Applicability
Excerpt from GOF book,
you want to use an existing class, and its interface does not match the one you need.
you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don’t necessarily have compatible interfaces.
(object adapter only) you need to use several existing subclasses, but it’s impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
Where can I use this pattern?
Based on the lecture, and the motivation this pattern can be used to connect different interfaces without a lot of effort.
There are a couple of examples for news libraries, frameworks, systems using a new interface and you won’t write your code again to be up to date with the interface.
Once your service is running, change the interface is really tough, because the clients and change the interface to the client has more work than adapt the interface internally.
Examples
GOF
The diagrams below show how to build and identify this pattern using an object since is possible to use it as an object and class.
The Adapter by class…
Hands-on
For this hands-on, I choose a real situation, connect an old TV with a modern console. In my case, was one Playstation One in ’90, and the TV doesn’t have a suitable input.
Because of this, I’ve to buy (my family), buy an adapter to convert the signal to a signal understandable to TV, and at last, I face some PAL-M, NTSC problem, but this is for the other article.
P.S. Also I had a voltage issue since the console works using 110v and the house had 220v 😢
Model
The model represents the problem and how the adapter pattern will help in this solution.
Adapter by object
This approach use composition, in this example the class HDMIAdapter depends on an HDMIConnector interface.
When you won a new PS5 but you have an old TV.
Adapter by class
On the other hand, using the Adapter by class is used inheritance instead. The good practice says “Prefer composition over inheritance”, and it’s good advice.
For instance, a real example.
Participants defined by GOF
Target →Defines the domain-specific interface that the Client uses.
Client → Collaborates with objects conforming to the Target interface.
Adaptee → Defines an existing interface that needs adapting.
Adapter → Adapts the interface of Adaptee to the Target interface.
Participants inside the project, and the code below
Target → HDMI, RCA connectors
Client → Tvs
Adaptee → HDMIConnector, RCA
Adapter → HDMIAdapter, RCAClassAdapter
Tests
Adapter by object
Adapter by class
Code
The complete code is available on my GitHub.
Conclusion
After applying this pattern to a real problem, I could see clearly how this pattern helps connect different things. Besides, allow old and new code features to live togheter.
References
1 — Gamma Erich, Helm Richard, Johnson Ralph, Vlissides John, Grady Booch. (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley.
2 — Eric Freeman, Elisabeth Robson. (2020). Head First Design Patterns, 2nd Edition. O’Reilly Media, Inc.
3 — Cover image — https://pixabay.com/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=4805387