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
22
votes
2 answers

Create library to override operator*() of iterator - risk dangling pointer

I am trying to create my own boost::adaptors::transformed. Here is the related boost code. Here is its usage (modified from a SO answer by LogicStuff):- C funcPointer(B& b){ //"funcPointer" is function convert from "B" to "C" …
javaLover
  • 6,347
  • 2
  • 22
  • 67
22
votes
9 answers

Design pattern for default implementation with empty methods

Is there a specific design pattern that describes the scenario where a non-abstract default implementation is provided that implements all or some of the methods on the interface with empty, NO-OP implementations. This being done with the intent of…
22
votes
3 answers

Bridge vs. Adapter Design Pattern

I was questioned by a colleague about the design pattern of my implementation of a WCF windows service in a ASP.net client application and I really could not tell whether it is Bridge or Adapter! Here is the implementation: I have obtained the…
Kamyar Nazeri
  • 25,786
  • 15
  • 50
  • 87
22
votes
5 answers

What is the difference between listeners and adapters?

I'm trying to differentiate between listeners and adapters. Are they pretty much the same but in listeners you have to implement all the methods in the interface, but with adapters you have an option to only implement the methods you need so the…
orange
  • 5,297
  • 12
  • 50
  • 71
21
votes
1 answer

Is a mapper a version of the adapter pattern

I have been trying to get my head around the adapter pattern, out of all the patterns it's the hardest for me to imagine a use for it. I think I understand what is achieved, to me it is to convert an object to another interface so a client can use…
berimbolo
  • 3,319
  • 8
  • 43
  • 78
20
votes
6 answers

RecyclerView Items are not changing

I have a RecyclerView in a fragment which is repeating in TabLayout. I am having the problem of unchanged view in RecyclerView. I have a spinner on each tab. I want to change the data when spinner items get selected. My cases: when switching…
20
votes
4 answers

Make last Item / ViewHolder in RecyclerView fill rest of the screen or have a min height

I'm struggeling with the RecyclerView. I use a recycler view to display the details of my model class. //My model class MyModel { String name; Double latitude; Double longitude; Boolean isOnline; ... } Since some of the values…
xxtesaxx
  • 6,175
  • 2
  • 31
  • 50
20
votes
10 answers

Cannot add header view to list -- setAdapter has already been called

I have one edittext field and one "search" button. When I click on search, I have to display a list view with data corresponding to the values entered in the edittext. I have added a header to my list using addHeader(). When I do search first time,…
user2740599
  • 449
  • 4
  • 10
  • 20
20
votes
2 answers

How to get items currently displayed in AdapterView?

As in title: I would like to know how to get list (array) of all currently displayed items in my AdapterView. Why? Objects that are displayed in my AdapterView require releasing a listener after user closes AdapterView. I need to do it in order to…
Chris Miemiec
  • 755
  • 1
  • 9
  • 19
19
votes
5 answers

Design pattern that Wrapper Classes use in Java?

I have found an old post which does not clarify my understanding about the design patterns that are used by Wrapper Classes, Moreover, on reading from Wikipedia I'm not getting any clear information. Does a Wrapper Class really use any design…
zack
  • 223
  • 1
  • 2
  • 8
19
votes
1 answer

Android ArrayAdapter.Add method not working

The ArrayAdapter.add() method is not working for me. I am using Eclipse Helios 3.6 with ADT Plugin, Target Source is a Froyo 2.2 emulator and 2.2 HTC Evo 4g. Here is my java class import android.app.Activity; import android.os.Bundle; …
Mike
  • 245
  • 1
  • 3
  • 9
19
votes
4 answers

What is the exact difference between 'Adapter' and 'Mediator" patterns?

I know that Adapter is a structural pattern and Mediator is a behavioral one. But as far I understood, what both of them are doing, is connecting two (or more) other classes which are potentially incompatible (not so maintainable) for direct…
18
votes
3 answers

Difference between Strategy Pattern and Adapter

Why is the strategy design pattern and the adapter related to each other? It seams to me that the adapter manipulates the result of some method to fullfill the input needs of anotherone. Wheras the strategy precibes behaviour.
Thomas Zenglein
  • 193
  • 1
  • 7
18
votes
4 answers

iterator adapter to iterate just the values in a map?

I'm just getting back into C++ after a couple of years of doing a lot of C#, and recently Objective C. One thing I've done before is to roll my own iterator adapter for std::map that will deref to just the value part, rather than the key-value pair.…
philsquared
  • 22,403
  • 12
  • 69
  • 98
18
votes
6 answers

android viewpager change adapter

I'm developing an app with a ViewPager for lateral scrolling tables, sometimes i need to change the adapter to load a different set of tables. I've tried to do the following: mViewPager.setAdapter(new…
Ste
  • 2,116
  • 7
  • 24
  • 24