Questions tagged [adapter]

Use this tag for questions relating to the Adapter design pattern, one of the Gang of Four's structural design patterns. Also consider using the [design-patterns] tag and a programming language tag if applicable.

According to the GoF book (page 139) the purpose of the Adapter design pattern is to,

Convert the interface of a class into another interface clients expect. The adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

There are two different versions of the Adapter design pattern (page 141).

  1. A class adapter uses multiple inheritance to adapt one interface to another. 2. An object adapter relies on object composition.

There are three scenarios where the Adapter design pattern is applicable (page 140).

  1. you want to use an existing class, and its interface does not match the one you need. 2. you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces. 3. (object adapter only) you need to use several existing subclasses, but it's unpractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.

There are different consequences of applying the Adapter design pattern depending on which version is used (page 142).

Class and object adapters have different trade-offs. A class adapter

  • adapts Adaptee to Target by committing to a concrete Adaptee class. As a consequence, a class adapter won't work when we want to adapt a class and all its subclasses.
  • lets Adapter override some of Adaptee's behavior, since Adapter is a subclass of Adaptee.
  • introduces only one object, and no additional pointer indirection is needed to get to the adaptee.

An object adapter

  • lets a single Adapter work with many Adaptees—that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once.
  • makes it harder to override Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself.

For details about the structure and implementation of the Adapter design pattern, see the following online resources.

Note the tag encompasses this pattern as well as the other 22 patterns from the GoF book. Consider using any of these tags in combination, as applicable.

5035 questions
18
votes
4 answers

What is the right way to communicate from a custom View to the Activity in which it resides?

I have a custom View class that extends Spinner. I'm trying to figure out what the correct way to talk to the Activity that it's embedded in is, when the user makes a selection. I see that the OnItemSelected listener gets a reference to the…
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
17
votes
4 answers

Implementing safe duck-typing in C#

After looking at how Go handles interfaces and liking it, I started thinking about how you could achieve similar duck-typing in C# like this: var mallard = new Mallard(); // doesn't implement IDuck but has the right methods IDuck duck =…
Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
17
votes
2 answers

Can boost:algorithm::join() concat a container of floats?

Boost join can be used to concatenate a container of strings optionally separated by a separator string as shown in this example: A good example for boost::algorithm::join My STL skills are weak. I'm wondering if there is any way to use the same…
Bitdiot
  • 1,506
  • 2
  • 16
  • 30
17
votes
3 answers

When getView() in ArrayAdapter is called

When creating a customized adapter for ListView in android, I see that I have to create a class the extends ArrayAdapter class and implements the getView(..) method. All of that is OK, but I want to know the sequence of calling methods and…
Adham
  • 63,550
  • 98
  • 229
  • 344
16
votes
7 answers

Treat Enumeration as Iterator

I have a class that implements the Enumeration interface, but Java's foreach loop requires the Iterator interface. Is there an Enumeration to Iterator Adapter in Java's standard library?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
16
votes
7 answers

A C++ iterator adapter which wraps and hides an inner iterator and converts the iterated type

Having toyed with this I suspect it isn't remotely possible, but I thought I'd ask the experts. I have the following C++ code: class IInterface { virtual void SomeMethod() = 0; }; class Object { IInterface* GetInterface() { ... } }; class…
El Zorko
  • 3,349
  • 2
  • 26
  • 34
16
votes
3 answers

Is it possible to get reference to ListView from Adapter in Android?

Is it possible to get reference to ListView from Adapter in Android without passing it as an argument to constructor?
Taras
  • 2,526
  • 3
  • 33
  • 63
15
votes
4 answers

Android: getView() called twice in custom adapter

I'm setting a custom SimpleCursorAdapter to a ListView. For some reason FriendAdapter's getView() is called twice for every item in the DB. After some investigation (I have no wrap_content in my contact_list.xml), I can still not figure out…
jul
  • 36,404
  • 64
  • 191
  • 318
15
votes
3 answers

kotlin-android-extensions in ViewHolder

class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bindata(text: SingleText){ itemView.title.text = text.title itemView.desc.text = text.desc } } like this code, Kotlin has any cache…
15
votes
2 answers

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…
uptoNoGood
  • 566
  • 5
  • 20
15
votes
1 answer

ListView with Title

Is there a way to insert another View above the List in a ListView? For example, I want a Title Bar (TextView) that sits on top of the List, and scrolls out of view as the List scrolls. I can think of two ways so far of doing this, but both are…
Matt
  • 5,461
  • 7
  • 36
  • 43
15
votes
4 answers

Custom view is missing constructor used by tools for adapter

I got the following warning: Custom view com/example/view/adapter/SomeAdapter is missing constructor used by tools: (Context) or (Context,AttributeSet) or (Context,AttributeSet,int) in my class SomeAdapter which extends some BaseAdapter which…
salcosand
  • 2,022
  • 4
  • 24
  • 27
15
votes
2 answers

Creating an Adapter to a CustomView

I have been trying to look online for any solutions or examples on how to do that, but could not manage to find anything what resembles my problem. I have a LinearLayout where I want to add/remove Views when ArrayList data changes. As far as I…
Arturs Vancans
  • 4,531
  • 14
  • 47
  • 76
15
votes
2 answers

Android: How to make an adapter with stable ids?

I've made my own custom adapter extended from BaseAdapter to show a listview and so on... I want it to support single and multi selection, so it must have stable ids. I've checked with the ADAPTER.hasStableIds() and the result is false. I've…
giorgiline
  • 1,271
  • 4
  • 21
  • 35
14
votes
5 answers

How to know which view inside a specific ListView item that was clicked

I'm having a ListView with my own custom adapter derived from a BaseAdapter. Each item in the ListView has sub items such as ImageView and TextView. How can I know which one of these sub items the user clicked? Is it possible to attach a listener in…
Henrik
  • 1,983
  • 3
  • 28
  • 52