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

Intent. Keep the interface and control access – lazy loading, caching, permissions, remoting.

In Kotlin. by lazy is a virtual proxy built into the language; by delegation covers the rest.

Structurally identical to Decorator, with a different job: the same face, controlled access. If you are adding behavior it is a decorator; if you are deciding whether, when, or for whom the original behavior runs, it is a proxy.

Structure

classDiagram
    class Subject {
        <<interface>>
        +request()
    }
    class RealSubject {
        +request()
    }
    class Proxy {
        -real: RealSubject
        +request()
    }
    Subject <|.. RealSubject
    Subject <|.. Proxy
    Proxy o--> RealSubject : controls access to

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.
  • Adapter — Change an interface so two otherwise incompatible things can work together.