Do Android adapters use Adapter Design pattern? The GoF design patterns book describes Adapter Design Pattern as
The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
There's a target interface which the adapter implements and the client uses(expects) and there is an adaptee to which the adapter delegates all the requests made by client.
I understand that its theory and real world pattern adapter interfaces don't exactly look like it, but still I can't figure out what the android adapters adapt(what target interface) and to which adaptee are the requests actually made to.
I have checked this, this and this. But none of them explain clearly how is the android adapter the adapter design pattern. The 1st and 2nd answers, in fact, are somewhat conflicting.
Can anyone please explain this?
Adapter design pattern is one of the structural design pattern and its used so that two unrelated interfaces can work together. The object that joins these unrelated interface is called an Adapter.
Adapter pattern works as a bridge between two incompatible interfaces. This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces.
Example. The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients. Socket wrenches provide an example of the Adapter. A socket attaches to a ratchet, provided that the size of the drive is the same.
If you do some research on the adapter pattern, you will find two different versions of it: The class adapter pattern that implements the adapter using inheritance. The object adapter pattern that uses composition to reference an instance of the wrapped class within the adapter.
No, they aren't. The GoF Adapter is used when you need to convert an interface between two types that are similar but not the same. The most common case is when interfacing between two libraries that were not written with each other in mind. For example you may use a library that returns a Map, but you want to pass that result into a networking library that expects a JSONObject. You could use an Adapter pattern to convert it (this is a little bit of a trivial example, but you get the idea).
An Android Adapter like for a ListView or RecyclerView doesn't do that. Instead it takes the data from a model and puts it into a View. Really its closest equivalent is an MVP Presenter.
There are plenty of classes in the world named similarly to GoF that have nothing to do with those patterns (for example, the word State is very rarely part of a State Machine). Adapter in particular was used for a dozen purposes long before GoF was written.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With