CHAPTER 11
Intermediate
Adapter and Facade Patterns
Updated: May 16, 2026
35 min read
# CHAPTER 11
Adapter and Facade Patterns
1. Introduction
We have transitioned from Creational patterns (making objects) to Structural Patterns (assembling objects). In real-world enterprise development, you rarely build an entire system from scratch. You often have to integrate your pristine new code with an ugly, 15-year-old legacy billing system, or a complex third-party API like Stripe or AWS. These external systems have interfaces that are completely incompatible with your application's logic. If you force your clean code to bend to their messy rules, your architecture rots. In this chapter, we will master the Adapter and Facade patterns. We will learn how to build digital translators to bridge incompatible interfaces and construct simple doorways to hide terrifying subsystem complexity.2. Learning Objectives
By the end of this chapter, you will be able to:- Define the intent of Structural Design Patterns.
- Understand how the Adapter pattern acts as a real-world translator.
- Implement an Adapter to safely integrate a third-party API.
- Understand the Facade pattern's role in simplifying complex systems.
- Distinguish between an Adapter (changing interfaces) and a Facade (simplifying interfaces).
3. The Adapter Pattern
The Adapter acts exactly like a real-world travel adapter. If you have an American laptop plug and a European wall socket, they are incompatible. The Adapter sits between them, translating the American pins into European holes.-
The Problem: Your analytics app expects data in
XMLformat. The new stock market API you just purchased only returnsJSON. They cannot communicate.
-
The Solution: You create a
JsonToXmlAdapterclass. Your app talks to the Adapter using standard XML method calls. The Adapter silently translates that request into JSON, fetches the data from the API, translates it back to XML, and returns it. Your app never knows JSON was involved.
4. The Facade Pattern
A Facade is like ordering food at a restaurant. You do not go into the kitchen, chop onions, turn on the stove, and cook the steak. You talk to a Waiter (The Facade). You give a simple command ("I want a steak"), and the Waiter coordinates the massive complexity of the kitchen on your behalf.-
The Problem: To process a video upload, your code has to manually instantiate the
FFmpegEncoder, calculate theBitrateManager, open theAwsS3Connection, and trigger theNotificationService. This is 50 lines of complex, error-prone code scattered throughout your app.
-
The Solution: You create a
VideoUploaderFacadeclass with a single method:uploadVideo(file). The Facade hides the 50 lines of terrifying subsystem logic behind one simple, easy-to-use doorway.
5. Adapter vs. Facade (The Difference)
These patterns look similar but have different intents.- Adapter: Makes an existing interface usable. It takes a *complex/incompatible* interface and changes it into the *specific* interface you need. (Translating).
- Facade: Makes a complex subsystem easy to use. It takes *dozens of complex* classes and provides a *single simplified* interface to control them all. (Simplifying).
6. UML Diagrams
*Adapter Structure*
text
*Facade Structure*
text
7. Code Example: Adapter (PHP)
Let's safely integrate a third-party Payment Gateway without breaking our app.
php
8. Best Practices
- Anti-Corruption Layer (ACL): In enterprise microservices (Domain-Driven Design), the Adapter pattern is heavily used to create an Anti-Corruption Layer. By forcing all external API communication through Adapters, you prevent the changing, messy logic of third-party vendors from "corrupting" the clean, internal logic of your core application.
9. Common Mistakes
-
The God Facade: If you put *every single feature* of your application behind one massive
SystemFacadeclass, it stops being a helpful doorway and becomes a terrifying God Class that violates the Single Responsibility Principle. Facades should be scoped to specific subsystems (e.g.,BillingFacade,SearchFacade).
10. Mini Project: Build an Image Conversion Facade
-
1.
The Subsystem: Imagine you have three complex classes:
FileReader,JpegToPngConverter, andFileWriter.
-
2.
The Facade: Create a simple
ImageConverterFacadeclass.
-
3.
The Method: Give it one method:
convert($filename, $format).
- 4. The Execution: The Facade takes the simple command, opens the FileReader, passes the data to the Converter, and uses the FileWriter to save it. The client code achieves this in exactly one line of code.
11. Practice Exercises
- 1. Define the primary intent of the Adapter pattern. Explain the "Travel Adapter" analogy.
- 2. Compare the structural intent of the Adapter pattern against the Facade pattern. When would an architect choose to use a Facade?
12. MCQs with Answers
Question 1
An engineering team purchases a legacy XML data-processing library. Their modern application strictly uses JSON. The team cannot alter the legacy library's source code. Which design pattern should they deploy to translate the JSON requests into XML so the systems can communicate?
Question 2
Which architectural pattern is specifically designed to provide a simplified, higher-level interface that hides the terrifying complexity of a massive subsystem containing dozens of interdependent classes?
13. Interview Questions
- Q: Explain how the Adapter pattern enforces the SOLID Dependency Inversion Principle when dealing with unstable third-party APIs (like Stripe or Twilio).
- Q: Walk me through the concept of an "Anti-Corruption Layer." How does wrapping external systems in Adapters protect your core business logic from breaking changes implemented by vendors?
- Q: A junior developer creates a massive class containing thousands of lines of code that interacts with every subsystem in the application, and calls it an "ApplicationFacade." Explain why this violates SOLID principles and how you would refactor it.