M3 · Wrappers: The Four SiblingsSession 6-7StructuralKotlin replaces itOutline

Intent. Change an interface so two otherwise incompatible things can work together.

In Kotlin. An extension function is often the whole adapter.

First of the four wrappers. All four wrap another object and forward calls; only their intent differs. Adapter is the one that changes the interface so two incompatible things can meet. Learn the four together or you will confuse them forever.

Structure

classDiagram
    class Client
    class Target {
        <<interface>>
        +request()
    }
    class Adapter {
        +request()
    }
    class Adaptee {
        +specificRequest()
    }
    Client --> Target
    Target <|.. Adapter
    Adapter o--> Adaptee : wraps

Telling It Apart

Patterns whose structure looks the same but whose intent does not. See the Confusing Pairs reference.

  • Decorator — Keep the interface and add responsibilities, stackable at runtime.
  • Facade — Put one simple interface in front of a complicated subsystem.
  • Bridge — Split abstraction from implementation so the two can vary independently.