Gang of Four Structural pattern: Adapter
Structural Pattern
Match interfaces of different classes.Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
An Adapter is used to provide a wrapper to a third party class so that it can be called by the Application.
The Application may expect to call a certain method of an interface which is part of its own API. However we wish to call a method of some other third party API.
What we do is we wrap the third party class in an adapter that implements our application API. When we call the application API method it internally calls the third party implementation.
To get the code for this example:
git clone https://github.com/spotadev/gangoffour.git
In src/main/java navigate to this package:
com.javaspeak.designpatterns.go4.structural.adapter
You can run the code from the main method of:
AdapterApplication
For example imagine the Application client code wishes to call the draw() method on a Square. Square is part of the application API.
We create a ShapeAdapter to wrap ShapeImpl which is part of a third party implementation.
When we call draw() on the ShapeAdapter it internally calls drawSquare() on the ShapeImpl class.
We have adapted the share.drawSquare() method so that it is executed when we call our application API square.draw() method.
package com.javaspeak.designpatterns.go4.structural.adapter;
/**
* Text book description:
* <ul>
* Adapter: Match interfaces of different classes. Convert the interface of a class into
* another interface clients expect. Adapter lets classes work together that couldn’t otherwise
* because of incompatible interfaces.
* </ul>
* An Adapter is used to provide a wrapper to a third party class so that it can be called by the
* Application.
* <p>
* The Application may expect to call a certain method of an interface which is part of its own API.
* However we wish to call a method of some other third party API.
* <p>
* What we do is we wrap the third party class in an adapter that implements our application API.
* When we call the application API method it internally calls the third party implementation.
* <p>
* For example imagine the Application client code wishes to call the draw() method on a Square.
* Square is part of the application API.
* <p>
* We create a ShapeAdapter to wrap ShapeImpl which is part of a third party implementation.
* <p>
* When we call draw() on the ShapeAdapter it internally calls drawSquare() on the ShapeImpl class.
* <p>
* We have adapted the share.drawSquare() method so that it is executed when we call our application
* API square.draw() method.
*
* @author John Dickerson - 22 Feb 2020
*/
public class AdapterApplication {
/**
* In this method we create an adapter for ShapeImpl. When we call square.draw() it internally
* calls shape.drawSquare()
*/
public void callClientMethod() {
Square square = new ShapeAdapter( new ShapeImpl() );
square.draw();
}
public static void main( String[] args ) {
AdapterApplication application = new AdapterApplication();
application.callClientMethod();
}
}
package com.javaspeak.designpatterns.go4.structural.adapter;
/**
* This is the interface that the Application (ApplicationAdapter) expects to call.
* <p>
* There is no implementation for Square and we want to use the drawSquare() implementation of
* Shape.
* <p>
* We create an adapter called ShapeAdapter which implements Square so that code in the Application
* does not have to change. Internally the ShapeAdapter makes a call to the drawSquare() method of
* Shape. Shape and ShapeImpl are legacy code.
*
* @author John Dickerson - 24 Feb 2020
*/
public interface Square {
/**
* This draw() method is called by the Application.
*/
public void draw();
}
package com.javaspeak.designpatterns.go4.structural.adapter;
/**
* The interface which we are adapting
*
* @author John Dickerson - 24 Feb 2020
*/
public interface Shape {
public void drawSquare();
}
package com.javaspeak.designpatterns.go4.structural.adapter;
/**
* This class is the adapter. We are providing a wrapper around Shape so that when we call draw()
* internally we are calling shape.drawSquare()
*
* @author John Dickerson - 24 Feb 2020
*
*/
public class ShapeAdapter implements Square {
private Shape shape;
/**
* Constructor that wraps the Shape
*
* @param shape
*/
public ShapeAdapter( Shape shape ) {
this.shape = shape;
}
@Override
public void draw() {
// The draw() method belongs to the Application API. It wraps the third party drawSquare()
// method.
shape.drawSquare();
}
}
package com.javaspeak.designpatterns.go4.structural.adapter;
/**
* This class is the third party class we are adapting.
*
* @author John Dickerson - 24 Feb 2020
*/
public class ShapeImpl implements Shape {
@Override
public void drawSquare() {
StringBuilder sb = new StringBuilder();
sb.append( "xxxx\n" );
sb.append( "x x\n" );
sb.append( "x x\n" );
sb.append( "xxxx\n" );
System.out.println( sb.toString() );
}
}
Back: Gang of Four
Page Author: JD