Algo-Sim › Design Patterns
Decorator: stack toppings at runtime
Easy · Design Patterns · step through it in Java, Python and C++ on your own input
Wrap an object in another object with the same interface to add behaviour, layer by layer, instead of subclassing every combination.
Problem
A drink starts as an espresso or a tea, and toppings (milk, caramel) can be added in any combination and any number. cost() returns the price of the drink with all its toppings. Model it so that adding a new topping is one new class, not one class per combination.
Constraints
- Each topping wraps a drink and implements the same interface
- cost() is O(number of toppings): each layer adds its price to the inner cost
- Here: espresso 3, tea 2, milk +1, caramel +2
Edge cases to test
- Decorating the same object twice
- The order of decorators changes the result
- A decorator that must reach the inner object
Follow-up questions
- How is it different from inheritance?
- Behaviour is combined at run time from small wrappers, instead of one subclass per combination.
- Where is it used?
- Java I/O streams (
BufferedInputStream), Python function decorators, and HTTP client middleware.
Solution
Java
interface Drink {
int cost();
}
class Espresso implements Drink {
public int cost() { return 3; }
}
class Tea implements Drink {
public int cost() { return 2; }
}
// A topping is a Drink that wraps another Drink and adds to its answer.
abstract class Topping implements Drink {
protected final Drink inner;
Topping(Drink inner) { this.inner = inner; }
}
class Milk extends Topping {
Milk(Drink inner) { super(inner); }
public int cost() { return inner.cost() + 1; }
}
class Caramel extends Topping {
Caramel(Drink inner) { super(inner); }
public int cost() { return inner.cost() + 2; }
}
class Order {
private Drink drink;
public void espresso() { drink = new Espresso(); }
public void tea() { drink = new Tea(); }
public void milk() { drink = new Milk(drink); }
public void caramel() { drink = new Caramel(drink); }
public int cost() { return drink.cost(); }
}Python
class Espresso:
def cost(self):
return 3
class Tea:
def cost(self):
return 2
# A topping wraps another drink and adds to its answer.
class Milk:
def __init__(self, inner):
self.inner = inner
def cost(self):
return self.inner.cost() + 1
class Caramel:
def __init__(self, inner):
self.inner = inner
def cost(self):
return self.inner.cost() + 2
class Order:
def __init__(self):
self.drink = None
def espresso(self):
self.drink = Espresso()
def tea(self):
self.drink = Tea()
def milk(self):
self.drink = Milk(self.drink)
def caramel(self):
self.drink = Caramel(self.drink)
def cost(self):
return self.drink.cost()C++
#include <bits/stdc++.h>
using namespace std;
struct Drink {
virtual int cost() const = 0;
virtual ~Drink() = default;
};
struct Espresso : Drink {
int cost() const override {
return 3;
}
};
struct Tea : Drink {
int cost() const override {
return 2;
}
};
struct Topping : Drink {
shared_ptr<Drink> inner;
explicit Topping(shared_ptr<Drink> d)
: inner(move(d)) {}
};
struct Milk : Topping {
using Topping::Topping;
int cost() const override {
return inner->cost() + 1;
}
};
struct Caramel : Topping {
using Topping::Topping;
int cost() const override {
return inner->cost() + 2;
}
};
class Order {
shared_ptr<Drink> drink;
public:
void espresso() {
drink = make_shared<Espresso>();
}
void tea() {
drink = make_shared<Tea>();
}
void milk() {
drink = make_shared<Milk>(drink);
}
void caramel() {
drink = make_shared<Caramel>(drink);
}
int cost() {
return drink->cost();
}
};
More Design Patterns problems
- Strategy: swappable discounts (Easy)
- Observer: publish and subscribe (Easy)
- State: a document workflow (Easy)
- Command: undo and redo (Medium)
- Chain of Responsibility: an ATM (Easy)
- Factory: create without naming classes (Easy)
- Singleton: one shared instance (Easy)
- Builder: a pizza step by step (Easy)
- Adapter: two legacy sensors, one interface (Easy)
- Proxy: a caching, lazy price service (Easy)
- Composite: folder sizes (Easy)