Chapter

Structural patterns

How to compose classes and objects into larger structures: adapter, decorator, facade, composite, proxy, and bridge, each with a small PHP example.

Design Patterns & Principles 6 Lessons from courses

About this chapter

Creational patterns were about making objects. Structural patterns are about combining them - fitting classes and objects together into bigger structures that stay flexible.

This chapter covers six Gang of Four structural patterns: the adapter (make an incompatible class fit), the decorator (add behavior by wrapping), the facade (a simple front over a messy subsystem), the composite (treat a tree uniformly), the proxy (a stand-in that controls access), and the bridge (split an abstraction from its implementation). Each solves a real "how do I connect these parts" problem you'll meet in everyday code.

Lessons from courses

  1. 1 Adapter - Make an Incompatible Class Fit Learn the adapter pattern: wrap a class with a mismatched interface so it fits the one your code expects. Adapt a payment gateway in PHP.
  2. 2 Decorator - Add Behavior by Wrapping Learn the decorator pattern: wrap an object to add behavior without subclassing, and stack decorators. Add caching and logging in PHP.
  3. 3 Facade - A Simple Front Over a Subsystem Learn the facade pattern: hide a complex subsystem behind one clean method. See a PHP example and how it differs from a framework Facade.
  4. 4 Composite - Treat a Tree Uniformly Learn the composite pattern: handle leaves and containers through one interface so a whole tree behaves like a single object. PHP example.
  5. 5 Proxy - Control Access to an Object Learn the proxy pattern: a stand-in with the same interface that controls access to the real object - lazy loading, caching, access checks.
  6. 6 Bridge - Split Abstraction From Implementation Learn the bridge pattern: separate an abstraction from its implementation so both vary independently and avoid a class explosion. PHP example.
Design Patterns & Principles Go to course