Welcome To Golang By Example

Adapter Design Pattern in Go (GoLang)

Note: Interested in understanding how all other design patterns can be implemented in GO. Please see this full reference – All Design Patterns in Go (Golang)

Introduction:

This design pattern is a Structural Design Pattern. The patter is best understood with an example. Let’s say you have two laptops

  1. MacBook Pro
  2. Windows Laptop

MacBook Pro has a USB port that is square in shape and Windows have a USB port that is circular in shape. You as a client have a USB cable that is square in shape so it can only be inserted in the mac laptop. So you see the problem here

Problem:

This is where Adapter Pattern comes into the picture. We create a class known as Adapter that will

When to Use

UML Diagram

Below is the corresponding mapping UML diagram with the example given above

Mapping 


The below table represents the mapping from the UML diagram actors to actual implementation actors in code.

Targetcomputer.go
Concrete Prototype 1mac.go
Concrete Prototype 2 (Adapter)windowsAdapter.go
adapteewindows.go
clientclient.go

Example:

computer.go

package main

type computer interface {
    insertInSquarePort()
}

mac.go

package main

import "fmt"

type mac struct {
}

func (m *mac) insertInSquarePort() {
    fmt.Println("Insert square port into mac machine")
}

windowsAdapter.go

package main

type windowsAdapter struct {
	windowMachine *windows
}

func (w *windowsAdapter) insertInSquarePort() {
	w.windowMachine.insertInCirclePort()
}

windows.go

package main

import "fmt"

type windows struct{}

func (w *windows) insertInCirclePort() {
    fmt.Println("Insert circle port into windows machine")
}

client.go

package main

type client struct {
}

func (c *client) insertSquareUsbInComputer(com computer) {
    com.insertInSquarePort()
}

main.go

package main

func main() {
    client := &client{}
    mac := &mac{}
    client.insertSquareUsbInComputer(mac)
    windowsMachine := &windows{}
    windowsMachineAdapter := &windowsAdapter{
        windowMachine: windowsMachine,
    }
    client.insertSquareUsbInComputer(windowsMachineAdapter)
}

Output:

Insert square port into mac machine
Insert circle port into windows machine