Skip to content

Decorator Chain Of Responsibility

BrandonRobare edited this page Jun 2, 2026 · 1 revision

Decorator & Chain of Responsibility

Folder: 11-decorator-chain-of-responsibility

What it is

A coffee shop. A customer orders a base drink and adds extras, and a line of baristas decides who is allowed to make it. One program shows two patterns at once. The extras are a Decorator, and the baristas are a Chain of Responsibility.

Decorator

Intent. Attach extra behavior to an object by wrapping it in another object of the same interface, so you can stack additions without subclassing every combination.

How this code does it. Drink is the base, with getName and getPrice. DrinkDecorator is an abstract wrapper that holds a Drink* and shares its interface. Sugar, Cream, and Honey each wrap a drink, forward getName and getPrice to the inner drink, and add their own name fragment and price. Because every decorator is itself a Drink, you can nest them: new Honey(new Sugar(new Drink(...))) is a drink with sugar and honey, and asking it for a price walks the chain inward summing each layer.

classDiagram
    class Drink {
        #int price_
        #DrinkType type_
        +getPrice() int
        +getName() string
    }
    class DrinkDecorator {
        #Drink* drink_
    }
    class Sugar {
        +getPrice() int
        +getName() string
    }
    class Cream
    class Honey
    Drink <|-- DrinkDecorator
    DrinkDecorator <|-- Sugar
    DrinkDecorator <|-- Cream
    DrinkDecorator <|-- Honey
    DrinkDecorator o-- Drink : wraps
Loading

Chain of Responsibility

Intent. Pass a request along a chain of handlers. Each handler either deals with the request or forwards it to the next, so the sender does not need to know which handler will act.

How this code does it. Barista is the handler base. It holds a next_ pointer and a serve method. Each concrete barista, JuniorBarista, SeniorBarista, and Manager, overrides canHandle to say which orders it accepts: a junior takes only plain orders, a senior takes anything without honey, and the manager takes everything. The main links them junior to senior to manager, then calls serve on the junior. If a barista cannot handle the order it forwards to next_, so a honey order falls past the junior and the senior and lands on the manager. When a barista accepts, it builds the decorated drink and prints the order.

classDiagram
    class Barista {
        #ExperienceLevel level_
        #Barista* next_
        +setNext(Barista*) void
        +serve(vector, Drink*, string) bool
        #canHandle(vector)* bool
        #getTitle()* string
    }
    class JuniorBarista
    class SeniorBarista
    class Manager
    Barista <|-- JuniorBarista
    Barista <|-- SeniorBarista
    Barista <|-- Manager
    Barista o-- Barista : next handler
Loading

Build and run

g++ -std=c++17 11-decorator-chain-of-responsibility/drink.cpp -o coffee
./coffee

The program prompts for a size, then for each extra, then your name, and prints the finished order with its price.

Notes

The Drink and DrinkDecorator skeleton came from the course. The barista chain, the serve logic that builds the decorated order, and the main are mine. The folder name leads with Decorator because that is the lab's headline, but the Chain of Responsibility carries equal weight in the program.

Clone this wiki locally