Build Shapes with Swappable Renderers

Bridge pattern

Challenge 4 of Make It PluggableIntermediate~55 minFree

Learn the Bridge pattern by decoupling shapes from the backends that render them. You build a Renderer implementation hierarchy (vector and raster) and a Shape abstraction (circle and square) that talks to a renderer through a reference. Because the two hierarchies vary independently, two shapes times two renderers reuse just four classes instead of exploding into a class per combination, and you can even swap the backend at runtime.

5 steps

  1. 1Define the Renderer implementations
  2. 2Bridge a Circle to its renderer
  3. 3Add squares to both backends
  4. 4Swap renderers at runtime
  5. 5Draw many shapes with no class explosion
Start step 1