Part 2 of 6 · 1 min read
The problem
Too many combinations.
Think of ordering a coffee. A latte can come with extra shot, oat milk, caramel, or any mix. If each mix needed its own class, the menu would need a class for "latte with oat milk and caramel" and another for "latte with extra shot and caramel", and the count grows quickly with each new extra.
A room booking has the same shape. The base is a room for some hours. Extras are a projector, catering and a discount. Subclassing for each combination (RoomWithProjector, RoomWithCateringAndProjector and so on) gives a class for every subset. With three extras there are eight combinations, and with ten extras there are over a thousand.
GoF describes the problem in its own example. A text view may need a border, a scroll bar, or both. Making a subclass for each choice gives classes such as a bordered scrollable view. GoF notes that inheritance needs a new class for each added responsibility, which causes a large number of classes. HF's coffee shop example, with its explosion of drink subclasses, makes the same point and ties it to the open-closed principle of Level 2, module 2: add features by extending, not by editing old code.
Class explosion
A class explosion is a design where each combination of features needs its own subclass, so the number of classes grows much faster than the number of features.
Example: three booking extras needing eight classes.
Check yourself: 1. How many combinations do three extras give? 2. What does GoF say inheritance needs for each added responsibility?