Search code examples
androiddesign-patternsadapterandroid-adapter

Are android adapters an example of Adapter Design pattern?


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?


Solution

  • 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.