# Three Wrappers, Three Reasons

The hub now has three kinds of class that look the same on paper, each holding one object and passing calls on: TuneoSpeakerAdapter, CachedWeather and GuardedLock. What each is for tells them apart (a new shape, a new feature, or control of the way in), and that decides where the next rule belongs.

Journeyman · OOP · Updated November 17, 2026

By Gregor Dzierzon

## After this lesson you can

- Tell Adapter, Decorator and Proxy apart by what each wrapper is for: a new shape, a new feature, or control of the way in
- Read an object diagram of wrapped objects and name the pattern each layer plays

## Key takeaways

- Adapter, Decorator and Proxy have nearly the same diagram: an object that holds another and passes calls on. Their intent is what differs.
- Adapter changes the shape: your interface outside, someone else's inside. Decorator keeps the shape and adds a feature. Proxy keeps the shape and controls the way in.
- Wrappers stack, and each layer keeps its own job: a proxy around an adapter around a brand's lock is normal.
- Put a new rule in the wrapper whose job it is; a permission check hidden in an adapter is lost the day the brand changes.

**Before this:** *Adapter*, *Proxy* and *Decorator*. This lesson compares them; it uses object diagrams, from *Object Diagrams: A Snapshot of Real Objects*.

Maya has a new rule: a guest can't play music on the kitchen speaker after ten at night. Leo opens the hub's code to add it, and stops. The speaker is already wrapped: `TuneoSpeakerAdapter` holds the Tuneo speaker and passes calls on. So is the weather (`CachedWeather` holds SkyCast) and the front door (`GuardedLock` holds the lock). Drawn as class diagrams, the three look the same: a box that keeps an interface, with an arrow to the one object it holds. Leo's first idea is to put the rule in the speaker's adapter, since that's the class already wrapped around the speaker. This lesson is about why that's the wrong home, and how to tell the three wrappers apart.

## Three wrappers in one house

Here are all three, from the last three lessons, working together. The front door's proxy wraps its adapter, so the door has two layers; the guest and Maya each get their own proxy, around the same door:

```csharp
const string News = "https://tuneo.example/news";

ISpeaker speaker = new TuneoSpeakerAdapter(new TuneoSpeaker("kitchen"));
IWeatherSource weather = new CachedWeather(new SkyCast());
ILock frontDoor = new BoltLockAdapter(new BoltLock("front door"));
ILock guestsDoor = new GuardedLock(frontDoor, "guest");
ILock mayasDoor = new GuardedLock(frontDoor, "Maya");

speaker.Play(News);
Console.WriteLine("Wall panel: " + weather.Today());
Console.WriteLine("Phone app: " + weather.Today());
guestsDoor.Unlock();
mayasDoor.Unlock();

interface ISpeaker { void Play(string station); void Stop(); }
interface IWeatherSource { string Today(); }
interface ILock { void Lock(); void Unlock(); }

// The brands' and SkyCast's own code.
class TuneoSpeaker { private readonly string room; public TuneoSpeaker(string room) { this.room = room; } public void StartStream(string url) => Console.WriteLine($"Tuneo [{room}]: streaming {url}"); public void Halt() => Console.WriteLine($"Tuneo [{room}]: halted"); }
class BoltLock { private readonly string door; public BoltLock(string door) { this.door = door; } public void Engage() => Console.WriteLine($"Bolt [{door}]: ENGAGED"); public void Release() => Console.WriteLine($"Bolt [{door}]: RELEASED"); }
class SkyCast : IWeatherSource { public string Today() { Console.WriteLine("SkyCast: asked over the internet (about 2 seconds)"); return "72F, sunny"; } }

// Adapter: the hub's shape outside, the brand's inside.
class TuneoSpeakerAdapter : ISpeaker
{
    private readonly TuneoSpeaker tuneo;
    public TuneoSpeakerAdapter(TuneoSpeaker tuneo) { this.tuneo = tuneo; }
    public void Play(string station) => tuneo.StartStream(station);
    public void Stop() => tuneo.Halt();
}

class BoltLockAdapter : ILock
{
    private readonly BoltLock bolt;
    public BoltLockAdapter(BoltLock bolt) { this.bolt = bolt; }
    public void Lock() => bolt.Engage();
    public void Unlock() => bolt.Release();
}

// Decorator: the same shape, with a feature added.
class CachedWeather : IWeatherSource
{
    private readonly IWeatherSource inner;
    private string? saved;
    public CachedWeather(IWeatherSource inner) { this.inner = inner; }
    public string Today() => saved ??= inner.Today();
}

// Proxy: the same shape, with control of the way in.
class GuardedLock : ILock
{
    private readonly ILock real;
    private readonly string person;
    public GuardedLock(ILock real, string person) { this.real = real; this.person = person; }
    public void Lock() => real.Lock();
    public void Unlock()
    {
        if (person == "guest") { Console.WriteLine("Front door: guests can't unlock it"); return; }
        real.Unlock();
    }
}
```

