Choosing a Structural Pattern

Choosing a structural design pattern: telling Adapter, Decorator, Facade, Proxy, Composite, Bridge and Flyweight apart by what each wrapper is for.

  • 5 cards
  • 4 questions
  • About 8 min
  • Intermediate to Advanced
Share

Five of the seven structural patterns put one object in front of another, and their class diagrams look much alike. What tells them apart is the promise each makes to the caller: whether the interface changes, whether behaviour is added, whether access is controlled, and how many objects sit behind it. This topic gathers those differences in one place, so that a pattern can be named from the code in front of you.

It opens on the four wrappers most often confused, Adapter, Decorator, Proxy and Facade, with one weather service wrapped all four ways so that only the wrapper changes. Two shorter comparisons follow for the pairs that remain: Bridge against Adapter, which differ in when they are introduced, and Composite against Decorator, which differ in how many objects they hold.

The exercise gives seven short listings, one per structural pattern, to name from the code, and the recap gathers the two questions that decide most cases.

Contents

5 cardsnot started