System Design Academy

Saving…

Lesson 5 of 11 in Module 30 of 45 lessons complete

Adapter

Module 3, lesson 5Concept12 min

Definition

Adapter

In plain English
An adapter wraps a type with the wrong interface so it can be used where a different interface is expected.
Think of it like
It is like a travel plug adapter that lets a UK plug fit a European socket.

Why it exists

Your refill service expects Check to return approval and an error. The insurer client accepts XML and returns numeric codes.

Repeating conversions across callers spreads the insurer’s quirks. Put the translation in one wrapper.

How it works

Third-party SDK

In plain English
A third-party SDK is code from another company that you use but can't change.
Think of it like
Like a rented appliance you can plug in but not rewire.

XML

In plain English
XML is a text format that uses named tags to describe data.
Think of it like
It is like labelled envelopes showing what each piece of information contains.
Complete library with Demo: translate prescription input and insurer status codes.
Go
package adapters

import (
	"encoding/xml"
	"errors"
	"fmt"
)

type Insurer interface{ Check(string) (bool, error) }
type LegacyClient struct{}

func (LegacyClient) VerifyClaim(payload string) int { return 200 }

type LegacyInsurerAdapter struct{ client LegacyClient }

func (a LegacyInsurerAdapter) Check(rx string) (bool, error) {
	payload, err := xml.Marshal(struct{ Code string }{Code: rx})
	if err != nil {
		return false, err
	}
	switch a.client.VerifyClaim(string(payload)) {
	case 200:
		return true, nil
	case 403:
		return false, nil
	default:
		return false, errors.New("insurer unavailable")
	}
}
func Demo() {
	var insurer Insurer = LegacyInsurerAdapter{LegacyClient{}}
	fmt.Println(insurer.Check("RX&42"))
}

LegacyInsurerAdapter satisfies Insurer and delegates translated calls to LegacyClient.

The refill service depends on its own interface.

LegacyInsurerAdapter satisfies Insurer and delegates translated calls to LegacyClient.

  1. State your need

    Define the interface your application wants.

  2. Wrap the outside type

    Store the vendor client in a small adapter.

  3. Translate both ways

    Convert inputs, outputs, units and errors.

  4. Keep callers focused

    Callers use your interface; creation code wires up the vendor client.

Examples

Translate insurer responses

The old insurer returns numeric status codes. Your refill code needs approval or an error.

Complete library with Demo: translate insurer responses.
Go
package adapters

import (
	"encoding/xml"
	"errors"
	"fmt"
)

type Insurer interface{ Check(string) (bool, error) }
type LegacyClient struct{}

func (LegacyClient) VerifyClaim(payload string) int { return 200 }

type LegacyInsurerAdapter struct{ client LegacyClient }

func (a LegacyInsurerAdapter) Check(rx string) (bool, error) {
	payload, err := xml.Marshal(struct{ Code string }{Code: rx})
	if err != nil {
		return false, err
	}
	switch a.client.VerifyClaim(string(payload)) {
	case 200:
		return true, nil
	case 403:
		return false, nil
	default:
		return false, errors.New("insurer unavailable")
	}
}
func Demo() {
	var insurer Insurer = LegacyInsurerAdapter{LegacyClient{}}
	fmt.Println(insurer.Check("RX&42"))
}

Here, 200 means approved and 403 means declined. Other codes become errors; these meanings are assumptions of this demonstration.

The insight

An adapter contains a foreign API’s quirks in one place.

Trade-offs

Pros

  • Callers use a clean interface.
  • Provider-specific translations stay together.

Cons

  • Adds one type per provider.
  • Conversions can lose information.

Use when

  • An outside library cannot be changed.
  • Units or error formats differ.

Avoid when

  • You own both sides and can align them.
  • The existing type already satisfies your interface.

Quick check

A vendor library has the right behaviour but the wrong method names and types for your interface. Which pattern?
After adding an adapter, which part of your code knows about the vendor library?

Takeaways

  • An adapter makes a mismatched type fit your interface.
  • It keeps foreign quirks in one place.
  • Use it for code you can't change.

Try the quick check above whenever you feel ready.

Previous: Singleton with sync.Once — and Why to Be Careful