
Java programmer expert
Today we will take a look at the first Java design pattern from the structural pattern category – Adapter. Design patterns in this category deal with class structure such as inheritance and composition.
Read more about other design patterns:
Adapter is a design pattern that allows communication between two existing classes or interfaces that cannot communicate with each other due to incompatibility of their interfaces. The adapter allows these incompatible parts to work together by providing a mediated interface.
So the Adapter solves the problem of communication between incompatible parts of components by creating an intermediate layer between them and passing calls between interfaces. Adapter is useful when we need to integrate existing code or classes with new code that expects a different interface. Instead of changing the code of the existing classes, we can create an adapter that mediates the communication.
This design pattern can also be found in Java when working with so-called wrapper classes. Wrapper classes create object types from primitive data types by wrapping them in an adapter, which then implements the desired object type properties. The effect of this is that we can work with an instance of the Integer class in much the same way as we would work with an ordinary variable of type int.
Now we will show you how to implement the Adapter pattern in Java.
We will use the Shape interface to calculate the area of any shape.
Shape.java
package designpatterns;
public interface Tvar {
public int vypocitajObsah(int x);
}
This interface is also used by the implementation of the Square class.
Square.java
package designpatterns;
// Aktuálna implementácia rozhrania Tvar
public class Stvorec implements Tvar {
@Override
public int vypocitajObsah(int x) {
return x * x;
}
}
We would also like to use an external implementation to calculate the content of the circle provided by a third party, but this does not implement our Shape interface, which we cannot modify because it is already in use.
Circle.java
package designpatterns;
// Implementácia triedy Kruh dodaná externe cez knižnicu
public class Kruh {
public double vypocitajObsahKruhu(int r) {
return 3.14 * r * r;
}
}
The solution is to adapt the Circle implementation to our Shape interface. So we need an adapter, as the methods are incompatible.
CircleAdapter.java
package designpatterns;
public class KruhAdapter extends Kruh implements Tvar {
@Override
public int vypocitajObsah(int x) {
return (int) vypocitajObsahKruhu(x);
}
}
Main.java
Now we can use both the Square and Circle shapes (via the CircleAdapter) and call the area method on them.
import designpatterns.KruhAdapter;
import designpatterns.Stvorec;
import designpatterns.Tvar;
// Použitie vzoru adapter
public class Main {
public static void main(String[] args) {
Tvar kruh = new KruhAdapter();
System.out.println("Obsah kruhu: " + kruh.vypocitajObsah(4));
Tvar stvorec = new Stvorec();
System.out.println("Obsah štvorca: " + stvorec.vypocitajObsah(4));
}
}
The output of this example is:
The Adapter design pattern is used in a situation where a class needs to have a different interface to the one it currently has. The Adapter allows classes to work together that would otherwise not work together due to their different interfaces. The Adapter can be divided into a class adapter and an object adapter, depending on the implementation used.
We have prepared the files with the above example as code that you can run directly in Java. You can download the Java Adapter code here.