Java design pattern adapter pattern (with code examples) learn design patterns once a day

Posted by fernandodonster on Thu, 24 Feb 2022 10:11:26 +0100

Structural patterns can describe two different things - classes and instances of classes (i.e. objects).

1. Adapter Pattern: convert the interface of a class into another interface desired by the customer. The Adapter Pattern allows those classes with incompatible interfaces to work together.

The adapter contains the following three roles.

(1) Target abstract class: the target abstract class defines the interface required by the customer, which can be an abstract class or interface, or a concrete class. In the adapter, because the Java language does not support multiple inheritance, it can only be an interface.

(2) Adapter (adapter class): it can call another interface as a converter to adapt Aadptee and Target. The adapter is the core of the adapter pattern. In the class adapter, it connects the two by inheriting the Target and associating an Adaptee object.

(3) Adaptee (adapter class): the adapter is the role to be adapted. It defines an existing interface that needs to be adapted. The adapter class is generally a specific class that contains the business methods that customers want to use. In some cases, there is even no source code of the adapter class.

/**Typical class adapter pattern*/
public class Adapter extend Adaptee implements Target{
    public void request(){
/**Typical object adapter*/
public class Adapter extends Target{
    private Adaptee adaptee;//Maintain a reference to the adapter object
    public Adapter(Adapter adapter){
        this.adapter = adapter;
    public void request(){
        adaptee.specificRequest();//Forward call

In the actual development, the object adapter is used more.

2. Example of adapter mode: a software company wants to design a new image processing tool, but it has developed the functions of image coloring and clipping before, and now it has designed the functions other than image reproduction. In order to reuse the code, a picture controller interface is designed, and the requirement method is defined here, and then the picture adapter is designed to adapt the original coloring and clipping (adapter) functions.

/**Picture control class, which acts as the target abstract class*/
public abstract class PictureControl {
    public static String TAG = "Adapter";
    public void copy(){
        System.out.println("Copy pictures");
    public abstract void cut();//Cut picture
    public abstract void color();//Picture coloring
/**Tailoring classes to act as adapters*/
public class PictureCut {
    public void alarmCut(){
        System.out.println("Clip the picture");
/**Color class, acting as an adapter*/
public class PictureColor {
    public void alarmColor(){
        System.out.println("Color the picture");
/**Picture adapter, acting as an adapter*/
public class PictureAdapter extends PictureControl {
    private PictureColor color;//Defines the apter PictureColor object
    private PictureCut cut;//Defines the apter PictureCut object
    public PictureAdapter(){
        color = new PictureColor();
        cut = new PictureCut();
    //Sound the siren
    public void cut(){
        cut.alarmCut();//Call the method of the adapter class PoliceSound
    //The alarm light flashes
    public void color(){
        color.alarmColor();//Call the method of the adapter class PoliceLamp

Configuration file config XML, which stores the class name of the adapter class in the configuration file (Note: This is a reflection mechanism. If you directly create an object instance on the client, you don't need the following method.)

<?xml version="1.0"?>

XML.Util tool class

public class XMLUtil{
    //This method is used to extract the class name of a specific class from the XML configuration file and return an instance object
        //Create DOM document object
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc;
        doc = builder.parse(new File("src//designpatterns//adapter//config.xml"));
        //Gets the text node containing the class name
        NodeList nl = doc.getElementsByTagName("className");
        Node classNode = nl.item(0).getFirstChild();
        String cName = classNode.getNodeValue();
        //Generate an instance object from the class name and return it
        Class c = Class.forName(cName);
        Object obj = c.newInstance();
        return obj;
    catch(Exception e){
        return null;

Client test class

public class Client{
    public static void main(String args[]){
        PictureController picture;
        //picture = (CarController)XMLUtil.getBean();// The reflection mechanism is implemented with the above XML and xmlutil tool classes
        picture = new PictureAdapter();

3. Code running results:


Topics: Java Android Design Pattern