您的位置:首页 > 其它

设计模式(二) 三种适配器模式 总结和使用场景

2016-12-09 11:40 323 查看


一 概述

定义:适配器模式将某个类的接口转换成客户端期望的另一个接口表示,主的目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同工作。其别名为包装器(Wrapper)。

属于结构型模式

主要分为三类:类适配器模式、对象的适配器模式、接口的适配器模式。

本文定义:

需要被适配的类、接口、对象(我们有的),简称 src(source) 

最终需要的输出(我们想要的),简称 dst (destination,即Target) 

适配器称之为 Adapter 。

一句话描述适配器模式的感觉: src->Adapter->dst,即src以某种形式(三种形式分别对应三种适配器模式)给到Adapter里,最终转化成了dst。

拿我们Android开发最熟悉的展示列表数据的三大控件:ListView,GridView,RecyclerView的Adapter来说,它们三个控件需要的是View(dst),而我们有的一般是datas(src),所以适配器Adapter就是完成了数据源datas
转化成 ItemView的工作。 

带入src->Adapter->dst中,即datas->Adapter->View.


使用场景:

1 系统需要使用现有的类,而这些类的接口不符合系统的需要。 

2 想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作。 

3 需要一个统一的输出接口,而输入端的类型不可预知。


二 类适配器模式:

一句话描述:Adapter类,通过继承 src类,实现 dst 类接口,完成src->dst的适配。

别的文章都用生活中充电器的例子来讲解适配器,的确,这是个极佳的举例,本文也不能免俗:

充电器本身相当于Adapter,220V交流电相当于src,我们的目dst标是5V直流电。 

我们现有的src类:
/**
* 介绍:src类: 我们有的220V电压
* 作者:zhangxutong
* 邮箱:zhangxutong@imcoming.com
* 时间: 2016/10/18.
*/

