loading...
Cover image for A pattern a day keeps the reviewers away - [Day 6] - Adapter Pattern

A pattern a day keeps the reviewers away - [Day 6] - Adapter Pattern

brownio profile image Antonio Djigo ・2 min read

Hey 👀✨

It's been 7 days already, that's totally a new achievement! I hope both you and I are learning a lot from these posts, and please, do not hesitate to contact me with your ideas and constructive criticism to make these post better and better!

Well, let's get started:

Unleash the post gif


The first day of a new week, and it's the turn for another design pattern, one that you may find really obvious, but also one that is not as used at it should be. Today, I'll be writing about the Adapter Controller.

Let's get in context. You may have worked before with some APIs that give you a lot of data in many different ways and shapes. Maybe you also had to transform this data and send it to a website, or a chart, or a PDF... Many options here.

Once you try to send the data for your first time, you notice that the service where you are sending the information needs your data in a specific format and type.

Here is where the Adapter Design Pattern gets into action.

Improvise, Adapt, Overcome GIF

This pattern functionality is pretty basic, you may have already realized how does it work. Is just the same logic as an electricity adapter.

A computer factory diagram

Diagram from https://www.javagists.com/adapter-design-pattern

What should you have in mind when working with Adapters?:

Well, there isn't much to say here. You'll always need to have in mind that the data transformation it's made inside the Adapter Class, and it will send it to the object that requested this information. It should be between the sender and the requester.

Want to know more about how to implement this pattern? Click here.

By following these simple rules, everything will fit together and you won't have to worry about that weird information that comes from that 0.1-alpha API you found.

Perfect fit gif

Perfect fit!

And this is my sixth post about design patterns for the series "A pattern a day keeps the reviewers away".

Bye!

I'll try to keep posting every day one of the different patterns that are around. I can't promise I'll fulfil this task every day though!

You can read more about this pattern at:

  1. Refactoring Guru
  2. Sourcemaking

Also, you can follow me so you are tuned to whenever I post something through my Twitter account!

Discussion

pic
Editor guide