Adapter Pattern with Golang

Carlos Garcia
1 min readJan 26, 2021

Hey everyone I wanna share this post with the community for learn how to implement the adapter pattern using Golang for this we will use an example code about how this works with a programming language didn’t have class.

¿What it’s Adapter Pattern?

Adapter pattern is used to make two interfaces, in principle different, can communicate. For this we will add an intermediate adapter, which will be in charge of converting from one interface to another.

Jumping to the code for understand it better.

First we will create a struct represent a Bicycle

And other struct for represent a Car

We need to create an interface for communicate the same order to this structs and this two structs understand us and execute the order.

and a factory for get the right adapter

with this we will have the possibility for communicate the same order to this different structs ignoring how this works inside because now we have a “translator” can speak with both of this.

So with this Design pattern we can make a translator for communicate with different structs they not necessary works same internally.

You can check a complete video guide I take as reference for understand how this pattern works.

--

--