```text
Tuneo [kitchen]: streaming https://tuneo.example/news
SkyCast: asked over the internet (about 2 seconds)
Wall panel: 72F, sunny
Phone app: 72F, sunny
Front door: guests can't unlock it
Bolt [front door]: RELEASED
```

```java
interface Speaker { void play(String station); void stop(); }
interface WeatherSource { String today(); }
interface Lock { void lock(); void unlock(); }

// The brands' and SkyCast's own code.
class TuneoSpeaker { private final String room; TuneoSpeaker(String room) { this.room = room; } void startStream(String url) { IO.println("Tuneo [" + room + "]: streaming " + url); } void halt() { IO.println("Tuneo [" + room + "]: halted"); } }
class BoltLock { private final String door; BoltLock(String door) { this.door = door; } void engage() { IO.println("Bolt [" + door + "]: ENGAGED"); } void release() { IO.println("Bolt [" + door + "]: RELEASED"); } }
class SkyCast implements WeatherSource { public String today() { IO.println("SkyCast: asked over the internet (about 2 seconds)"); return "72F, sunny"; } }

// Adapter: the hub's shape outside, the brand's inside.
class TuneoSpeakerAdapter implements Speaker {
    private final TuneoSpeaker tuneo;
    TuneoSpeakerAdapter(TuneoSpeaker tuneo) { this.tuneo = tuneo; }
    public void play(String station) { tuneo.startStream(station); }
    public void stop() { tuneo.halt(); }
}

class BoltLockAdapter implements Lock {
    private final BoltLock bolt;
    BoltLockAdapter(BoltLock bolt) { this.bolt = bolt; }
    public void lock() { bolt.engage(); }
    public void unlock() { bolt.release(); }
}

// Decorator: the same shape, with a feature added.
class CachedWeather implements WeatherSource {
    private final WeatherSource inner;
    private String saved;
    CachedWeather(WeatherSource inner) { this.inner = inner; }
    public String today() {
        if (saved == null) saved = inner.today();
        return saved;
    }
}

// Proxy: the same shape, with control of the way in.
class GuardedLock implements Lock {
    private final Lock real;
    private final String person;
    GuardedLock(Lock real, String person) { this.real = real; this.person = person; }
    public void lock() { real.lock(); }
    public void unlock() {
        if (person.equals("guest")) { IO.println("Front door: guests can't unlock it"); return; }
        real.unlock();
    }
}

final String NEWS = "https://tuneo.example/news";

void main() {
    Speaker speaker = new TuneoSpeakerAdapter(new TuneoSpeaker("kitchen"));
    WeatherSource weather = new CachedWeather(new SkyCast());
    Lock frontDoor = new BoltLockAdapter(new BoltLock("front door"));
    Lock guestsDoor = new GuardedLock(frontDoor, "guest");
    Lock mayasDoor = new GuardedLock(frontDoor, "Maya");

    speaker.play(NEWS);
    IO.println("Wall panel: " + weather.today());
    IO.println("Phone app: " + weather.today());
    guestsDoor.unlock();
    mayasDoor.unlock();
}
```

```text
Tuneo [kitchen]: streaming https://tuneo.example/news
SkyCast: asked over the internet (about 2 seconds)
Wall panel: 72F, sunny
Phone app: 72F, sunny
Front door: guests can't unlock it
Bolt [front door]: RELEASED
```

