Exam 2 - L15 - Adapter Design Pattern Flashcards
What type of design pattern is the Adapter Pattern
A structural design pattern.
What is the purpose of the Adapter Pattern?
To allow objects with incompatible interfaces to collaborate by wrapping one object to adapt its interface to another.
What is another name for the Adapter Pattern?
The Wrapper Pattern.
When might you use an Adapter instead of modifying a class directly?
When you don’t have access to the source code
or
When modifying it would break existing functionality that relies on its current interface.
How does an adapter work internally?
The adapter wraps an object, intercepts method calls, translates or converts data, and forwards the request to the target object in a compatible format.
Can an adapter be bidirectional?
Yes, in some cases, two-way adapters can be created to handle conversions in both directions.
How does the Adapter Pattern follow the Single Responsibility Principle?
It separates the concern of data / interface conversion from the core business logic.
How does the Adapter Pattern follow the Open/Closed Principle?
You can add new adapters for new data formats or libraries without changing the existing client code.
What is the downside of using the Adapter Pattern?
It increases overall code complexity by introducing more interfaces and classes.
What does it mean that the Adapter Pattern is a structural pattern?
Patterns that focus on how classes and objects are composed - - - Helping ensure that components work together despite incompatible interfaces.
What is meant by incompatible interfaces?
Two classes or systems have methods or data formats that don’t match, preventing direct collaboration unless an intermediary (like an adapter) is used.
What is interface conversion?
Changing how one class presents its methods or data so that it matches what another class expects.
What is the role of a wrapper in the Adapter Pattern?
The wrapper (adapter) encapsulates an existing object and translates method calls and data formats to match a new interface.
Why is modifying a third-party library often avoided?
You may not have access to its source code, or changing it could break compatibility with other parts of the system.
How is an adapter created in code?
By implementing a target interface expected by the client and wrapping an instance of the incompatible class, translating method calls and data.
What does a one-way adapter do?
It only translates calls in one direction, from the client to the adaptee.
What is a two-way adapter?
An adapter that can translate calls in both directions, allowing full communication between two incompatible interfaces.
What is the ‘client’ in the Adapter Pattern context?
The class or system that wants to use another class but cannot because of interface mismatch.
What is the ‘adaptee’ in the Adapter Pattern?
The existing class or object whose interface is incompatible with the client.
What principle helps adapters make a system extensible without modifying existing code?
The Open/Closed Principle.
Classes should be open for extension but closed for modification.
What specific benefit does the Adapter Pattern offer to legacy system integration?
It enables modern systems to interact with outdated components by adapting their interfaces instead of rewriting code.
What is a real world analogy for the Adapter Pattern?
A power plug adapter that lets a U.S. plug work in a European socket by translating physical and electrical formats.