adapter Flashcards

1
Q

the adapter pattern is defined as

A

allowing incompatible classes to work together by converting the interface of one class into another expected by the clients

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

The class diagram of the adapter pattern consists of the following entities

A

Target
Client
Adaptee
Adapter

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

We’ll need an adapter here that can make the HotAirBalloon class work with the IAircraft interface. The adapter in pattern-speak should

A

implement the client interface, which is the IAircraft interface.

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

The adapter implementation would be the following:

A

public class Adapter implements IAircraft {
HotAirBalloon hotAirBalloon;
public Adapter(HotAirBalloon hotAirBalloon) {
this.hotAirBalloon = hotAirBalloon;
}
@Override
public void fly() {
String feulUsed = hotAirBalloon.inflateWithGas();
hotAirBalloon.fly(feulUsed);
}
}

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

The important things to note about the adapter are:

A

The adapter is composed with the Adaptee object, which in our case is the HotAirBalloon object.

The adapter implements the interface the client knows about and consumes. In this case, it is the IAircraft.

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

Note the client is manipulating objects that implement the IAircraft interface. It doesn’t know anything about the HotAirBalloon class and the adapter is responsible for masking the gory details for the client.. Let’s see the client code now

A

public void main() {

    HotAirBalloon hotAirBalloon = new HotAirBalloon();
    Adapter hotAirBalloonAdapter = new Adapter(hotAirBalloon);
    
    hotAirBalloonAdapter.fly();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Using objects for adaptation gains us the usual benefits of object composition,

A

The design becomes flexible and the adapter can stand in place of the adaptee or any of its subclassed-objects.

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

The class adapter works via multiple inheritance, the idea is that

A

the adapter extends both, the interface in use by the client, as well as, the adaptee class. Adaptation works via inheritance instead of composition.

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

In the Java API, one can find as examples of the adapter pattern

A

java.io.InputStreamReader and java.io.OutputStreamWriter

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