uptoNoGood
uptoNoGood

Reputation: 586

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?

Upvotes: 15

Views: 8738

Answers (2)

Aun
Aun

Reputation: 1923

Android adapters are in fact the same Adapter design pattern as per the GoF. Adapters are used to give a known interface to unknown objects. eg: if we are using any 3rd party libraries, it is recommended to have adapters implemented so that the 3rd party interface is converted to a known interface. Then it becomes easy to replace the 3rp party libraries with just adding a new adapter.

Now, look at the ListView Adapter concept in Android as a whole. 3rd party developers are free to add any data backend and make the list view work if they implement the known interface which is the Android defined adapter kind. I hope that clarifies the design pattern.

Upvotes: 4

Gabe Sechan
Gabe Sechan

Reputation: 93569

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.

Upvotes: 25

Related Questions