public class Voltage220 {
public int output220V() {
int src = 220;
System.out.println("我是" + src + "V");
return src;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14

我们想要的dst接口:
/**
* 介绍:dst接口:客户需要的5V电压
* 作者:zhangxutong
* 邮箱:zhangxutong@imcoming.com
* 时间: 2016/10/18.
*/

public interface Voltage5 {
int output5V();
}
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11

适配器类:
/**
* 介绍:Adapter类:完成220V-5V的转变
* 通过继承src类,实现 dst 类接口,完成src->dst的适配。
* 作者:zhangxutong
* 邮箱:zhangxutong@imcoming.com
* 时间: 2016/10/18.
*/

public class VoltageAdapter extends Voltage220 implements Voltage5 {
@Override
public int output5V() {
int src = output220V();
System.out.println("适配器工作开始适配电压");
int dst = src / 44;
System.out.println("适配完成后输出电压:" + dst);
return dst;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Client类:
/**
* 介绍:Client类:手机 .需要5V电压
* 作者:zhangxutong
* 邮箱:zhangxutong@imcoming.com
* 时间: 2016/10/18.
*/

public class Mobile {
/**
* 充电方法
*
* @param voltage5
*/
public void charging(Voltage5 voltage5) {
if (voltage5.output5V() == 5) {
System.out.println("电压刚刚好5V,开始充电");
} else if (voltage5.output5V() > 5) {
System.out.println("电压超过5V,都闪开 我要变成note7了");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

测试代码:
System.out.println("===============类适配器==============");
Mobile mobile = new Mobile();
mobile.charging(new VoltageAdapter());
1
2
3
1
2
3

输出:
===============类适配器==============
我是220V
适配器工作开始适配电压
适配完成后输出电压:5
电压刚刚好5V,开始充电
1
2
3
4
5
1
2
3
4
5

类图如下: 




小结:

Java这种单继承的机制,所有需要继承的我个人都不太喜欢。 

所以类适配器需要继承src类这一点算是一个缺点, 

因为这要求dst必须是接口,有一定局限性; 

且src类的方法在Adapter中都会暴露出来,也增加了使用的成本。

但同样由于其继承了src类,所以它可以根据需求重写src类的方法,使得Adapter的灵活性增强了。


三 对象适配器模式(常用):

基本思路和类的适配器模式相同,只是将Adapter类作修改,这次不继承src类,而是持有src类的实例,以解决兼容性的问题。 

即:持有 src类,实现 dst 类接口,完成src->dst的适配。 

(根据“合成复用原则”,在系统中尽量使用关联关系来替代继承关系,因此大部分结构型模式都是对象结构型模式。) 

Adapter类如下:
/**
* 介绍:对象适配器模式:
* 持有 src类,实现 dst 类接口,完成src->dst的适配。 。以达到解决**兼容性**的问题。
* 作者:zhangxutong
* 邮箱:zhangxutong@imcoming.com
* 时间: 2016/10/18.
*/

public class VoltageAdapter2 implements Voltage5 {
private Voltage220 mVoltage220;

public VoltageAdapter2(Voltage220 voltage220) {
mVoltage220 = voltage220;
}

@Override
public int output5V() {
int dst = 0;
if (null != mVoltage220) {
int src = mVoltage220.output220V();
System.out.println("对象适配器工作,开始适配电压");
dst = src / 44;
System.out.println("适配完成后输出电压:" + dst);
}
return dst;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

测试代码:
System.out.println("\n===============对象适配器==============");
VoltageAdapter2 voltageAdapter2 = new VoltageAdapter2(new Voltage220());
Mobile mobile2 = new Mobile();
mobile2.charging(voltageAdapter2);
1
2
3
4
1
2
3
4

输出:
===============对象适配器==============
我是220V
对象适配器工作,开始适配电压
适配完成后输出电压:5
电压刚刚好5V,开始充电
1
2
3
4
5
1
2
3
4
5

类图: 




小结:

对象适配器和类适配器其实算是同一种思想,只不过实现方式不同。 

根据合成复用原则,组合大于继承, 

所以它解决了类适配器必须继承src的局限性问题,也不再强求dst必须是接口。 

同样的它使用成本更低,更灵活。

(和装饰者模式初学时可能会弄混,这里要搞清,装饰者是对src的装饰,使用者毫无察觉到src已经被装饰了(使用者用法不变)。 这里对象适配以后,使用者的用法还是变的。 
即,装饰者用法: setSrc->setSrc,对象适配器用法:setSrc->setAdapter.)


四 接口适配器模式

也有文献称之为认适配器模式(Default Adapter Pattern)或缺省适配器模式。 

定义: 

当不需要全部实现接口提供的方法时,可先设计一个抽象类实现接口,并为该接口中每个方法提供一个默认实现(空方法),那么该抽象类的子类可有选择地覆盖父类的某些方法来实现需求,它适用于一个接口不想使用其所有的方法的情况。

我们直接进入大家最喜爱的源码撑腰环节:


源码撑腰环节:

Android中的属性动画
ValueAnimator
类可以通过
addListener(AnimatorListener
listener)
方法添加监听器, 

那么常规写法如下:
ValueAnimator valueAnimator = ValueAnimator.ofInt(0,100);
valueAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {

}

@Override
public void onAnimationEnd(Animator animation) {

}

@Override
public void onAnimationCancel(Animator animation) {

}

@Override
public void onAnimationRepeat(Animator animation) {

}
});
valueAnimator.start();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

有时候我们不想实现
Animator.AnimatorListener
接口的全部方法,我们只想监听
onAnimationStart
,我们会如下写:
ValueAnimator valueAnimator = ValueAnimator.ofInt(0,100);
valueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
//xxxx具体实现
}
});
valueAnimator.start();
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8

显然,这个
AnimatorListenerAdapter
类,就是一个接口适配器。 

查看该Adapter类源码:
public abstract class AnimatorListenerAdapter implements Animator.AnimatorListener,
Animator.AnimatorPauseListener {
@Override
public void onAnimationCancel(Animator animation) {
}

@Override
public void onAnimationEnd(Animator animation) {
}

@Override
public void onAnimationRepeat(Animator animation) {
}

@Override
public void onAnimationStart(Animator animation) {
}

@Override
public void onAnimationPause(Animator animation) {
}

@Override
public void onAnimationResume(Animator animation) {
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

可见,它空实现了
Animator.AnimatorListener
类(src)的所有方法. 

对应的src类:
public static interface AnimatorListener {
void onAnimationStart(Animator animation);

void onAnimationEnd(Animator animation);

void onAnimationCancel(Animator animation);

void onAnimationRepeat(Animator animation);
}
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9

类图:



我们程序里的匿名内部类就是Listener1 2 这种具体实现类。
new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
//xxxx具体实现
}
}
1
2
3
4
5
6
1
2
3
4
5
6

接口适配器模式很好理解,令我们的程序更加简洁明了。


五 总结

我个人理解,三种命名方式,是根据 src是以怎样的形式给到Adapter(在Adapter里的形式)来命名的。 

类适配器,以类给到,在Adapter里,就是将src当做类,继承, 

对象适配器,以对象给到,在Adapter里,将src作为一个对象,持有。 

接口适配器,以接口给到,在Adapter里,将src作为一个接口,实现。

Adapter模式最大的作用还是将原本不兼容的接口融合在一起工作。 

但是在实际开发中,实现起来不拘泥于本文介绍的三种经典形式, 

例如Android中ListView、GridView的适配器Adapter,就不是以上三种经典形式之一, 

我个人理解其属于对象适配器模式,一般日常使用中,我们都是在Adapter里持有datas,然后通过getView()/onCreateViewHolder()方法向ListView/RecyclerView提供View/ViewHolder。

Client是Lv Gv Rv ,它们是显示View的类。 

所以dst(Target)是View。 

一般来说我们有的src是数据datas, 

即,我们希望:datas(src)->Adapter->View(dst)->Rv(Client)。

文中源码传送门: 
https://github.com/mcxtzhang/Demos/tree/master/libadapterpattern

转载请标明出处: 

http://blog.csdn.net/zxt0601/article/details/52848004

本文出自:【张旭童的博客】
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: