Bird
Raised Fist0
Software Engineeringknowledge~10 mins

Structural patterns (Adapter, Decorator, Facade) in Software Engineering - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Structural patterns (Adapter, Decorator, Facade)
Start
↓
Identify need
↓
Adapter
↓
Convert
↓
interface
↓
Use adapted
↓
component
↓
End
This flow shows how to start from a need, choose one of the three structural patterns, and apply it to solve interface, behavior, or complexity problems.
Execution Sample
Software Engineering
class OldPrinter {
  print(text) { console.log(text); }
}

class PrinterAdapter {
  constructor(oldPrinter) { this.oldPrinter = oldPrinter; }
  printText(text) { this.oldPrinter.print(text); }
}
This code adapts an old printer's method to a new interface using the Adapter pattern.
Analysis Table
StepActionObject/Method CalledResultNotes
1Create OldPrinter instanceOldPrinter()OldPrinter object createdOldPrinter has print(text) method
2Create PrinterAdapter with OldPrinterPrinterAdapter(oldPrinter)PrinterAdapter object createdAdapter holds reference to OldPrinter
3Call printText('Hello') on AdapterPrinterAdapter.printText('Hello')Calls oldPrinter.print('Hello')Adapter converts printText to print
4OldPrinter prints 'Hello'OldPrinter.print('Hello')Console outputs: HelloOldPrinter does actual printing
5End of flow--Adaptation successful
💡 Adapter pattern completes when the new interface method calls the old method successfully.
State Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
oldPrinternullOldPrinter instanceOldPrinter instanceOldPrinter instanceOldPrinter instance
adapternullnullPrinterAdapter instance (holds oldPrinter)PrinterAdapter instancePrinterAdapter instance
Key Insights - 3 Insights
Why does the Adapter need to hold a reference to the old object?
Because the Adapter calls the old object's methods internally to provide the new interface, as shown in execution_table step 3.
How does the Decorator add behavior without changing the original object?
The Decorator wraps the original object and adds extra actions before or after delegating calls, similar to how Adapter delegates but adds behavior.
What problem does the Facade solve compared to Adapter and Decorator?
Facade simplifies a complex system by providing a single interface, unlike Adapter which changes interfaces or Decorator which adds behavior.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what method does PrinterAdapter call internally at step 3?
AoldPrinter.print()
BoldPrinter.printText()
CPrinterAdapter.print()
DPrinterAdapter.printText()
💡 Hint
Check the 'Object/Method Called' column at step 3 in execution_table.
At which step does the actual printing to console happen?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where console outputs text in execution_table.
If the Adapter did not hold a reference to OldPrinter, what would happen at step 3?
AIt would still print correctly
BIt would cause an error because oldPrinter is undefined
CIt would print twice
DIt would skip printing
💡 Hint
Refer to variable_tracker to see the importance of adapter holding oldPrinter.
Concept Snapshot
Structural patterns help organize code structure.
Adapter changes an interface to match what client expects.
Decorator adds behavior without changing original object.
Facade provides a simple interface to a complex system.
Use Adapter for compatibility, Decorator for enhancements, Facade for simplicity.
Full Transcript
Structural patterns include Adapter, Decorator, and Facade. Adapter lets one interface work with another by converting calls. Decorator wraps an object to add new behavior without changing it. Facade offers a simple interface to a complex system, hiding details. The example shows Adapter wrapping an old printer to provide a new method. Step-by-step, the adapter calls the old printer's method to print text. Variables track the adapter holding the old printer instance. Key points include why the adapter holds the old object and how each pattern solves different problems. Quiz questions check understanding of method calls, execution steps, and variable roles.

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