This is the 11th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

preface

I’ve written about the implementation of the singleton design pattern before, and recently I’m going to reorganize the common design patterns and how they should be used in different scenarios.

  • The singleton pattern

Today, our topic is adapter mode, and we will try to write all the commonly used ones in our work. If you think it is helpful, I will be more motivated by a “like”.

Adapter pattern definition

The adapter design pattern is one of the structural design patterns that allows two incompatible interfaces to work together. The objects that connect these unrelated interfaces are called Adapters.

scenario

The example of using the most classic adapter mode in the display is the charger of our mobile phone. Our mobile phone is generally charged with 3V or 5V voltage, but the voltage of our ordinary socket is 220V or 250V, and the mobile phone charger is an adapter between the ordinary socket and the mobile phone. The adaptor’s job is to convert the voltage from the socket to the 5V voltage at which the phone can be charged.

Code implementation

In this installment, I’ll use the adapter design pattern to implement the functionality of a mobile phone charger (adapter).

First, create two classes representing voltage Volt and socket Scoket:

public class Volt {

    private int volts;

    public Volt(int v){
        this.volts=v;
    }

    public int getVolts(a) {
        return volts;
    }

    public void setVolts(int volts) {
        this.volts = volts; }}Copy the code

Socket socket class Scoket, our socket default it is 220V:

public class Socket {
    public Volt getVolt(a){
        return new Volt(220); }}Copy the code

Now I want to build a charger that ADAPTS 220V to 5V. First, we will create an adapter interface using these methods.

public interface SocketAdapter {
	public Volt get5Volt(a);
}
Copy the code

When implementing the adapter pattern, there are two approaches, a class adapter and an object adapter, which produce the same result.

The class adapter uses Java to inherit and extend the source interface, in our case the Socket class.

Object adapters use Java composition and contain source objects.

First we use the class adapter to implement:

// The adapter inherits the Socket
public class SocketClassAdapterImpl extends Socket implements SocketAdapter{

	@Override
	public Volt get5Volt(a) {
		Volt v= getVolt();
		return convertVolt(v,5);
	}
	
	private Volt convertVolt(Volt v, int i) {
        if(v.getVolts()==i){
            return v;
        }
		return newVolt(i); }}Copy the code

This approach may be easier to understand by comparing an adapter to a conversion socket.

Object adapter implementation:

public class SocketObjectAdapterImpl implements SocketAdapter{
	// Combine sockets as properties
	private Socket sock = new Socket();
	
	@Override
	public Volt get5Volt(a) {
		Volt v= sock.getVolt();
		return convertVolt(v,5);
	}
	
	private Volt convertVolt(Volt v, int i) {
        if(v.getVolts()==i){
            return v;
        }
		return newVolt(i); }}Copy the code

These two adapters are almost identical in that they both implement the SocketAdapter interface.

The following is a test program implemented using the adapter design pattern.

public class AdapterPatternTest {

	public static void main(String[] args) {
		testClassAdapter();
		testObjectAdapter();
	}

	private static void testObjectAdapter(a) {
		SocketAdapter sockAdapter = new SocketObjectAdapterImpl();
		Volt v5 = sockAdapter.get5Volt();
		System.out.println("v5 volts using Object Adapter="+v3.getVolts());
	}

	private static void testClassAdapter(a) {
		SocketAdapter sockAdapter = new SocketClassAdapterImpl();
		Volt v5 = sockAdapter.get5Volt();
		System.out.println("v5 volts using Object Adapter="+v3.getVolts()); }}Copy the code

Output result:

v5 volts using Class Adapter=5
v5 volts using Object Adapter=5
Copy the code

Adapter pattern class diagram

Adapter pattern in the JDK

There are quite a few adapter patterns in the JDK:

  • java.util.Arrays.asList()
  • java.io.InputStreamReader(InputStream) (returns a Reader)
  • java.io.OutputStreamWriter(OutputStream) (returns a Writer)

Advantages of the adapter pattern

  • You can have any two unrelated classes run together
  • Improved class reuse
  • Increased class transparency
  • Flexibility is good

Of course, adapters also have disadvantages, too much use can make the code very messy, such as A named call to interface A, but internal call to interface B, this situation too much is not easy to manage.

The use of adapters is generally used when extending an existing functioning function, rather than being considered at the beginning of the design.


That’s all for this episode, and giving me a “like” if it’s helpful is my biggest encouragement. I’m Hei. See you next time.