```python
NEWS = "https://tuneo.example/news"


# The brands' and SkyCast's own code.
class TuneoSpeaker:
    def __init__(self, room): self.room = room
    def start_stream(self, url): print(f"Tuneo [{self.room}]: streaming {url}")
    def halt(self): print(f"Tuneo [{self.room}]: halted")


class BoltLock:
    def __init__(self, door): self.door = door
    def engage(self): print(f"Bolt [{self.door}]: ENGAGED")
    def release(self): print(f"Bolt [{self.door}]: RELEASED")


class SkyCast:
    def today(self):
        print("SkyCast: asked over the internet (about 2 seconds)")
        return "72F, sunny"


class TuneoSpeakerAdapter:
    """Adapter: the hub's shape outside, the brand's inside."""

    def __init__(self, tuneo): self.tuneo = tuneo
    def play(self, station): self.tuneo.start_stream(station)
    def stop(self): self.tuneo.halt()


class BoltLockAdapter:
    def __init__(self, bolt): self.bolt = bolt
    def lock(self): self.bolt.engage()
    def unlock(self): self.bolt.release()


class CachedWeather:
    """Decorator: the same shape, with a feature added."""

    def __init__(self, inner):
        self.inner = inner
        self.saved = None

    def today(self):
        if self.saved is None:
            self.saved = self.inner.today()
        return self.saved


class GuardedLock:
    """Proxy: the same shape, with control of the way in."""

    def __init__(self, real, person):
        self.real = real
        self.person = person

    def lock(self):
        self.real.lock()

    def unlock(self):
        if self.person == "guest":
            print("Front door: guests can't unlock it")
            return
        self.real.unlock()


speaker = TuneoSpeakerAdapter(TuneoSpeaker("kitchen"))
weather = CachedWeather(SkyCast())
front_door = BoltLockAdapter(BoltLock("front door"))
guests_door = GuardedLock(front_door, "guest")
mayas_door = GuardedLock(front_door, "Maya")

speaker.play(NEWS)
print("Wall panel: " + weather.today())
print("Phone app: " + weather.today())
guests_door.unlock()
mayas_door.unlock()
```

```text
Tuneo [kitchen]: streaming https://tuneo.example/news
SkyCast: asked over the internet (about 2 seconds)
Wall panel: 72F, sunny
Phone app: 72F, sunny
Front door: guests can't unlock it
Bolt [front door]: RELEASED
```

```javascript
const NEWS = "https://tuneo.example/news";

// The brands' and SkyCast's own code.
class TuneoSpeaker { constructor(room) { this.room = room; } startStream(url) { console.log(`Tuneo [${this.room}]: streaming ${url}`); } halt() { console.log(`Tuneo [${this.room}]: halted`); } }
class BoltLock { constructor(door) { this.door = door; } engage() { console.log(`Bolt [${this.door}]: ENGAGED`); } release() { console.log(`Bolt [${this.door}]: RELEASED`); } }
class SkyCast { today() { console.log("SkyCast: asked over the internet (about 2 seconds)"); return "72F, sunny"; } }

// Adapter: the hub's shape outside, the brand's inside.
class TuneoSpeakerAdapter {
  #tuneo;
  constructor(tuneo) { this.#tuneo = tuneo; }
  play(station) { this.#tuneo.startStream(station); }
  stop() { this.#tuneo.halt(); }
}

class BoltLockAdapter {
  #bolt;
  constructor(bolt) { this.#bolt = bolt; }
  lock() { this.#bolt.engage(); }
  unlock() { this.#bolt.release(); }
}

// Decorator: the same shape, with a feature added.
class CachedWeather {
  #inner;
  #saved = null;
  constructor(inner) { this.#inner = inner; }
  today() { return (this.#saved ??= this.#inner.today()); }
}

// Proxy: the same shape, with control of the way in.
class GuardedLock {
  #real;
  #person;
  constructor(real, person) { this.#real = real; this.#person = person; }
  lock() { this.#real.lock(); }
  unlock() {
    if (this.#person === "guest") { console.log("Front door: guests can't unlock it"); return; }
    this.#real.unlock();
  }
}

const speaker = new TuneoSpeakerAdapter(new TuneoSpeaker("kitchen"));
const weather = new CachedWeather(new SkyCast());
const frontDoor = new BoltLockAdapter(new BoltLock("front door"));
const guestsDoor = new GuardedLock(frontDoor, "guest");
const mayasDoor = new GuardedLock(frontDoor, "Maya");

speaker.play(NEWS);
console.log("Wall panel: " + weather.today());
console.log("Phone app: " + weather.today());
guestsDoor.unlock();
mayasDoor.unlock();
```

```text
Tuneo [kitchen]: streaming https://tuneo.example/news
SkyCast: asked over the internet (about 2 seconds)
Wall panel: 72F, sunny
Phone app: 72F, sunny
Front door: guests can't unlock it
Bolt [front door]: RELEASED
```

