(Turn) design pattern adapter mode mobile phone charger, for example

[Reproduced reasons: adapter mode is also a very good example]

[Reserved Original: https://blog.csdn.net/lmj623565791/article/details/25833393 ]

Copyright: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
This link: https: //blog.csdn.net/lmj623565791/article/details/25833393
 

Insist, insist ~

Continue to design mode, Nima stop a period of time not to write, and we must insist today - bring adapter mode

The old way, the definition: Convert the interface of a class into another interface clients expect, the interface is not compatible with the adapter so that the original class can cooperate with each other. This definition Fortunately, said the function of the adapter is to turn into one interface to another interface.

I found two pictures may well explain adapter mode:

Very good description of the two maps the role of the adapter Kazakhstan, saying that I was bought a European version of the HTC G17, is also equipped with a plug converter, this converter is to get up plug adapter role. Ha down to explain the dot code, such as the title, cell phone chargers are generally it is about 5V, we heavenly 220V household AC voltage, it is required to charge a mobile phone adapter (buck), there is nothing wrong use of physical terms, sorry.

First, a cell 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");
	}
}

As can be seen, the phone provides a 5V voltage dependent interface:

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

Then we have is 220V AC household:

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

Now we need an adapter, 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 ; 
	} 
	
}

The last test, we rushed to a phone call:

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:
existing class: I offer 220V AC voltage.
Adapters: I quietly adapting the voltage.
Phone (client): I need 5V voltage charge, now is -> 5V

As can be seen, we use an adapter to complete the conversion of the 5V to 220V then provided to mobile phone use, and we use a combination of (OO design principles), original mobile phone, as well as 200V voltage class do not need to change, and the phone (Customer end) and 220V (the fitter) completely decoupled.
Finally draw uml class diagram, facilitate better understanding:

The above is a class diagram of the adapter, the following is our example of a class diagram, Ze Yang, also good. Nothing bad draw a map, or installed software are white. . . .

Finally, congratulations, you have learned a design pattern, adapter pattern. Chan continued to beg, beg message ~

 

Published 449 original articles · won praise 133 · views 240 000 +

Guess you like

Origin blog.csdn.net/u010886217/article/details/103960046