Abstraction: hide the data structure
Change how a class stores its data without touching the code that uses it.
Watch the narrated video
Every step
Problem
Steps 1–3Refactor
Steps 4–8Payoff
Steps 9–10Recap
Steps 11–12The code: before and after
Before
struct Warehouse {
std::map<std::string, int> stock;
};
struct Receiving {
void receive(Warehouse& w, const std::string& sku, int qty) {
w.stock[sku] += qty;
}
};
struct Picking {
void pick(Warehouse& w, const std::string& sku, int qty) {
int have = w.stock[sku];
if (have >= qty) {
w.stock[sku] = have - qty;
}
}
};
struct Report {
void print(const Warehouse& w, const std::string& sku) {
int left = w.stock.contains(sku) ? w.stock.at(sku) : 0;
std::cout << sku << ": " << left << '\n';
}
};
int main() {
Warehouse w;
Receiving receiving;
Picking picking;
Report report;
receiving.receive(w, "TEA-1", 20);
receiving.receive(w, "TEA-1", 10);
picking.pick(w, "TEA-1", 15);
report.print(w, "TEA-1"); // TEA-1: 15
}After
class Warehouse {
public:
void receive(const std::string& sku, int qty) {
stock[sku].push_back(qty);
}
bool pick(const std::string& sku, int qty) {
if (quantity(sku) < qty) {
return false;
}
auto& queue = stock[sku];
while (qty > 0) {
int used = std::min(queue.front(), qty);
queue.front() -= used;
if (queue.front() == 0) {
queue.pop_front();
}
qty -= used;
}
return true;
}
int quantity(const std::string& sku) const {
if (!stock.contains(sku)) {
return 0;
}
const auto& queue = stock.at(sku);
return std::accumulate(queue.begin(), queue.end(), 0);
}
std::vector<int> deliveries(const std::string& sku) const {
auto it = stock.find(sku);
if (it == stock.end()) {
return {};
}
return std::vector<int>(it->second.begin(), it->second.end());
}
private:
std::map<std::string, std::deque<int>> stock;
};
struct Receiving {
void receive(Warehouse& w, const std::string& sku, int qty) {
w.receive(sku, qty);
}
};
struct Picking {
void pick(Warehouse& w, const std::string& sku, int qty) {
w.pick(sku, qty);
}
};
struct Report {
void print(const Warehouse& w, const std::string& sku) {
int left = w.quantity(sku);
std::cout << sku << ": " << left << '\n';
}
};
int main() {
Warehouse w;
Receiving receiving;
Picking picking;
Report report;
receiving.receive(w, "TEA-1", 20);
receiving.receive(w, "TEA-1", 10);
picking.pick(w, "TEA-1", 15);
report.print(w, "TEA-1"); // TEA-1: 15
for (int n : w.deliveries("TEA-1")) {
std::cout << n << ' '; // 5 10
}
}Transcript, step by step
Problem
Step 1: A shop's warehouse stock Before
A small shop's warehouse is a struct, so its stock map, from product code to quantity, is public. Receiving adds a delivery to the map, Picking takes items out when there are enough, and Report prints what's left. Two deliveries of tea come in, 20 and then 10, and after 15 are picked, the report prints 15.
Step 2: Oldest delivery first Code smell
The tea goes stale, so picking must take the oldest delivery first, and ops need the stock kept per delivery. The quick way turns each value in the map into a deque, a queue of delivery quantities. Receiving now pushes onto the back of the queue, and Picking sums it, then takes from the front in a loop. Report sums the queue too, and the run still prints 15.
Step 3: Every caller knows the map Code smell
Every caller works on the map directly, so each one depends on how the warehouse stores its stock. Receiving uses the map here, Picking here, and the report here. One change to the storage meant rewriting three classes that only wanted to receive, pick and count. The sum over the queue now exists twice, written one way in Picking and another way in Report. And when the stock moves to a database next year, all three get rewritten again.
Refactor
Step 4: Refactor first Back it out
Let's take the queue out again. We'll refactor first, so that the warehouse owns its stock, and then we'll change the storage in one place.
Step 5: Receive moves in Move Method
First, the update moves into Warehouse, as a member function called receive. Receiving now calls receive on the warehouse, and it no longer touches the map.
Step 6: Pick moves in Move Method
The check and the update move in next, as pick, which returns true when there was enough stock. Picking calls pick on the warehouse, and the run still prints 15.
Step 7: Quantity moves in Move Method
The lookup moves in last, as quantity, which returns zero for a product the warehouse has never seen. It's marked const because it only reads, so Report can call it on the read-only warehouse it gets.
Step 8: Make the map private Encapsulate Collection
No caller touches the map any more, so the struct becomes a class, and the map moves under private. Now the compiler stops any code outside Warehouse from reaching the map. That's abstraction, where callers use what the warehouse does, receive, pick and quantity, and never see how it stores the stock. Encapsulation hides a single field, and abstraction goes one step further, because it hides how a whole collection is stored.
Payoff
Step 9: Keep stock per delivery Substitute Algorithm
Now the change request comes back, and this time it stays inside Warehouse. The private map now holds a deque of deliveries per product, and receive pushes onto its back. Quantity sums the queue, and pick checks the quantity, then takes from the oldest delivery first. Each member function keeps its signature and gets a new body, and that move is called Substitute Algorithm. Receiving, Picking and Report didn't change, and main gains a loop that prints the deliveries.
Step 10: One class knows the storage Payoff
The map calls are gone from the callers, so Receiving calls receive here, Picking calls pick here, and Report calls quantity here. The sum over the queue is written once, in quantity, and pick reuses it for its check. The 15 picked came out of the oldest delivery of 20, so 5 of that one are left, next to the newer 10. The three callers depend only on receive, pick and quantity, so changing how Warehouse stores the stock stayed inside Warehouse.
Recap
Step 11: Where we started, where we ended Before / after
On the left, the stock is a public map, and Receiving, Picking and Report each work on it with map calls. On the right, the map is private and holds a deque per product, behind receive, pick and quantity. A caller like Picking makes one call, and the warehouse decides which delivery the items come from.
Step 12: When to reach for it Takeaways
Reach for it when several classes read or change one collection, and the way it's stored may change. Skip it when one short function owns the map and nothing outside reads it, because member functions around it add lines and hide nothing. You'll spot the need by subscripts, find or at on another object's collection, or by the same lookup copied into several classes. The deque we used works this way too, because you push and pop, and it manages its own blocks of memory.