```typescript
// A module of its own (export {}), so this Lock isn't mixed up with the browser's built-in Lock type.
export {};

const NEWS = "https://tuneo.example/news";

interface Speaker { play(station: string): void; stop(): void; }
interface WeatherSource { today(): string; }
interface Lock { lock(): void; unlock(): void; }

// The brands' and SkyCast's own code.
class TuneoSpeaker { private readonly room: string; constructor(room: string) { this.room = room; } startStream(url: string): void { console.log(`Tuneo [${this.room}]: streaming ${url}`); } halt(): void { console.log(`Tuneo [${this.room}]: halted`); } }
class BoltLock { private readonly door: string; constructor(door: string) { this.door = door; } engage(): void { console.log(`Bolt [${this.door}]: ENGAGED`); } release(): void { console.log(`Bolt [${this.door}]: RELEASED`); } }
class SkyCast implements WeatherSource { today(): string { console.log("SkyCast: asked over the internet (about 2 seconds)"); return "72F, sunny"; } }

// Adapter: the hub's shape outside, the brand's inside.
class TuneoSpeakerAdapter implements Speaker {
  private readonly tuneo: TuneoSpeaker;
  constructor(tuneo: TuneoSpeaker) { this.tuneo = tuneo; }
  play(station: string): void { this.tuneo.startStream(station); }
  stop(): void { this.tuneo.halt(); }
}

class BoltLockAdapter implements Lock {
  private readonly bolt: BoltLock;
  constructor(bolt: BoltLock) { this.bolt = bolt; }
  lock(): void { this.bolt.engage(); }
  unlock(): void { this.bolt.release(); }
}

// Decorator: the same shape, with a feature added.
class CachedWeather implements WeatherSource {
  private readonly inner: WeatherSource;
  private saved: string | null = null;
  constructor(inner: WeatherSource) { this.inner = inner; }
  today(): string { return (this.saved ??= this.inner.today()); }
}

// Proxy: the same shape, with control of the way in.
class GuardedLock implements Lock {
  private readonly real: Lock;
  private readonly person: string;
  constructor(real: Lock, person: string) { this.real = real; this.person = person; }
  lock(): void { this.real.lock(); }
  unlock(): void {
    if (this.person === "guest") { console.log("Front door: guests can't unlock it"); return; }
    this.real.unlock();
  }
}

const speaker: Speaker = new TuneoSpeakerAdapter(new TuneoSpeaker("kitchen"));
const weather: WeatherSource = new CachedWeather(new SkyCast());
const frontDoor: Lock = new BoltLockAdapter(new BoltLock("front door"));
const guestsDoor: Lock = new GuardedLock(frontDoor, "guest");
const mayasDoor: Lock = new GuardedLock(frontDoor, "Maya");

speaker.play(NEWS);
console.log("Wall panel: " + weather.today());
console.log("Phone app: " + weather.today());
guestsDoor.unlock();
mayasDoor.unlock();
```

```text
Tuneo [kitchen]: streaming https://tuneo.example/news
SkyCast: asked over the internet (about 2 seconds)
Wall panel: 72F, sunny
Phone app: 72F, sunny
Front door: guests can't unlock it
Bolt [front door]: RELEASED
```

Each wrapper has the same few lines in it: a field for the object it holds, a constructor that's handed it, methods that pass calls on. The code alone doesn't say which pattern each one is. What it's *for* does.

## The same diagram, three intents

Drawn as role diagrams, side by side, the three are nearly the same picture. The difference is in the labels, and in where the fence goes:

