Bird
Raised Fist0
Software Engineeringknowledge~6 mins

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

Choose your learning style10 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 Pattern → Allows incompatible interfaces to work together by converting one interface to another.
Decorator Pattern → Adds new responsibilities to objects dynamically without altering their structure.
Facade Pattern → Provides a simple interface to a complex set of interfaces in a subsystem.
Structural Design Patterns → Patterns 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.

Practice

(1/5)
1. Which structural pattern allows incompatible interfaces to work together by converting one interface into another?
easy
A. Decorator
B. Adapter
C. Facade
D. Singleton

Solution

  1. Step 1: Understand the role of Adapter pattern

    The Adapter pattern converts the interface of one class into another interface clients expect, enabling incompatible interfaces to work together.
  2. Step 2: Compare with other patterns

    Decorator adds features, Facade simplifies interfaces, Singleton controls instance creation. Only Adapter changes interfaces for compatibility.
  3. Final Answer:

    Adapter -> Option B
  4. Quick Check:

    Interface conversion = Adapter [OK]
Hint: Adapter = interface converter between incompatible parts [OK]
Common Mistakes:
  • Confusing Decorator with Adapter
  • Thinking Facade changes interfaces
  • Mixing Singleton with structural patterns
2. Which of the following is the correct way to describe the Decorator pattern?
easy
A. It converts one interface to another.
B. It provides a simple interface to a complex system.
C. It adds new responsibilities to objects dynamically.
D. It ensures a class has only one instance.

Solution

  1. Step 1: Identify the purpose of Decorator pattern

    Decorator adds new features or responsibilities to objects dynamically without changing their structure.
  2. Step 2: Eliminate other options

    It provides a simple interface to a complex system. describes Facade, It converts one interface to another. describes Adapter, It ensures a class has only one instance. describes Singleton pattern.
  3. Final Answer:

    It adds new responsibilities to objects dynamically. -> Option C
  4. Quick Check:

    Decorator = add features dynamically [OK]
Hint: Decorator = add features without changing original object [OK]
Common Mistakes:
  • Mixing Decorator with Facade
  • Confusing Adapter with Decorator
  • Thinking Singleton is a structural pattern
3. Consider a system where a Facade pattern is used to simplify multiple complex subsystems. What is the main benefit when a client uses the Facade?
medium
A. The client interacts with a simple interface hiding complexity.
B. The client must adapt its interface to each subsystem.
C. The client can access all subsystem details directly.
D. The client can add new features to subsystems dynamically.

Solution

  1. Step 1: Understand Facade pattern role

    Facade provides a simple unified interface to a set of interfaces in a subsystem, hiding complexity from the client.
  2. Step 2: Analyze options

    The client can access all subsystem details directly. is wrong because Facade hides details. The client must adapt its interface to each subsystem. describes Adapter. The client can add new features to subsystems dynamically. describes Decorator.
  3. Final Answer:

    The client interacts with a simple interface hiding complexity. -> Option A
  4. Quick Check:

    Facade = simple interface to complex system [OK]
Hint: Facade hides complexity behind a simple interface [OK]
Common Mistakes:
  • Thinking Facade exposes all subsystem details
  • Confusing Facade with Adapter or Decorator
  • Assuming Facade adds features dynamically
4. A developer tries to use an Adapter pattern but the client code still fails due to interface mismatch. What is the most likely error?
medium
A. The client code is using the correct interface.
B. The Decorator pattern was used instead of Adapter.
C. The Facade pattern was applied to simplify the interface.
D. The Adapter does not implement the target interface correctly.

Solution

  1. Step 1: Identify Adapter pattern requirement

    Adapter must implement the target interface expected by the client to convert calls properly.
  2. Step 2: Analyze failure cause

    If the client still fails, likely the Adapter does not correctly implement or map the target interface methods.
  3. Final Answer:

    The Adapter does not implement the target interface correctly. -> Option D
  4. Quick Check:

    Adapter interface mismatch = implementation error [OK]
Hint: Adapter must match target interface exactly [OK]
Common Mistakes:
  • Confusing Decorator or Facade with Adapter errors
  • Assuming client code is always correct
  • Ignoring interface method signatures
5. You have a legacy payment system with a complex API. You want to provide a simple interface for new clients and also add logging features without changing the legacy code. Which combination of structural patterns should you use?
hard
A. Facade to simplify interface and Decorator to add logging
B. Decorator to simplify interface and Adapter to add logging
C. Adapter to add logging and Facade to simplify interface
D. Adapter to simplify interface and Decorator to add logging

Solution

  1. Step 1: Identify pattern for simplifying complex API

    Facade provides a simple interface to a complex system, ideal for the legacy payment system.
  2. Step 2: Identify pattern for adding features without changing code

    Decorator adds new responsibilities like logging dynamically without modifying original code.
  3. Step 3: Combine patterns logically

    Use Facade to simplify the interface for clients, then wrap with Decorator to add logging features.
  4. Final Answer:

    Facade to simplify interface and Decorator to add logging -> Option A
  5. Quick Check:

    Simplify = Facade, Add features = Decorator [OK]
Hint: Facade simplifies, Decorator adds features without code change [OK]
Common Mistakes:
  • Using Adapter to simplify interface instead of Facade
  • Confusing roles of Adapter and Decorator
  • Trying to add logging via Adapter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard