5. Design mode adapter mode takes mobile phone charger as an example

Hold on, hold on~

Continue to design mode, Nima stopped writing for a while and didn’t want to write, we must stick to it~ Today brings adapter mode

The old way, definition: Convert the interface of a class into another interface that the customer expects, and the adapter allows classes with incompatible interfaces to cooperate with each other. This definition is good, saying that the function of the adapter is to convert one interface to another.

I found two pictures to explain the adapter mode well:
Insert picture description here

Insert picture description here

These two pictures are a good illustration of the role of the adapter. Say I bought a European version of HTC G17, and it was equipped with a plug converter. This plug converter is used as an adapter. Let's take a bit of code explanation. For example, mobile phone chargers are generally about 5V. Our home AC voltage is 220V, so mobile phone charging needs an adapter (bucker). Any physical terms are used incorrectly, please forgive me.

The first mobile phone: Mobile.java

package com.zhy.pattern.adapter;
 
public class Mobile
{
	/**
	 * 充电
	 * @param power 
	 */
	public void inputPower(V5Power power)
	{
		int provideV5Power = power.provideV5Power();
		System.out.println("手机(客户端):我需要5V电压充电,现在是-->" + provideV5Power + "V");
	}
}

It can be seen that the mobile phone relies on an interface that provides 5V voltage:

package com.zhy.pattern.adapter;
/**
 * 提供5V电压的一个接口
 * @author zhy
 *
 */
public interface V5Power
{
	public int provideV5Power();
}

Then what we have is 220V household AC:

package com.zhy.pattern.adapter;
 
/**
 * 家用220V交流电
 * @author zhy
 *
 */
public class V220Power
{
	/**
	 * 提供220V电压
	 * @return
	 */
	public int provideV220Power()
	{
		System.out.println("我提供220V交流电压。");
		return 220 ; 
	}
}

Next we need an adapter to complete the role of 220V to 5V:

package com.zhy.pattern.adapter;
 
/**
 * 适配器,把220V电压变成5V
 * @author zhy
 *
 */
public class V5PowerAdapter implements V5Power
{
	/**
	 * 组合的方式
	 */
	private V220Power v220Power ;
	
	public V5PowerAdapter(V220Power v220Power)
	{
		this.v220Power = v220Power ;
	}
 
	@Override
	public int provideV5Power()
	{
		int power = v220Power.provideV220Power() ;
		//power经过各种操作-->5 
		System.out.println("适配器:我悄悄的适配了电压。");
		return 5 ; 
	} 
	
}

In the final test, we charge the phone:

package com.zhy.pattern.adapter;
 
public class Test
{
	public static void main(String[] args)
	{
		Mobile mobile = new Mobile();
		V5Power v5Power = new V5PowerAdapter(new V220Power()) ; 
		mobile.inputPower(v5Power);
	}
}

Output:

现有类:我提供220V交流电压。
适配器:我悄悄的适配了电压。
手机(客户端):我需要5V电压充电,现在是-->5V

It can be seen that we have used an adapter to convert 220V to 5V and then provide it to the mobile phone, and we use the combination (OO design principle), the original mobile phone, and the 200V voltage category do not need to be changed, and the mobile phone (customer Terminal) and 220V (adapted person) completely decoupled.
Finally, draw an uml class diagram for everyone to understand:

Insert picture description here

The above is the class diagram of the adapter, and the following is the class diagram of our example. It's not bad to draw a picture, otherwise the software will be installed for nothing. . . .

Guess you like

Origin blog.csdn.net/GTC_GZ/article/details/108742702