📘 Definition of Structural Design Pattern
Structural Design Patterns are design patterns that deal with object composition—that is, how classes and objects are composed to form larger structures. They help ensure that if one part of a system changes, the entire system doesn't need to change.
🔧 Definition:
Structural design patterns define the composition of classes or objects to form larger structures while keeping the system flexible and efficient.
🧱 Common Structural Design Patterns
Here are the 7 main structural design patterns:
-
Adapter – Allows incompatible interfaces to work together.
-
Bridge – Separates abstraction from implementation.
-
Composite – Treats individual objects and compositions uniformly.
-
Decorator – Adds behavior to objects dynamically.
-
Facade – Provides a simplified interface to a complex system.
-
Flyweight – Shares common parts of state between multiple objects.
-
Proxy – Acts as a placeholder or surrogate for another object.
✅ How to Check or Identify a Structural Design Pattern
To recognize or use a structural design pattern in a codebase, ask the following:
Question | If Yes, Then... |
---|---|
Does this part of the system wrap an object to modify or simplify its behavior? | Decorator or Proxy |
Are different interfaces being made compatible? | Adapter |
Is an abstraction separated from its implementation? | Bridge |
Are objects being organized into a tree structure? | Composite |
Is a simplified interface provided over a complex system? | Facade |
Is memory being saved by sharing data between objects? | Flyweight |
Is a stand-in object controlling access to a real one? | Proxy |
🔍 How to "Check Out" a Structural Pattern in Code
If you're exploring existing code and want to identify structural design patterns:
-
Look for class hierarchies that wrap other classes or delegate behavior.
-
Check for methods that reference or instantiate other classes in a modular way.
-
Identify interfaces or base classes with multiple implementations.
-
Search for pattern names in documentation or comments (
Adapter
,Decorator
, etc.). -
Use UML diagrams or visual tools to see class relationships clearly.
The Adapter Design Pattern is a structural design pattern that allows objects with incompatible interfaces to work together. It acts as a bridge between two incompatible interfaces.
🔧 Definition
The Adapter pattern converts the interface of a class into another interface that a client expects. It allows classes to work together that couldn’t otherwise because of incompatible interfaces.
🧱 Components
-
Target Interface: The interface the client expects.
-
Adaptee: The existing class with a different interface.
-
Adapter: The class that implements the target interface and translates the requests to the adaptee.
🔄 Real-world Analogy
Imagine a power adapter that allows a US plug (adaptee) to fit into a European socket (target). The adapter converts the plug shape and voltage as needed.
✅ When to Use
-
You want to reuse an existing class, but its interface does not match the one you need.
-
You want to create a reusable class that cooperates with classes that don’t necessarily have compatible interfaces.
👨💻 Example in Python
✅ Output
0 Comments