Adapter Design Pattern in Java

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.
/* The OLD */
class SquarePeg {
    private double width;

    public SquarePeg(double width) {
        this.width = width;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }
}

/* The NEW */
class RoundHole {
    private final int radius;

    public RoundHole(int radius) {
        this.radius = radius;
        System.out.println("RoundHole: max SquarePeg is " + radius * 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 final SquarePeg squarePeg;

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

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

public class AdapterDemoSquarePeg {
    public static void main( String[] args ) {
        RoundHole roundHole = new RoundHole( 5 );
        SquarePegAdapter squarePegAdapter;
        for (int i = 6; i < 10; i++) {
            squarePegAdapter = new SquarePegAdapter((double)i);
			// The client uses (is coupled to) the new interface
            squarePegAdapter.makeFit(roundHole);
        }
    }
}

Output

RoundHole: max SquarePeg is 7.0710678118654755
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

Code examples