Usage examples: The Adapter pattern is pretty common in Java code. It’s very often used in systems based on some legacy code. In such cases, Adapters make legacy code work with modern classes.
There are some standard Adapters in Java core libraries:
Identification: Adapter is recognizable by a constructor which takes an instance of a different abstract/interface type. When the adapter receives a call to any of its methods, it translates parameters to the appropriate format and then directs the call to one or several methods of the wrapped object.
Fitting square pegs into round holes
This simple example shows how an Adapter can make incompatible objects work together.
round
round/RoundHole.java: Round holes
packagerefactoring_guru.adapter.example.round;/** * RoundHoles are compatible with RoundPegs. */publicclassRoundHole {privatedouble radius;publicRoundHole(double radius) {this.radius= radius; }publicdoublegetRadius() {return radius; }publicbooleanfits(RoundPeg peg) {boolean result; result = (this.getRadius() >=peg.getRadius());return result; }}
round/RoundPeg.java: Round pegs
packagerefactoring_guru.adapter.example.round;/** * RoundPegs are compatible with RoundHoles. */publicclassRoundPeg {privatedouble radius;publicRoundPeg() {}publicRoundPeg(double radius) {this.radius= radius; }publicdoublegetRadius() {return radius; }}
square
square/SquarePeg.java: Square pegs
packagerefactoring_guru.adapter.example.square;/** * SquarePegs are not compatible with RoundHoles (they were implemented by * previous development team). But we have to integrate them into our program. */publicclassSquarePeg {privatedouble width;publicSquarePeg(double width) {this.width= width; }publicdoublegetWidth() {return width; }publicdoublegetSquare() {double result; result =Math.pow(this.width,2);return result; }}
adapters
adapters/SquarePegAdapter.java: Adapter of square pegs to round holes
packagerefactoring_guru.adapter.example.adapters;importrefactoring_guru.adapter.example.round.RoundPeg;importrefactoring_guru.adapter.example.square.SquarePeg;/** * Adapter allows fitting square pegs into round holes. */publicclassSquarePegAdapterextendsRoundPeg {privateSquarePeg peg;publicSquarePegAdapter(SquarePeg peg) {this.peg= peg; } @OverridepublicdoublegetRadius() {double result;// Calculate a minimum circle radius, which can fit this peg. result = (Math.sqrt(Math.pow((peg.getWidth() /2),2) *2));return result; }}
Demo.java: Client code
packagerefactoring_guru.adapter.example;importrefactoring_guru.adapter.example.adapters.SquarePegAdapter;importrefactoring_guru.adapter.example.round.RoundHole;importrefactoring_guru.adapter.example.round.RoundPeg;importrefactoring_guru.adapter.example.square.SquarePeg;/** * Somewhere in client code... */publicclassDemo {publicstaticvoidmain(String[] args) {// Round fits round, no surprise.RoundHole hole =newRoundHole(5);RoundPeg rpeg =newRoundPeg(5);if (hole.fits(rpeg)) {System.out.println("Round peg r5 fits round hole r5."); }SquarePeg smallSqPeg =newSquarePeg(2);SquarePeg largeSqPeg =newSquarePeg(20);// hole.fits(smallSqPeg); // Won't compile.// Adapter solves the problem.SquarePegAdapter smallSqPegAdapter =newSquarePegAdapter(smallSqPeg);SquarePegAdapter largeSqPegAdapter =newSquarePegAdapter(largeSqPeg);if (hole.fits(smallSqPegAdapter)) {System.out.println("Square peg w2 fits round hole r5."); }if (!hole.fits(largeSqPegAdapter)) {System.out.println("Square peg w20 does not fit into round hole r5."); } }}
OutputDemo.txt: Execution result
Round peg r5 fits round hole r5.
Square peg w2 fits round hole r5.
Square peg w20 does not fit into round hole r5.