Adapter in Java

Adapter in Java

Why read if you can watch?

Watch Adapter's video tutorial
read full article

Another Adapter source code example

  1. Identify the desired interface.
  2. Design a "wrapper" class that can "impedance match" the old to the new.
  3. The adapter/wrapper class "has a" instance of the legacy class.
  4. The adapter/wrapper class "maps" (or delegates) to the legacy object.
  5. The client uses (is coupled to) the new interface.
class SquarePeg {                     /*** The OLD ***/
private double width;
public SquarePeg( double w ) { width = w; }
public double getWidth() { return width; }
public void setWidth( double w ) { width = w; }
}

class RoundHole { /*** The NEW ***/
private int radius;
public RoundHole( int r ) {
radius = r;
System.out.println( "RoundHole: max SquarePeg is " + r * Math.sqrt(2) );
}
public int getRadius() { return radius; }
}

//Design a "wrapper" class that can "impedance match" the old to the new
class SquarePegAdapter {

//The adapter/wrapper class "has a" instance of the legacy class
private SquarePeg sp;

public SquarePegAdapter( double w ) { sp = new SquarePeg( w ); }

//Identify the desired interface
public void makeFit( RoundHole rh ) {
//The adapter/wrapper class delegates to the legacy object
double amount = sp.getWidth() - rh.getRadius() * Math.sqrt(2);
System.out.println( "reducing SquarePeg " + sp.getWidth() + " by "
+ ((amount < 0) ? 0 : amount) + " amount" );
if (amount > 0) {
sp.setWidth( sp.getWidth() - amount );
System.out.println( " width is now " + sp.getWidth() );
} } }

class AdapterDemoSquarePeg {
public static void main( String[] args ) {
RoundHole rh = new RoundHole( 5 );
SquarePegAdapter spa;

for (int i=6; i < 10; i++) {
spa = new SquarePegAdapter( (double) i );
//The client uses (is coupled to) the new interface
spa.makeFit( rh );
} } }

RoundHole: max SquarePeg is 7.0710678118 reducing SquarePeg 6.0 by 0.0 amount reducing SquarePeg 7.0 by 0.0 amount reducing SquarePeg 8.0 by 0.9289321881345245 amount width is now 7.0710678118654755 reducing SquarePeg 9.0 by 1.9289321881345245 amount width is now 7.0710678118654755