Structural Design Patterns: Facade, Flyweight, and Proxy
Structural design patterns help you organize classes and objects.
Today we cover the final three patterns to complete your toolkit.
- The Facade Pattern Facade simplifies complex systems. It provides one simple interface to a group of messy classes.
Think of a movie theater. To watch a movie, you need to dim lights, start the projector, and open curtains. Instead of calling five different systems, you call one method: theater.watch_movie().
Use it when:
- You want to simplify a complex subsystem.
- You need a single entry point for a large API.
- You want to decouple clients from internal logic.
- The Flyweight Pattern Flyweight saves memory. It works when you have thousands of similar objects.
Instead of storing every detail in every object, you split the data. You keep shared, unchanging data (intrinsic state) in one place. You keep unique data (extrinsic state) separate.
Use it when:
- Memory usage is a real problem.
- You manage millions of similar objects, like characters in a text editor or particles in a game.
- You want to use object pooling to improve performance.
- The Proxy Pattern Proxy acts as a stand-in for another object. It sits between the client and the real object to control access.
A proxy can:
- Lazy load: Load heavy images only when a user clicks them.
- Control access: Check if a user has permission to delete a database.
- Log activity: Track who uses a specific service.
- Cache results: Return saved data instead of running expensive logic.
Use it when:
- You need to delay expensive operations.
- You must protect a sensitive service.
- You want to add logging or security without changing the original class.
Summary Table
• Adapter: Makes incompatible systems work together. • Bridge: Decouples abstraction from implementation. • Composite: Builds tree structures. • Decorator: Adds behavior without modifying classes. • Facade: Simplifies complex subsystems. • Flyweight: Shares data to save memory. • Proxy: Controls access to objects.
The Golden Rule: Use these patterns to make code maintainable. Do not use them just to show off.
Next, we start the Behavioral Design Patterns series.