java设计模式适配器模式_Java中的适配器设计模式

java设计模式适配器模式

Adapter design pattern is one of the structural design pattern and its used so that two unrelated interfaces can work together. The object that joins these unrelated interface is called an Adapter.

适配器设计模式是结构设计模式之一 ,它的使用使两个不相关的接口可以一起工作。 加入这些不相关接口的对象称为Adapter

适配器设计模式 (Adapter Design Pattern)

adapter design pattern, adapter design pattern in java, adapter pattern, adapter pattern java example

One of the great real life example of Adapter design pattern is mobile charger. Mobile battery needs 3 volts to charge but the normal socket produces either 120V (US) or 240V (India). So the mobile charger works as an adapter between mobile charging socket and the wall socket.


现实生活中适配器设计模式的一个很好的例子就是移动充电器。 移动电池需要3伏特充电,但普通插座产生的电压为120V(美国)或240V(印度)。 因此,移动充电器可充当移动充电插座和壁式插座之间的适配器。

We will try to implement multi-adapter using adapter design pattern in this tutorial.

在本教程中,我们将尝试使用适配器设计模式来实现多适配器。

So first of all we will have two classes – Volt (to measure volts) and Socket (producing constant volts of 120V).

因此,首先,我们将分为两类- Volt (用于测量电压)和Socket (产生120V的恒定电压)。

package com.journaldev.design.adapter;

public class Volt {

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

	public int getVolts() {
		return volts;
	}

	public void setVolts(int volts) {
		this.volts = volts;
	}
	
}
package com.journaldev.design.adapter;

public class Socket {

	public Volt getVolt(){
		return new Volt(120);
	}
}

Now we want to build an adapter that can produce 3 volts, 12 volts and default 120 volts. So first of all we will create an adapter interface with these methods.

现在我们要构建一个可以产生3伏,12伏和默认120伏的适配器。 因此,首先,我们将使用这些方法创建一个适配器接口。

package com.journaldev.design.adapter;

public interface SocketAdapter {

	public Volt get120Volt();
		
	public Volt get12Volt();
	
	public Volt get3Volt();
}

两路适配器模式 (Two Way Adapter Pattern)

While implementing Adapter pattern, there are two approaches – class adapter and object adapter – however both these approaches produce same result.

在实现适配器模式时,有两种方法-类适配器和对象适配器-但是,这两种方法都会产生相同的结果。

  1. Class Adapter – This form uses java inheritance and extends the source interface, in our case Socket class.

    类适配器 –这种形式使用Java继承并扩展了源接口,在我们的例子中是Socket类。
  2. Object Adapter – This form uses Java Composition and adapter contains the source object.

    对象适配器 –此表单使用Java组合,并且适配器包含源对象。

适配器设计模式–类适配器 (Adapter Design Pattern – Class Adapter)

Here is the class adapter approach implementation of our adapter.

这是我们的适配器的类适配器方法实现。

package com.journaldev.design.adapter;

//Using inheritance for adapter pattern
public class SocketClassAdapterImpl extends Socket implements SocketAdapter{

	@Override
	public Volt get120Volt() {
		return getVolt();
	}

	@Override
	public Volt get12Volt() {
		Volt v= getVolt();
		return convertVolt(v,10);
	}

	@Override
	public Volt get3Volt() {
		Volt v= getVolt();
		return convertVolt(v,40);
	}
	
	private Volt convertVolt(Volt v, int i) {
		return new Volt(v.getVolts()/i);
	}

}

适配器设计模式–对象适配器实现 (Adapter Design Pattern – Object Adapter Implementation)

Here is the Object adapter implementation of our adapter.

这是适配器对象适配器实现。

package com.journaldev.design.adapter;

public class SocketObjectAdapterImpl implements SocketAdapter{

	//Using Composition for adapter pattern
	private Socket sock = new Socket();
	
	@Override
	public Volt get120Volt() {
		return sock.getVolt();
	}

	@Override
	public Volt get12Volt() {
		Volt v= sock.getVolt();
		return convertVolt(v,10);
	}

	@Override
	public Volt get3Volt() {
		Volt v= sock.getVolt();
		return convertVolt(v,40);
	}
	
	private Volt convertVolt(Volt v, int i) {
		return new Volt(v.getVolts()/i);
	}
}

Notice that both the adapter implementations are almost same and they implement the SocketAdapter interface. The adapter interface can also be an abstract class.

请注意,两个适配器实现几乎都是相同的,并且它们都实现了SocketAdapter接口。 适配器接口也可以是抽象类

Here is a test program to consume our adapter design pattern implementation.

这是一个使用我们的适配器设计模式实现的测试程序。

package com.journaldev.design.test;

import com.journaldev.design.adapter.SocketAdapter;
import com.journaldev.design.adapter.SocketClassAdapterImpl;
import com.journaldev.design.adapter.SocketObjectAdapterImpl;
import com.journaldev.design.adapter.Volt;

public class AdapterPatternTest {

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

	private static void testObjectAdapter() {
		SocketAdapter sockAdapter = new SocketObjectAdapterImpl();
		Volt v3 = getVolt(sockAdapter,3);
		Volt v12 = getVolt(sockAdapter,12);
		Volt v120 = getVolt(sockAdapter,120);
		System.out.println("v3 volts using Object Adapter="+v3.getVolts());
		System.out.println("v12 volts using Object Adapter="+v12.getVolts());
		System.out.println("v120 volts using Object Adapter="+v120.getVolts());
	}

	private static void testClassAdapter() {
		SocketAdapter sockAdapter = new SocketClassAdapterImpl();
		Volt v3 = getVolt(sockAdapter,3);
		Volt v12 = getVolt(sockAdapter,12);
		Volt v120 = getVolt(sockAdapter,120);
		System.out.println("v3 volts using Class Adapter="+v3.getVolts());
		System.out.println("v12 volts using Class Adapter="+v12.getVolts());
		System.out.println("v120 volts using Class Adapter="+v120.getVolts());
	}
	
	private static Volt getVolt(SocketAdapter sockAdapter, int i) {
		switch (i){
		case 3: return sockAdapter.get3Volt();
		case 12: return sockAdapter.get12Volt();
		case 120: return sockAdapter.get120Volt();
		default: return sockAdapter.get120Volt();
		}
	}
}

When we run above test program, we get following output.

当我们在测试程序之上运行时,我们得到以下输出。

v3 volts using Class Adapter=3
v12 volts using Class Adapter=12
v120 volts using Class Adapter=120
v3 volts using Object Adapter=3
v12 volts using Object Adapter=12
v120 volts using Object Adapter=120

适配器设计模式类图 (Adapter Design Pattern Class Diagram)

JDK中的适配器设计模式示例 (Adapter Design Pattern Example in JDK)

Some of the adapter design pattern example I could easily find in JDK classes are;

我可以在JDK类中轻松找到的一些适配器设计模式示例是:

  • java.util.Arrays#asList()

    java.util.Arrays#asList()
  • java.io.InputStreamReader(InputStream) (returns a Reader)

    java.io.InputStreamReader(InputStream)(返回一个阅读器)
  • java.io.OutputStreamWriter(OutputStream) (returns a Writer)

    java.io.OutputStreamWriter(OutputStream)(返回一个Writer)

That’s all for adapter design pattern in java.

这就是Java中适配器设计模式的全部内容。

翻译自: https://www.journaldev.com/1487/adapter-design-pattern-java

java设计模式适配器模式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值