Design Patterns -- Adapter Patterns

adapter mode


Purpose: Convert the interface of a class to another interface that the client wants. The adapter pattern enables classes to work together that would otherwise not work together due to incompatible interfaces.

Hypothetical scenario: In the past, the mobile phone had a memory card, the mobile phone user put the data in the memory card, and the mobile phone could directly read the data in the memory card. But then this kind of mobile phone with memory card was gradually eliminated, but you want to read the data in the memory card, so you bought a computer to read, but the computer can only read the data of the u disk, not directly To read the data in the memory card, the computer needs a card reader to read the data in the memory card indirectly.

The memory card here is the interface that needs to be converted. Our existing things (computer) are not compatible to use this interface (memory card), so we need an adapter (card reader) to let the computer hold the card reader, so that we can Read data from the memory card.

Write code:

U disk and memory card are both external devices, we write an abstract class uniformly:
package adapter.study;

/**
 * disk
 * @author Dumb chick
 */
public abstract class Disk {
    private String data;

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}

package adapter.study;

/**
 * memory card
 * @author Dumb chick
 */
public class MemoryCard extends Disk {
}

package adapter.study;

/**
 * U plate
 * @author Dumb chick
 */
public class Udick extends Disk{
}

Both the card reader and the computer can read the data of external devices, we will write a read external data interface:
package adapter.study;


/**
 * read external data
 * @author Dumb chick
 */
public interface ReadExternalData {
    /**
     * read external data
     * @param disk external medium
     */
    public void read(Disk disk);
}

package adapter.study;

/**
 * card reader
 * @author Dumb chick
 */
public class CardReaderAdapter implements ReadExternalData {
    @Override
    public void read(Disk disk) {
        System.out.println("Read the data of the mobile phone memory card");
    }
}

package adapter.study;

/**
 * @author Dumb chick
 */
public class Computer implements ReadExternalData {


    ReadExternalData readExternalData=new CardReaderAdapter()   ;

    @Override
    public void read(Disk disk) {
        if(disk instanceof Udick){
            System.out.println("computer read U disk data: "+disk.getData());
        }
        else if(disk instanceof MemoryCard){
            readExternalData.read(disk);
        }
        else {
            System.out.println("The computer does not recognize the device");
        }
    }
}

The client uses the computer to read the memory card data:
package adapter.study;

/**
 * @author Dumb chick
 */
public class Client {
    public static void main(String[] args) {
        Disk u=new Udick();
        u.setData("I'm U plate!");
        Disk p=new MemoryCard();
        p.setData("i am memory card");
        ReadExternalData computer=new Computer();
        computer.read(u);
        computer.read(p);
    }
}

Tags: Java Design Pattern

Posted by phpnew on Fri, 13 May 2022 14:20:27 +0300