适配器模式及SDK源码中的运用(附:分别面向接口、类、对象的适配器扩展)
来源:互联网 发布:神优化游戏 编辑:程序博客网 时间:2024/06/15 04:42
将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
适用性
1.你想使用一个已经存在的类,而它的接口不符合你的需求。
2.你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类(即那些接口可能不一定兼容的类)协同工作。
3.(仅适用于对象Adapter)你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口。对象适配器可以适配它的父类接口。
参与者
1.Target 定义Client使用的与特定领域相关的接口。
2.Client 与符合Target接口的对象协同。
3.Adaptee 定义一个已经存在的接口,这个接口需要适配。
4.Adapter 对Adaptee的接口与Target接口进行适配
类图
其实现方式主要有两种:
类适配器![](http://img.blog.csdn.net/20151010144333435)
对象适配器![](http://img.blog.csdn.net/20151010144348148)
例子(对象适配器)
Target
public interface Target { void adapteeMethod(); void adapterMethod();}
Adaptee
public class Adaptee { public void adapteeMethod() { System.out.println("Adaptee method!"); }}
Adapter
public class Adapter implemente Target { private Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; }public void adapteeMethod() {adaptee.adapteeMethod();}public void adapterMethod() {System.out.println("Adapter method!"); }}
Client
public class Test { public static void main(String[] args) { Target target = new Adapter(new Adaptee()); target.adapteeMethod(); target.adapterMethod(); }}
result
Adaptee method!
Adapter method!
SDK源码例子
为了简明直接,我省略了相关的其他适配器 ,只以此两个适配器为例。
ListViews做为client,他所需要的目标接口(target interface)就是ListAdapter,包含getCount(),getItem(),getView()等几个基本的方法,为了兼容List<T>,Cursor等数据类型作为数据源,我们专门定义两个适配器来适配他们:ArrayAdapter和 CursorAdapter。这两个适配器,说白了,就是针对目标接口对数据源进行兼容修饰。
这就是适配器模式。其中BaseAdapter实现了如isEmpty()方法,使子类在继承BaseAdapter后不需要再实现此方法,这就是缺省适配器,这也是缺省适配器的一个最明显的好处。
我们以最简单的若干个方法举例如下,ListAdapter接口如下(,为了简单,我省略了继承自Adapter接口):
public interface ListAdapter { public int getCount(); Object getItem(int position); long getItemId(int position); View getView(int position, View convertView, ViewGroup parent); boolean isEmpty();}
抽象类BaseAdapter,我省略其他代码,只列出两个方法,以作示意:
public abstract class BaseAdapter implements ListAdapter, SpinnerAdapter{ // ... ... public View getDropDownView(int position, View convertView, ViewGroup parent) { return getView(position, convertView, parent); } public boolean isEmpty() { return getCount() == 0; }}
ArrayAdapter对List<T>进行封装成ListAdapter的实现,满足ListView的调用:
public class ArrayAdapter<T> extends BaseAdapter implements Filterable { private List<T> mObjects; //我只列出这一个构造函数,大家懂这个意思就行 public ArrayAdapter(Context context, int textViewResourceId, T[] objects) { init(context, textViewResourceId, 0, Arrays.asList(objects)); } private void init(Context context, int resource, int textViewResourceId, List<T> objects) { mContext = context; mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mResource = mDropDownResource = resource; mObjects = objects; //引用对象,也是表达了组合优于继承的意思 mFieldId = textViewResourceId; } public int getCount() { return mObjects.size(); } public T getItem(int position) { return mObjects.get(position); } public View getView(int position, View convertView, ViewGroup parent) { return createViewFromResource(position, convertView, parent, mResource); } // ... ...}
我们就如此成功的把List<T>作为数据源以ListView想要的目标接口的样子传给了ListView,同理CursorAdapter也是一模一样的道理,就不写具体代码了。 适配器本身倒是不难,但是提供了解决不兼容问题的惯用模式。 关于什么时候使用适配器模式,大概有三种情况: (1). 你想使用一个已经存在的类,而它的接口不符合你的需求,这个在处理旧系统时比较常见。
(2). 你想创建一个可以复用的类,该类可以和其他不相关的类或不可预见的累协同工作,这就是我们android开发者经常碰到的情况:我们常常自定义一个新的Adapter。
(3). 你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配他们的接口,对象适配器可以适配他的父类接口。
效果
1. 结构性模式
2. 上面论述的主要是对象适配器,关于类适配器除了实现目标端口外,还要实现你要兼容的源类,这样可以少写几行代码,但是从组合优于继承的角度看,它总则没有那么的干净。
3. 对同一个适配器(即同一个对象)对同样的源进行双向甚至多向的适配,则能使其适用两个甚至多个客户调用。
扩展(三种适配器)
首先,我们来看看类的适配器模式,先看类图:
核心思想就是:有一个Source类,拥有一个方法,待适配,目标接口时Targetable,通过Adapter类,将Source的功能扩展到Targetable里,看代码:
public class Source { public void method1() { System.out.println("this is original method!"); } } public interface Targetable { /* 与原类中的方法相同 */ public void method1(); /* 新类的方法 */ public void method2(); } public class Adapter extends Source implements Targetable { @Override public void method2() { System.out.println("this is the targetable method!"); } }
Adapter类继承Source类,实现Targetable接口,下面是测试类:
public class AdapterTest { public static void main(String[] args) { Targetable target = new Adapter(); target.method1(); target.method2(); } }
输出:
this is original method!
this is the targetable method!
这样Targetable接口的实现类就具有了Source类的功能。
对象的适配器模式
基本思路和类的适配器模式相同,只是将Adapter类作修改,这次不继承Source类,而是持有Source类的实例,以达到解决兼容性的问题。看图:
只需要修改Adapter类的源码即可:
public class Wrapper implements Targetable { private Source source; public Wrapper(Source source){ super(); this.source = source; } @Override public void method2() { System.out.println("this is the targetable method!"); } @Override public void method1() { source.method1(); } }
测试类:
public class AdapterTest { public static void main(String[] args) { Source source = new Source(); Targetable target = new Wrapper(source); target.method1(); target.method2(); } }
输出与第一种一样,只是适配的方法不同而已。
第三种适配器模式是接口的适配器模式,接口的适配器是这样的:有时我们写的一个接口中有多个抽象方法,当我们写该接口的实现类时,必须实现该接口的所有方法,这明显有时比较浪费,因为并不是所有的方法都是我们需要的,有时只需要某一些, 此处为了解决这个问题,我们引入了接口的适配器模式,借助于一个抽象类,该抽象类实现了该接口,实现了所有的方法,而我们不和原始的接口打交道,只和该抽象类取得联系,所以我们写一个类,继承该抽象类,重写我们需要的方法就行。看一下类图:
这个很好理解,在实际开发中,我们也常会遇到这种接口中定义了太多的方法,以致于有时我们在一些实现类中并不是都需要。看代码:
public interface Sourceable { public void method1(); public void method2(); }
抽象类Wrapper2:
public abstract class Wrapper2 implements Sourceable{ public void method1(){} public void method2(){} } public class SourceSub1 extends Wrapper2 { public void method1(){ System.out.println("the sourceable interface's first Sub1!"); } } public class SourceSub2 extends Wrapper2 { public void method2(){ System.out.println("the sourceable interface's second Sub2!"); } } public class WrapperTest { public static void main(String[] args) { Sourceable source1 = new SourceSub1(); Sourceable source2 = new SourceSub2(); source1.method1(); source1.method2(); source2.method1(); source2.method2(); } }
测试输出:
the sourceable interface's first Sub1!
the sourceable interface's second Sub2!
达到了我们的效果!
讲了这么多,总结一下三种适配器模式的应用场景:
类的适配器模式:当希望将一个类转换成满足另一个新接口的类时,可以使用类的适配器模式,创建一个新类,继承原有的类,实现新的接口即可。
对象的适配器模式:当希望将一个对象转换成满足另一个新接口的对象时,可以创建一个Wrapper类,持有原类的一个实例,在Wrapper类的方法中,调用实例的方法就行。
接口的适配器模式:当不希望实现一个接口中所有的方法时,可以创建一个抽象类Wrapper,实现所有方法,我们写别的类的时候,继承抽象类即可。- 适配器模式及SDK源码中的运用(附:分别面向接口、类、对象的适配器扩展)
- [设计模式](五):适配器模式(Adapter)[类适配器、对象适配器、接口适配器]
- 面向类的适配器模式
- 面向对象的设计模式(九),适配器模式
- 适配器(adapter)模式--类适配器和对象适配器
- 适配器模式(Adapter):类适配器、对象适配器
- 适配器模式(Adapter):类适配器、对象适配器
- 适配器模式(Adapter):类适配器、对象适配器
- 适配器模式(Adapter):类适配器、对象适配器
- 适配器模式(Adapter):类适配器、对象适配器
- 适配器模式(Adapter):类适配器、对象适配器
- 适配器模式(Adapter):类适配器、对象适配器
- 适配器模式(Adapter):类适配器、对象适配器
- 适配器模式(Adapter):类适配器、对象适配器
- 适配器模式(Adapter):类适配器、对象适配器
- 适配器模式(Adapter):类适配器、对象适配器
- 适配器模式(Adapter):类适配器、对象适配器
- 适配器模式:类适配器和对象适配器
- VMware NAT做端口映射,win7/win8设置防火墙端口开放及检测开放端口
- mysql上给表增加一个自增长列的脚本
- android布局及其常用属性
- Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒
- ormlite的一对多详解
- 适配器模式及SDK源码中的运用(附:分别面向接口、类、对象的适配器扩展)
- Spring中的<context:annotation-config/>
- JVM之我们需要掌握的
- MyBatis学习(二) - SQL语句映射文件(1)resultMap
- android ndk NEON Support
- effective c++学习笔记
- manual make usb installer for windows in ubuntu15.04
- 关于更改Zend Studio/Eclipse代码风格主题的介绍
- 黑马程序员——Java基础---API(下)