14

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?

Community
  • 1
  • 1
uptoNoGood
  • 518
  • 3
  • 19

2 Answers2

22

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.

Gabe Sechan
  • 77,740
  • 9
  • 79
  • 113
  • 1
    Agree with your point . This link also mentions recyclerview adapter as adapter design pattern : https://www.raywenderlich.com/109843/common-design-patterns-for-android – codevscolor Feb 23 '17 at 11:09
  • So how about this answer? https://stackoverflow.com/questions/13495405/is-androids-baseadapter-an-example-of-adapter-pattern – nhoxbypass Jan 03 '18 at 09:32
  • @GabeSechan how do you then explain that list of any data (consider it as a first interface) converts to the list of Views (consider it as a second interface)? doesn't it look like an adapter to you? – Sergey May 26 '21 at 19:44
  • @Sergey No. Because an Adapter pattern isn't about converting data. It's about converting interfaces between disparate systems. Similarly code that converts a string in the format "1234" to an int 1234 isn't an Adapter. Because the core of an Adapter isn't about converting data formats. – Gabe Sechan May 26 '21 at 23:37
  • I'm not talking about converting data, I mentioned converting interfaces. For example RecyclerView can't work directly with our custom interfaces, but it provides it's own interface(adapter) to convert our interface to the interface RecyclerView can work with. – Sergey May 27 '21 at 05:37
4

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.

Aun
  • 1,755
  • 1
  • 15
  • 26
  • I agree with your opinion that we make our own class implement known interface and client use an object of our class, but I think the main purpose is a bit different than GoF adapter pattern. Because GoF adapter pattern is for converting one interface to another interface. But, on the other hand, in android adapter, we don't compose any other interface in the adapter and delegate the function call in general. – Sam Mar 01 '20 at 02:27