Patterns: composition · 3 MIN READ

Adapter: translate one contract into another

Keep conversion details at the boundary that needs them.

Keep conversion details at the boundary that needs them. An adapter lets an existing implementation satisfy a different caller contract. Trace the worked Go example, then compare the benefit with the cost before applying this design to your own project.

Structural pattern · A MAP OF THE IDEA

  1. Consumer expects Fahrenheit
  2. Adapter converts units and method shape
  3. Existing sensor supplies Celsius

Useful when

  • An external API does not match the consumer
  • Conversions should stay out of domain policy

Watch out

  • Error translation must preserve meaning
  • An unnecessary wrapper increases indirection

Implementation & operation cost: Low

Fit depends on the task

Small tool or CLI4/5
Domain-rich application5/5
Independent distributed services4/5

Editorial suitability, not benchmarks or a universal ranking. Styles describe different axes and can be combined.

01

The design decision

An adapter lets an existing implementation satisfy a different caller contract. It translates data or method shape while preserving meaningful errors. Keep units explicit: an adapter is a useful place to prevent two compatible-looking numbers from meaning different things.

02

A complete Go example

Read this program from the entry point and follow the calls shown in the diagram. The output is included at the end so you can check the behavior without a compiler.

go
package main

import (
	"fmt"
)

type CelsiusSensor struct{}

func (CelsiusSensor) Celsius() float64 { return 20 }

type Fahrenheit interface{ Fahrenheit() float64 }
type SensorAdapter struct{ source CelsiusSensor }

func (a SensorAdapter) Fahrenheit() float64 { return a.source.Celsius()*9/5 + 32 }
func Display(s Fahrenheit)                  { fmt.Println(s.Fahrenheit()) }

func main() {
	Display(SensorAdapter{source: CelsiusSensor{}})
}

// Output:
// 68
Read the code

SensorAdapter translates Celsius into Fahrenheit and satisfies the consumer’s method implicitly. Display does not know the source API. In a real sensor adapter, also translate unavailable readings and calibration failures without inventing a valid zero temperature.

03

Tradeoffs and the simpler alternative

Use an adapter for an actual mismatch. If you own both sides and can make their contracts agree directly, another wrapper may add no value. Adapter changes the interface; Decorator usually preserves it while adding behavior.

TAKE THIS WITH YOU

Translates an existing interface for a different caller. Use an adapter for an actual mismatch. If you own both sides and can make their contracts agree directly, another wrapper may add no value. Adapter changes the interface; Decorator usually preserves it while adding behavior.

Check your understanding

Which statement best describes Adapter: translate one contract into another?

Go to the source

Progress stays in this browser. No account needed.