Adapter Design Pattern Flashcards

1
Q

What is the Adapter Design Pattern?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When should you use the Adapter Pattern?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What problem does the Adapter Pattern solve?

A

It allows integration of incompatible interfaces without modifying the existing code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are some challenges when integrating a 3rd-party library?

A

Challenges include:
* Inability to modify the library
* Risk of breaking existing code
* Lack of access to the library’s source code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the advantages of the Adapter Pattern?

A

Advantages include:
* Reusability
* Flexibility
* Adherence to Single Responsibility Principle
* Compliance with Open/Closed Principle.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the disadvantages of the Adapter Pattern?

A

Disadvantages include:
* Increased complexity
* Potential performance overhead
* Not a universal solution for all incompatible interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between Adapter, Proxy, and Decorator Patterns?

A

Adapter changes the interface of an existing object, Proxy controls access to an object, and Decorator adds behavior to an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does ‘changing the interface’ mean in programming?

A

It refers to adapting a class’s methods or properties to match what the client code expects, often when dealing with incompatible interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an example of incompatible interfaces?

A

Examples include:
* Client expects a method called print(), but service provides print_document()
* Client expects data in JSON format, but service provides XML format.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the role of the Adapter?

A

The Adapter implements the expected interface, translates requests from the client to the service, and converts responses from the service to the client.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the LegacyPrinter class in the example scenario?

A

A class with an incompatible interface that has a method print_document().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the ModernPrinter class in the example scenario?

A

An interface expected by the client with a method print().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the PrinterAdapter class?

A

An adapter class that implements the ModernPrinter interface and wraps a LegacyPrinter object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the client_code function do?

A

It takes a printer object and calls the print() method on it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

True or False: The Adapter Pattern can add complexity to the system.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Fill in the blank: The Adapter Pattern promotes _______ and flexibility.

A

reusability

17
Q

What are key takeaways regarding the Adapter Pattern?

A

Key takeaways include:
* Useful for integrating incompatible interfaces
* Promotes reusability and flexibility
* Can add complexity
* Python’s dynamic typing simplifies implementation.