设计模式(java)- 适配器模式

1. 简介

  适配器模式是将两个接口不兼容的类连接在一起,使其可以进行工作,它是一种结构性设计模式。它主要为了满足客户需要的接口,通过中间的适配层来匹配用户所需要的功能。
  例如,我们在维护老的项目的时候,B组重构了自己组内的代码,此时他们也需要修改自己的接口名,但是这些接口A组有调用,那么这个时候我们不能直接要求A组也跟着改调用接口的代码,因为如果将接口名全部修改为B组提供的,那会花费很长时间,且容易出错。这个时候就可以使用适配器模式将两个组的接口适配到一起,从而在不影响A组代码的情况下,重构了本组的代码,并且两组所维护的代码边界也很清晰,也方便各自的维护。
  再如,我们常常遇到这样的情景:一个房间内只有三孔的插口,而我的电源线是两孔的。那么这时我就需要一个三孔的电源适配器,适配我的两孔电源线。此时,这个适配器一端是两孔插口,一端是插向三孔的接口。这个就是适配器模式。
  再如,在开发中,我们会遇到与硬件驱动通信的情况。那么在没有真实的硬件驱动的时候,为了不影响项目开发进度,就需要一个仿真的驱动接口。这样,做驱动层的开发人员和做应用的开发人员可以并行的完成自己的开发内容。当在没有真实驱动的时候,我可以使用我的仿真来进行调试,当有真实驱动的时候我就可以切换到真实驱动接口进行调试。这种接口适配器也属于适配器模式。
  适配器模式分为:类适配器、对象适配器、接口适配器。

2. 类图

  如下,分三种UML类图分别表示适配器模式的三种方式。

2.1 类适配器

在这里插入图片描述

2.2 对象适配器

在这里插入图片描述

2.3 接口适配器

在这里插入图片描述

  从上图我们可以看到,类适配器和对象适配器本质是一样的,只是实现的方式不一样。一个使用继承的方式,一个使用聚合的方式。一般的,我们更多的使用对象适配器。而接口适配器,就适用于例举的第三个例子。

3. 实例

  同样的,使用代码示例来说明这三种适配器模式实现的基本方式。

3.1 类适配器

interface IThreeHoleSocket {
	public void connectThreeHole();
}

interface ITwoHolePowerCord {
	public void connectTwoHole();
}

class EThreeHoleSocket implements IThreeHoleSocket {
	public void connectThreeHole() {
		System.out.println("Successful connection to the three-hole power cord!");
	}
}

class ETwoHolePowerCord implements ITwoHolePowerCord {
	public void connectTwoHole() {
		System.out.println("Successful connection to the two-hole socket!");
	}
}

  如上,分别有一个三孔插座和一个两孔的电源线,那么怎么让两孔的电源线使用三孔的插座呢。这里就用到了一个适配器,将两者联系起来:

class EThreeHoleClassAdapter extends EThreeHoleSocket implements ITwoHolePowerCord {
	public void connectTwoHole() {
		connectThreeHole();
	}
}

  客户代码:

		ITwoHolePowerCord threeHoleClassAdapter = new EThreeHoleClassAdapter();
		threeHoleClassAdapter.connectTwoHole();

  输出:

Successful connection to the three-hole power cord!

3.2 对象适配器

  对象适配器与类适配器不同的时候,它使用聚合的方式,在适配器中存在需要适配的对象的引用,然后通过调用适配对象的方法来适配用户需要的接口。

class EThreeHoleObjectAdapter implements ITwoHolePowerCord {
	private EThreeHoleSocket threeHoleSocket;
	
	public EThreeHoleObjectAdapter(EThreeHoleSocket threeHoleSocket) {
		this.threeHoleSocket = threeHoleSocket;
	}
	
	public void connectTwoHole() {
		threeHoleSocket.connectThreeHole();
	}
}

  客户代码:

		EThreeHoleSocket threeHoleSocket = new EThreeHoleSocket(); 
		ITwoHolePowerCord threeHoleObjectAdapter = new EThreeHoleObjectAdapter(threeHoleSocket);
		threeHoleObjectAdapter.connectTwoHole();

  输出与类适配器一样。可以发现,对象适配器的耦合度是小于类适配器的,所以建议更多的使用对象适配器。

3.3 接口适配器

  根据简介中的第三个例子,有下面的代码示例:

interface IDriver {
	void funcA();
	void funcB();
	void funcC();
	void funcD();
}

abstract class EDriver implements IDriver {
	public void funcA() {}
	public void funcB() {}
	public void funcC() {}
	public void funcD() {}
}

class ESimDriver extends EDriver {
	public void funcB() {
		System.out.println("this is Sim funcB!");
	}
	
	public void funcC() {
		System.out.println("this is Sim funcC!");
	}
}

class ERealDriver extends EDriver {
	
	public void funcA() {
		System.out.println("this is Real funcA!");
	}
	
	public void funcB() {
		System.out.println("this is Real funcB!");
	}
	
	public void funcC() {
		System.out.println("this is Real funcC!");
	}
	
	public void funcD() {
		System.out.println("this is Real funcD!");
	}
}

  如上,有一个驱动接口类IDriver,抽象类EDriver,以及具体的仿真驱动类ESimDriver和真实驱动类ERealDriver 。那么为什么中间要加一个抽象类EDriver呢,这是因为仿真类中可能实现了部分的接口方法,这样使得具体的类更加的灵活选择自己要实现的方法。
  客户代码:

		IDriver simDriver = new ESimDriver();
		simDriver.funcA();
		simDriver.funcB();
		simDriver.funcC();
		simDriver.funcD();
		
		IDriver realDriver = new ERealDriver();
		realDriver.funcA();
		realDriver.funcB();
		realDriver.funcC();
		realDriver.funcD();

  输出:

this is Sim funcB!
this is Sim funcC!
this is Real funcA!
this is Real funcB!
this is Real funcC!
this is Real funcD!

总结

  适配器模式相对也比较简单,但应用的场景比较多,在项目中我们会经常使用到该设计模式。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非正经程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值