![Three wrappers with the same shape, side by side, each the hub calling a wrapper that calls what it wraps. Adapter, a new shape: TuneoSpeakerAdapter says it in Tuneo's words. Decorator, a new feature: CachedWeather adds its bit. Proxy, control of the way in: GuardedLock passes it on if allowed](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/2d24a5fca919411196f08594fff17a04.webp)

Look at the wrapped boxes. The adapter's wraps something with **a different shape** (`startStream`, not `play`). The decorator's and the proxy's wrap something with **the same shape** as themselves; they keep it and add to the call (a feature) or stand in front of it (a decision). That gives two quick questions:

1. **Does the thing inside have a different interface from the wrapper?** Then it's an adapter.
2. **Same interface: does the wrapper add something to every call, or decide whether (or when) the call happens at all?** Adding is a decorator; deciding is a proxy.

## Adapter, Decorator or Proxy?

| Pattern | What it's for | Strengths | Weaknesses | Pick it when |
|---|---|---|---|---|
| **Adapter** | Makes something with the wrong shape keep the interface your code expects | Code you can't change fits without editing it; its words stay in one class | One class per thing that doesn't fit; it only translates, and anything more hides there | Something you need has a different interface from the one your code uses |
| **Decorator** | Adds a feature to an object without changing it, keeping the same interface | Features stack in any order; each one is written once and works on anything with that interface | Many small layers can be hard to follow; the order matters and isn't always obvious | You want to add behavior (caching, logging, counting) around something, in combinations |
| **Proxy** | Stands in for an object with the same interface and controls access to it: who, when or where | Every caller gets the rule without writing it; the real object stays simple | A slow or refused call can look like an ordinary one; one more hop | Calls to an object need a check, a delay until it's needed, or a trip across the network |

Decorator and Proxy are the closest pair, since both keep the shape. If you remove a decorator, the program still does its job, only without the feature (no cache, but still the weather). If you remove a protection proxy, the program still runs, but now it does something it shouldn't (a guest unlocks the door). That's a good test of which one you're looking at.

## The layers at run time

A class diagram can't show that the front door has two layers and two people's proxies; an object diagram can. Here are the hub's wrapped objects while the program above runs:

![The hub's wrappers as real objects, each layer named by its pattern: an adapter around the kitchen's TuneoSpeaker; a decorator, CachedWeather (saved "72F, sunny"), around SkyCast; and two proxies, the guest's door and Maya's door, sharing one BoltLockAdapter around the front door's BoltLock](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/79a5e56a8d6345faafe1d62b913ca603.webp)

Read the third group from the outside in. A guest's `unlock()` reaches `guestsDoor`, the proxy, which stops it. Maya's reaches `mayasDoor`, which passes it to `frontDoor`, the adapter, which says it in Bolt's words, `release()`. Two layers, two jobs, and each layer is still only doing its own.

## So where does Maya's new rule go?

"A guest can't play music after ten at night" decides whether a call gets through. That's a proxy's job: a `GuardedSpeaker` that keeps the `Speaker` interface, holds any speaker, and checks the person and the time before passing `play` on.

Leo's first idea, the check inside `TuneoSpeakerAdapter`, would work today. But the adapter's job is translating Tuneo's words. When Sam's bedroom speaker arrives from another brand, it gets a new adapter, and the rule isn't in it: the wall panel's bug from *Proxy*, all over again. In a proxy, the rule wraps *any* speaker, whatever brand is inside.

**Facade** looks different from all three: it doesn't wrap one object, it's a front on *many* (*Facade: One Button, Many Parts* has its own look-alike table, with Adapter and Mediator).

## Where you've already used them

- **Adapter:** Java's `InputStreamReader` around a stream of bytes, so it reads as text.
- **Decorator:** Java's `BufferedReader` around that reader, so it reads in big chunks, still a `Reader`.
- **Proxy:** the lazy-loaded orders Entity Framework hands back, and every web service client.

`new BufferedReader(new InputStreamReader(System.in))` is two of them stacked in one line: an adapter, then a decorator around it.

## When not to tell them apart

Sometimes one class really does two of these jobs, such as a SkyCast client that translates SkyCast's replies *and* stands in for a far-away service (adapter and remote proxy at once). That's fine when the jobs always go together. Name it for what it mostly does, and split it the day one of the jobs needs to change without the other.

**How often you'll meet them:** every day (our estimate: wrappers are everywhere; telling them apart is how you know where the next change goes).

## Check yourself

2 questions to try on the lesson page.

## So what

Three wrappers can share one diagram and still be three different tools. Ask what each one is for (a new shape, a new feature, or a door that decides), and put each new rule in the wrapper whose job it is.

## Key terms

- [Adapter Pattern](https://www.ask-codey.com/glossary/adapter-pattern)
- [Decorator Pattern](https://www.ask-codey.com/glossary/decorator-pattern)
- [Proxy Pattern](https://www.ask-codey.com/glossary/proxy-pattern)

## Cheat sheets

- [Three Wrappers, Three Reasons](https://www.ask-codey.com/cheat-sheets/dp-adapter-decorator-proxy-cheat-sheet)

Source: https://www.ask-codey.com/lessons/dp-adapter-decorator-proxy
