"Adapter Pattern" Design Pattern

What is the adapter pattern

The adapter pattern is a structural design pattern that allows an existing class or interface to be converted into another interface that is required, so that the originally incompatible interfaces can work together. Adapter mode is usually used to solve interface incompatibility between old and new systems, and can also be used to encapsulate some complex logic.

Why use the adapter pattern

Adapter classes are used when you want to use a class, but its interface is not compatible with other code.
If you need to reuse such classes, they are in the same inheritance system, and they have some additional common methods, but these common methods are not common to all subclasses in this inheritance system.
Sometimes we need to use some existing code or class library, but they do not meet our needs, then we can use the adapter pattern to convert them into the interface we need. Adapter mode can help us quickly realize functional requirements without changing existing code.

Where do you use it at work

The adapter pattern is often used in existing programs to allow incompatible classes to cooperate well.
Adapters can modify the interface of existing objects.
Adapters can provide different interfaces for encapsulated objects
In Android development, the adapter pattern is very common. For example, ListView is a commonly used list control, which needs an Adapter (adapter) to load and display data. We can define an adapter to handle data in different formats, and then convert it to the format required by ListView. In addition, on different versions of the Android system, the naming rules of layout files will also be different, we can use the adapter mode to deal with this compatibility issue.

Design ideas

The adapter pattern mainly consists of three roles:

  • Target interface (Target): The interface required by the client, which defines the request required by the client.
  • Source interface (Adaptee): The interface that needs to be adapted, which defines the existing interface, but is not compatible with the interface required by the client.
  • Adapter: A class that converts a source interface to a target interface, implements the target interface, and holds an instance of the source interface.

The core idea of ​​the adapter pattern is to hold an instance of the source interface in the adapter and convert the method of the source interface to the method of the target interface. This conversion can be achieved through inheritance or aggregation.

Code

Now we need to define a 220V to 5V interface:

    interface Adapter {
    
    //适配器类
        int convert_5v();//装换成5V
    }

Adapted roles are generally existing classes that need to be adapted to new interfaces. 220V power supply is everywhere in life:

    public class Electric {
    
    // 电源
        public int output_220v() {
    
    //输出220V
            return 220;
        }
    }

We need a specific adapter, which is a transformer, which can convert 220V to 5V output:
the first method is the object adapter (the more commonly used one)

     public class PhoneAdapter1 implements Adapter {
    
    //手机适配器类
        private Electric mElectric;//适配器持有源目标对象

        public PhoneAdapter(Electric electric) {
    
    //通过构造方法传入对象
            mElectric = electric;
        }

        @Override
        public int convert_5v() {
    
    
            System.out.println("适配器一开始工作:");
            System.out.println("输入电压:" + mElectric.output_220v());
            System.out.println("输出电压:" + 5);
            return 5;
        }
    }

The second way class adapter (implemented by inheritance)

public class PhoneAdapter2 extends Electric implements Adapter {
    
    
	@Override
    public int convert_5v() {
    
    
    	int param = output_220v();
    	return suit(param);
    }
	
	/** 
     * 转换操作 
     * 当然你可以在转换中做很多事情(这里只是简单的说明)
     */ 
    private int suit(int param) {
    
     
  		int output = param / 44; 
  		System.out.println("适配器二开始工作:");
        System.out.println("输入电压:" + mElectric.output_220v());
        System.out.println("输出电压:" + output);
        return output; 
    } 
}

test class

 public void test() {
    
    
    Electric electric = new Electric();
    System.out.println("默认电压:" + electric.output_220v());

    Adapter phoneAdapter1 = new PhoneAdapter1(electric);//传递一个对象给适配器
    System.out.println("适配转换后的电压1:" + phoneAdapter.convert_5v());
    Adapter phoneAdapter2 = new PhoneAdapter2();
    System.out.println("适配转换后的电压2:" + phoneAdapter2.convert_5v());

}

output result

默认电压:220
适配器一开始工作:
输入电压:220
输出电压:5
适配转换后的电压:5

适配器二开始工作:
输入电压:220
输出电压:5
适配转换后的电压:5

Summarize

The adapter pattern is a commonly used design pattern, which can help us quickly realize functional requirements without changing the existing code. In Android development, the adapter pattern is very common, especially when dealing with controls such as lists. The core idea of ​​the adapter pattern is to hold an instance of the source interface in the adapter and convert the method of the source interface to the method of the target interface.

Guess you like

Origin blog.csdn.net/weixin_45112340/article/details/129703586