Adapters

Java Core
Level 9 , Lesson 1
Available

"Hello, Amigo! Today I will tell you what exactly an «adapter» is. I hope that after learning about this topic you'll have a much better understanding of input/output streams."

Adapters - 1

Imagine your program uses two frameworks written by other programmers/companies. Both frameworks are very good and use OOP principles: abstraction, polymorphism, encapsulation. Together, they almost completely cover what your program needs to do. You're left with a simple task. You need to pass objects created by one framework to the other framework. But both frameworks are completely different and "don't know about each other", i.e. they don't have any classes in common. You need to somehow convert the objects of one framework into objects of the other.

This task can be beautifully solved by applying the «adapter» technique (design pattern):

Java code Description
class MyClass implements Interface2
{
 private Interface1 object;
 MyClass(Interface1 object)
 {
  this.object = object;
 }
// This is where we put the Interface2 methods 
// that call Interface1 methods
}
This reflects the adapter design pattern.

The basic idea is that the MyClass class converts (adapts) one interface to the other.

"Can you give me a more specific example?"

"OK. Let's say that each framework has its own unique "list" interface. They might look something like this:"

Java code Description
interface AlphaList
{
 void add(int value);
 void insert(int index, int value);
 int get(int index);
 void set(int index, int value);
 int count();
 void remove(int index);
}
Code from the first (Alpha) framework

AlphaList is one of the interfaces that allows the framework code to interact with the code that uses the framework.

class AlphaListManager
{
 public static AlphaList createList()
 {
  //some code to create an object
 }
}
AlphaListManager AlphaListManager is a class in the framework. Its createList method creates an AlphaList object
interface BetaList
{
 int getValue(int index);
 void setValue(int index, int value);
 int getSize();
 void setSize(int newSize);
}
class BetaSaveManager
{
 public static void saveList(BetaList list)
 {
  //some code to save a BetaList object
  //to a file on disk
 }
}
Code from the second (Beta) framework.

BetaList is one of the interfaces that allows the framework code to interact with the code that uses the framework.

BetaSaveManager is a class in the framework. Its saveList method saves a BetaList object

class ListAdapter implements BetaList
{
 private AlphaList list;
 ListAdapter(AlphaList list)
 {
  this.list = list;
 }

 int getValue(int index)
 {
  return this.list.get(index);
 }

 void setValue(int index, int value)
 {
  this.list.set(index, value);
 }

 int getSize()
 {
  return this.list.count();
 }

 void setSize(int newSize)
 {
  if (newSize > this.list.count()
  {
   while (this.list.count() < newSize)
  {
   this.list.add(null);
  }
 }
 else if (newSize < this.list.count() {
   while (this.list.count() > newSize)
   {
    list.remove(list.count() - 1);
   }
  }
 }
}
«Adapter» class that converts from the AlphaList interface to the BetaList interface

The ListAdapter class implements the BetaList interface from the second framework.

When someone calls these methods, the class code «forwards» the calls to the list variable, which is an AlphaList from the first framework.

An AlphaList object is passed to the ListAdapter constructor

The setSize method operates according to the following rules: if the size of the list must be increased, add empty (null) items. If the size of the list needs to be reduced, delete items at the end.

public static void main(String[] args)
{
 AlphaList listAlpha = AlphaListManager.createList();
 BetaList listBeta = new ListAdapter(listAlpha);
 BetaSaveManager.saveList(listBeta);
}
An example of how it might be used

"I liked your last example most of all. Very concise and understandable."

Comments (34)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
matemate123 Level 50, Kraków, Poland
27 January 2023
If we'll copy that code in one file in intellij we'll get error in every method in ListAdapter class and information says:

'getValue(int)' in 'ListAdapter' clashes with 'getValue(int)' in 'BetaList'; attempting to assign weaker access privileges ('package-private'); was 'public'
If I got it right, we need more access in inherited ListAdapter, but why? Why to remove this errors we need add public in every method in ListAdapter class when we have default (package private) access - that is the same access modifier in both class -> interface BetaList and inherited it ListAdapter?
Ibrahim Level 41, Sheffield, United Kingdom
30 December 2021
https://www.youtube.com/watch?v=qG286LQM6BU Great explanation in the video above for the adaptor design pattern.
Justin Smith Level 41, Greenfield, USA, United States
8 October 2021
The examples are a bit too abstract. Let me see if I can create a more tangible example and someone can tell me if I'm getting it right? Suppose I have an interface Vehicle, which is implemented by classes Plane, Car, Train, Boat Suppose I have an interface StaysOnGround, which is implemented by classes Car, Train, Goat, and Table. Some of these classes are implemented by both, so I might want to be able to take a Vehicle object and be able to work on it through the StaysOnGround set of methods, so this would be the approach for that? Or am I way off?
Karas Level 1, Tampa, United States
19 September 2021
Implementing the interface of one framework to your own class, then storing an object of another framework into your own class, and then each call being forwarded to the object stored in your class using the methods of the framework you are working with. Usually the same functionallity but the names for the methods might change... thus adapters... with all what I have learned so far, am I right?
Gellert Varga Level 23, Szekesfehervar, Hungary
12 September 2021
I hope I understand correctly: the ListAdapter class will do such a transformation so that we can use ListAdapter objects to make BetaList methods work on an AlphaList object. In other words it is almost like it would convert objects of type AlphaList into objects of type BetaList.
Jurij Thmsn Level 29, Flensburg, Germany
17 April 2021
I don't really get the difference between decorator and adapter pattern here ....
Jurij Thmsn Level 29, Flensburg, Germany
17 April 2021
found this on stackoverflow: Decorator is used when you want to add functionality to an object, but not by extending that object's type. This allows you to do so at runtime. Adapter is used when you have an abstract interface, and you want to map that interface to another object which has similar functional role, but a different interface.
Andrei Level 41
3 February 2021
Funny and nice explanatory video. If you like games, robots and tanks. :D
Mateusz Level 29, Poland
16 September 2020
https://www.geeksforgeeks.org/adapter-pattern/
Blaise Level 20, London, United Kingdom
15 June 2020
I have a very bad feeling about this.
Andrei Level 41
2 February 2021
😂
BlueJavaBanana Level 37
29 May 2020
In simple terms: Adapter Class is a middle man or a translator you give it one language (method call) and it uses another language (method call) to do the equivelent job. e.g. I'm in Rome, I order a drink in English and my friend whose is Italian understands my method call and uses the waiters method calls to get me a refreshing beverage. Note: When I say language I am using this as an example, I'm not talking Java vs Python, etc...
Nikitata Level 22, Ba Sing Se, United States
6 August 2020
So kind of like a translator? Where here your friend is passing on your request to the waiter because she knows both English and Italian. So he/she is the middle-man here
BlueJavaBanana Level 37
6 August 2020
Yes. Pretty much. You know how to say things using one language (or API) and the adapter class knows what you mean and call the equivelent methods in the other language (or API).