0
0
Software Engineeringknowledge~6 mins

Structural patterns (Adapter, Decorator, Facade) in Software Engineering - Full Explanation

Choose your learning style9 modes available
Introduction
When building software, different parts often need to work together smoothly, even if they were not designed to do so. Structural patterns help solve problems related to how objects and classes are organized and connected to make systems easier to build and maintain.
Explanation
Adapter Pattern
The Adapter pattern acts like a translator between two incompatible interfaces. It allows one object to use another object's methods even if their interfaces don't match. This helps reuse existing code without changing it.
Adapter lets incompatible interfaces work together by translating calls.
Decorator Pattern
The Decorator pattern adds new features to an object dynamically without changing its structure. It wraps the original object with a new layer that provides extra behavior. This allows flexible and reusable enhancements.
Decorator adds new behavior to objects at runtime by wrapping them.
Facade Pattern
The Facade pattern provides a simple interface to a complex system. It hides the complicated details behind a single, easy-to-use interface. This makes the system easier to use and reduces dependencies.
Facade simplifies complex systems by offering a unified interface.
Real World Analogy

Imagine a universal power adapter that lets you plug your device into different types of sockets, a gift wrapper that decorates a plain box to make it look special, and a hotel concierge who handles all your requests through one person instead of talking to many staff.

Adapter Pattern → Universal power adapter that converts plug shapes to fit sockets
Decorator Pattern → Gift wrapper that adds decoration without changing the box
Facade Pattern → Hotel concierge who simplifies guest requests by managing many services
Diagram
Diagram
┌─────────────┐       ┌─────────────┐       ┌─────────────┐
│ Client      │──────▶│ Adapter     │──────▶│ Adaptee     │
└─────────────┘       └─────────────┘       └─────────────┘

┌─────────────┐       ┌─────────────┐       ┌─────────────┐
│ Client      │──────▶│ Decorator   │──────▶│ Component   │
└─────────────┘       └─────────────┘       └─────────────┘

┌─────────────┐       ┌─────────────┐
│ Client      │──────▶│ Facade      │
└─────────────┘       └─────────────┘
                         │
           ┌─────────────┼─────────────┐
           │             │             │
     ┌─────────┐   ┌─────────┐   ┌─────────┐
     │ Subsys1 │   │ Subsys2 │   │ Subsys3 │
     └─────────┘   └─────────┘   └─────────┘
This diagram shows how Adapter connects incompatible interfaces, Decorator wraps components to add behavior, and Facade simplifies multiple subsystems behind one interface.
Key Facts
Adapter PatternAllows incompatible interfaces to work together by converting one interface to another.
Decorator PatternAdds new responsibilities to objects dynamically without altering their structure.
Facade PatternProvides a simple interface to a complex set of interfaces in a subsystem.
Structural Design PatternsPatterns that ease the design by identifying simple ways to realize relationships between entities.
Common Confusions
Thinking Adapter changes the original object’s interface permanently.
Thinking Adapter changes the original object’s interface permanently. Adapter only translates calls at runtime without modifying the original object’s code or interface.
Believing Decorator modifies the original object directly.
Believing Decorator modifies the original object directly. Decorator wraps the object and adds behavior externally without changing the original object.
Assuming Facade hides the entire system’s functionality.
Assuming Facade hides the entire system’s functionality. Facade simplifies access but does not remove or replace the underlying system components.
Summary
Structural patterns help organize and connect parts of software to work together smoothly.
Adapter translates between incompatible interfaces to enable reuse without changes.
Decorator adds new features to objects dynamically by wrapping them.
Facade offers a simple interface to complex systems, making them easier to use.