𝗦𝘁𝗿𝘂𝗰𝘁𝘂𝗿𝗮𝗹 𝗗𝗲𝘀𝗶𝗴𝗻 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀: 𝗙𝗮𝗰𝗮𝗱𝗲, 𝗙𝗹𝘆𝘄𝗲𝗶𝗴𝗵𝘁, 𝗮𝗻𝗱 𝗣𝗿𝗼𝘅𝘆

Structural design patterns help you organize classes and objects.

Today we cover the final three patterns to complete your toolkit.

𝟭. 𝗧𝗵𝗲 𝗙𝗮𝗰𝗮𝗱𝗲 𝗣𝗮𝘁𝘁𝗲𝗿𝗻 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.

𝟮. 𝗧𝗵𝗲 𝗙𝗹𝘆𝘄𝗲𝗶𝗴𝗵𝘁 𝗣𝗮𝘁𝘁𝗲𝗿𝗻 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.

𝟯. 𝗧𝗵𝗲 𝗣𝗿𝗼𝘅𝘆 𝗣𝗮𝘁𝘁𝗲𝗿𝗻 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.

𝗦𝘂𝗺𝗺𝗮𝗿𝘆 𝗧𝗮𝗯𝗹𝗲

• 𝗔𝗱𝗮𝗽𝘁𝗲𝗿: Makes incompatible systems work together. • 𝗕𝗿𝗶𝗱𝗴𝗲: Decouples abstraction from implementation. • 𝗖𝗼𝗺𝗽𝗼𝘀𝗶𝘁𝗲: Builds tree structures. • 𝗗𝗲𝗰𝗼𝗿𝗮𝘁𝗼𝗿: Adds behavior without modifying classes. • 𝗙𝗮𝗰𝗮𝗱𝗲: Simplifies complex subsystems. • 𝗙𝗹𝘆𝘄𝗲𝗶𝗴𝗵𝘁: Shares data to save memory. • 𝗣𝗿𝗼𝘅𝘆: 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.

Źródło: https://dev.to/mahdi0shamlou/mahdi-shamlou-structural-design-patterns-2026-facade-flyweight-proxy-production-examples-g5l