Java implements 23 design patterns: Adapter Pattern

Keywords: Java less

Classification of 23 design patterns

1, Overview

The Adapter Pattern serves as a bridge between two incompatible interfaces. This type of design pattern belongs to structural pattern, which combines the functions of two independent interfaces.

This pattern involves a single class that is responsible for adding independent or incompatible interface functions. For example, the card reader is used as the adapter between the memory card and the notebook. You insert the memory card into the card reader, and then insert the card reader into the notebook, so that the memory card can be read from the notebook.


  • The client can call the target interface transparently through the adapter.
  • Reusing the existing classes, programmers do not need to modify the original code and reuse the existing adapter classes.
  • By decoupling the target class and the adapter class, the problem of inconsistent interface between the target class and the adapter class is solved.


  • Excessive use of adapters will make the system very messy and difficult to grasp as A whole. For example, it is obvious that the A interface is called, but in fact, it is internally adapted to the B interface implementation. If there are too many such cases in A system, it is no different from A disaster. So if it's not necessary, you can refactor the system directly instead of using the adapter.
  • Since JAVA inherits at most one class, at most one adapter class can be adapted, and the target class must be an abstract class.


When you are motivated to modify the interface of a functioning system, you should consider using the adapter pattern.

2, Implementation

1. Structure diagram

The Adapter pattern consists of the following main roles:

  • Target interface: the interface expected by the current system business. It can be an abstract class or interface.
  • Adapter class: it is the component interface in the existing component library accessed and adapted.
  • Adapter class: it is a converter that converts the adapter interface into the target interface by inheriting or referencing the adapter's object, so that customers can access the adapter according to the format of the target interface.

PS: the class adapter mode has a high degree of coupling, which requires understanding the internal structure of related components in the component library, and relatively less than the object adapter mode;

PS: the UML structure diagram can be referred to. The example implementation is not completed according to the UML diagram, and it can be implemented flexibly;

2. Implementation

package cn.missbe.model.adpater;

 * Copyright (c) 2020.
 * Email:
 * @author lyg  2020/4/29 4:28 PM
 * description:
 * Voltage adapter: 240V in the UK and 220V in China

public class VoltageAdapter {
    public static UKCharger chinaCharger2UKCharger(ChinaCharger chinaCharger) {
        UKCharger ukCharger = new UKCharger();
        ukCharger.voltage = chinaCharger.voltage;
        ukCharger.frequency = chinaCharger.voltage;
        return ukCharger;

    public static ChinaCharger uKCharger2ChinaCharger(UKCharger ukCharger) {
        ChinaCharger chinaCharger = new ChinaCharger();
        chinaCharger.voltage = ukCharger.voltage;
        chinaCharger.frequency = ukCharger.frequency;
        return chinaCharger;
    public static void main(String[] args) {
         * In order to realize the charger in UK can be used in China, it is necessary to change the voltage and frequency
        UKCharger ukCharger = new UKCharger();
        ChinaCharger chinaCharger = VoltageAdapter.uKCharger2ChinaCharger(ukCharger);

         * To make Chinese chargers available in the UK, it is necessary to change the voltage and frequency
        ukCharger = VoltageAdapter.chinaCharger2UKCharger(chinaCharger);

 * British charger standard
class UKCharger {
    String voltage = "240v";
    String frequency = "50HZ";

    public void charger() {
        System.out.println("UKCharger is Charging.");

 * Charger standard in China
class ChinaCharger {
    String voltage = "220v";
    String frequency = "50HZ";

    public void charger() {
        System.out.println("ChinaCharger is Charging.");

Posted by wiley on Thu, 04 Jun 2020 20:00:48 -0700