"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."