Adapter Design Pattern Flashcards
What is the Adapter Design Pattern?
A structural design pattern that allows incompatible interfaces to work together by converting the interface of a class into another interface that a client expects.
When should you use the Adapter Pattern?
When you want to:
* Use an existing class with a mismatched interface
* Create a reusable class that cooperates with incompatible interfaces
* Integrate third-party libraries or legacy code.
What problem does the Adapter Pattern solve?
It allows integration of incompatible interfaces without modifying the existing code.
What are some challenges when integrating a 3rd-party library?
Challenges include:
* Inability to modify the library
* Risk of breaking existing code
* Lack of access to the library’s source code.
What are the advantages of the Adapter Pattern?
Advantages include:
* Reusability
* Flexibility
* Adherence to Single Responsibility Principle
* Compliance with Open/Closed Principle.
What are the disadvantages of the Adapter Pattern?
Disadvantages include:
* Increased complexity
* Potential performance overhead
* Not a universal solution for all incompatible interfaces.
What is the difference between Adapter, Proxy, and Decorator Patterns?
Adapter changes the interface of an existing object, Proxy controls access to an object, and Decorator adds behavior to an object.
What does ‘changing the interface’ mean in programming?
It refers to adapting a class’s methods or properties to match what the client code expects, often when dealing with incompatible interfaces.
What is an example of incompatible interfaces?
Examples include:
* Client expects a method called print()
, but service provides print_document()
* Client expects data in JSON format, but service provides XML format.
What is the role of the Adapter?
The Adapter implements the expected interface, translates requests from the client to the service, and converts responses from the service to the client.
What is the LegacyPrinter class in the example scenario?
A class with an incompatible interface that has a method print_document()
.
What is the ModernPrinter class in the example scenario?
An interface expected by the client with a method print()
.
What is the PrinterAdapter class?
An adapter class that implements the ModernPrinter interface and wraps a LegacyPrinter object.
What does the client_code function do?
It takes a printer object and calls the print()
method on it.
True or False: The Adapter Pattern can add complexity to the system.
True.
Fill in the blank: The Adapter Pattern promotes _______ and flexibility.
reusability
What are key takeaways regarding the Adapter Pattern?
Key takeaways include:
* Useful for integrating incompatible interfaces
* Promotes reusability and flexibility
* Can add complexity
* Python’s dynamic typing simplifies implementation.