5. Adapter mode of design mode

Adapter mode of design mode

table of Contents

Adapter mode of design mode

One, the basic introduction of the adapter mode

Two, class adapter mode

2.1, UML graphics

2.2, example demonstration

2.3. Notes and details of class adapter mode

Three, the object adapter mode

3.1, UML graphics

3.2 Introduction to Object Adapter Mode

3.3, example demonstration

Fourth, the interface adapter mode

4.1, UML graphics

4.2. Introduction to Interface Adapter Mode

4.3, example demonstration

Fifth, the source code analysis of the adapter pattern in the SpringMVC framework application

Sixth, the precautions and details of the adapter mode


One, the basic introduction of the adapter mode

basic introduction

1) The Adapter Pattern converts the interface of a certain class into another interface representation expected by the client. The main purpose is compatibility, so that two classes that cannot work together due to interface mismatch can work together. Its alias is Wrapper

2) The adapter mode is a structural mode

3) Mainly divided into three categories : class adapter mode, object adapter mode, interface adapter mode

working principle

1) Adapter mode: Convert the interface of one class to another interface. Make the classes that are not compatible with the original interface can be compatible

2) From the user's point of view, the matched person cannot be seen, it is decoupled

3) The user invokes the target interface method transformed by the adapter, and the adapter then invokes the relevant interface method of the adapted person

4) When the user receives the feedback result, he feels that he is only interacting with the target interface

Two, class adapter mode

2.1, UML graphics

2.2, example demonstration

 Application example description

To explain the adapter as an example of a charger in life, the charger itself is equivalent to the Adapter, 220V AC is equivalent to src (ie the person being adapted), and our target dst (ie the target) is 5V DC

Adapted class: 220V voltage

//被适配的类
public class Voltage220V {
	//输出220V的电压
	public int output220V() {
		int src = 220;
		System.out.println("电压=" + src + "伏");
		return src;
	}
}

Adapter interface: 5V voltage

//适配接口
public interface IVoltage5V {
	public int output5V();
}

 

Adapter category: inherit 220V voltage and realize 5V voltage interface

//适配器类
public class VoltageAdapter extends Voltage220V implements IVoltage5V {

	@Override
	public int output5V() {
		// TODO Auto-generated method stub
		//获取到220V电压
		int srcV = output220V();
		int dstV = srcV / 44 ; //转成 5v
		return dstV;
	}

}

phone charging:

public class Phone {

	//充电
	public void charging(IVoltage5V iVoltage5V) {
		if(iVoltage5V.output5V() == 5) {
			System.out.println("电压为5V, 可以充电~~");
		} else if (iVoltage5V.output5V() > 5) {
			System.out.println("电压大于5V, 不能充电~~");
		}
	}
}

2.3. Notes and details of class adapter mode

1) Java is a single inheritance mechanism, so it is a disadvantage that the class adapter needs to inherit the src class, because this requires dst to be an interface, which has certain limitations;

2) The methods of the src class will be exposed in the Adapter , which also increases the cost of use.

3) Because it inherits the src class, it can rewrite the methods of the src class according to requirements, which enhances the flexibility of the Adapter.

Three, the object adapter mode

3.1, UML graphics

3.2 Introduction to Object Adapter Mode

1) The basic idea is the same as the adapter mode of the class, except that the Adapter class is modified, instead of inheriting the src class, but holding an instance of the src class to solve the compatibility problem. That is: hold the src class, implement the dst class interface, and complete the adaptation of src->dst

2) According to the " synthetic reuse principle ", try to use association relationships to replace inheritance relationships in the system .

3) Object adapter mode is a commonly used adapter mode

3.3, example demonstration

//适配器类
public class VoltageAdapter  implements IVoltage5V {

	private Voltage220V voltage220V; // 关联关系-聚合
	
	
	//通过构造器,传入一个 Voltage220V 实例
	public VoltageAdapter(Voltage220V voltage220v) {
		
		this.voltage220V = voltage220v;
	}



	@Override
	public int output5V() {
		
		int dst = 0;
		if(null != voltage220V) {
			int src = voltage220V.output220V();//获取220V 电压
			System.out.println("使用对象适配器,进行适配~~");
			dst = src / 44;
			System.out.println("适配完成,输出的电压为=" + dst);
		}
		
		return dst;
		
	}

}

Fourth, the interface adapter mode

4.1, UML graphics

4.2. Introduction to Interface Adapter Mode

1) Some books are called: Default Adapter Pattern or Default Adapter Pattern.

2) When you do not need to implement all the methods provided by the interface, you can first design an abstract class to implement the interface, and provide a default implementation (empty method) for each method in the interface , then the subclasses of the abstract class can selectively Override some methods of the parent class to achieve requirements

3) Suitable for situations where an interface does not want to use all its methods .

4.3, example demonstration

interface:

public interface Interface4 {
	public void m1();
	public void m2();
	public void m3();
	public void m4();
}

 

Abstract class:

public abstract class AbsAdapter implements Interface4 {

	//默认实现
	public void m1() {

	}

	public void m2() {

	}

	public void m3() {

	}

	public void m4() {

	}
}

application:

public class Client {
	public static void main(String[] args) {
		
		AbsAdapter absAdapter = new AbsAdapter() {
			//只需要去覆盖我们 需要使用 接口方法
			@Override
			public void m1() {
				// TODO Auto-generated method stub
				System.out.println("使用了m1的方法");
			}
		};
		
		absAdapter.m1();
	}
}

Fifth, the source code analysis of the adapter pattern in the SpringMVC framework application

The HandlerAdapter in SpringMvc uses the adapter mode

Sixth, the precautions and details of the adapter mode

Notes and details of the adapter mode

1) The three naming methods are named according to how the src is given to the Adapter (in the form of the Adapter).

2) Class adapter: given by class. In Adapter, src is used as a class and inherited from object adapter: given by object. In Adapter, src is given as an object, holding interface adapter: given by interface. In the Adapter, use src as an interface to implement

3) The biggest function of the Adapter mode is to integrate the originally incompatible interfaces to work together. 4) In actual development, the realization is not limited to the three classic forms we explained

Guess you like

Origin blog.csdn.net/qq_45072383/article/details/114037240