Android开发设计模式04

来源:互联网 发布:上帝之眼软件 编辑:程序博客网 时间:2024/06/05 22:38

对于android开发者来说起,适配器模式简直太熟悉不过,有很多应用可以说是天天在直接或者间接的用到适配器模式,比如ListView。
ListView用于显示列表数据,但是作为列表数据集合有很多形式,有Array,有Cursor,我们需要对应的适配器作为桥梁,处理相应的数据(并能形成ListView所需要的视图)。
正是因为定义了这些适配器接口和适配器类,才能使我们的数据简单灵活而又正确的显示到了adapterview的实现类上。
适配器模式,Adapter Pattern,勇敢的去适配,大量的资源可以重用。

1.意图
适配器模式,把一个类的接口变换成客户端所期待的另一种接口,从而使原本不匹配而无法在一起工作的两个,类能够在一起工作。
适配器模式分为类适配器模式和对象适配器模式。
关于类适配器模式,因为java的单继承,如果继承一个类,另外的则只能是接口,需要手动实现相应的方法。
热门词汇:类的适配器模式 对象的适配器模式 缺省适配器模式 源类 目标接口

2.结构图和代码

为了简明直接,我省略了相关的其他适配器 ,只以此两个适配器为例。
ListViews做为client,他所需要的目标接口(target interface)就是ListAdapter,包含getCount(),getItem(),getView()等几个基本的方法,为了兼容List<T>,Cursor等数据类型作为数据源,我们专门定义两个适配器来适配他们:ArrayAdapter和CursorAdapter。这两个适配器,说白了,就是针对目标接口对数据源进行兼容修饰。
这就是适配器模式。
其中BaseAdapter实现了如isEmpty()方法,使子类在继承BaseAdapter后不需要再实现此方法,这就是缺省适配器,这也是缺省适配器的一个最明显的好处。 

我们以最简单的若干个方法举例如下,ListAdapter接口如下(,为了简单,我省略了继承自Adapter接口):

1
2
3
4
5
6
7
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,我省略其他代码,只列出两个方法,以作示意:

1
2
3
4
5
6
7
8
9
public abstract class BaseAdapterimplements 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的调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class ArrayAdapter<T>extends BaseAdapterimplements 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). 你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配他们的接口,对象适配器可以适配他的父类接口。 

3.效果
1. 结构性模式 
2. 上面论述的主要是对象适配器,关于类适配器除了实现目标端口外,还要实现你要兼容的源类,这样可以少写几行代码,但是从组合优于继承的角度看,它总则没有那么的干净。
3. 对同一个适配器(即同一个对象)对同样的源进行双向甚至多向的适配,则能使其适用两个甚至多个客户调用。

---------------------------------

CV一族,应该很容易理解原型模式的原理,复制,粘贴完后看具体情况是否修改,其实这就是原型模式。
从java的角度看,一般使用原型模式有个明显的特点,就是实现cloneable的clone()方法。
原型模式,能快速克隆出一个与已经存在对象类似的另外一个我们想要的新对象。

1.意图
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
热门词汇:克隆 深拷贝 浅拷贝

2.结构图和代码
它的结构图非常简单,我们以Intent为例子:


Intent的clone方法非常简单:

1
2
3
4
@Override
public Object clone() {
    return new Intent(this);
}

返回一个新的Intent对象。
克隆操作分深拷贝和浅拷贝,浅拷贝说白了就是把原对象所有的值和引用直接赋给新对象。深拷贝则不仅把原对象的值赋给新对象,而且会把原对象的引用对象也重新创建一遍再赋给新对象。
我们具体分析一下Intent是浅拷贝还是深拷贝吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public Intent(Intent o) {
    this.mAction = o.mAction;
    this.mData = o.mData;
    this.mType = o.mType;
    this.mPackage = o.mPackage;
    this.mComponent = o.mComponent;
    this.mFlags = o.mFlags;
    //下面几个是引用对象被重新创建了,是深拷贝
    if (o.mCategories != null) {
        this.mCategories = new HashSet<String>(o.mCategories);
    }
    if (o.mExtras != null) {
        this.mExtras = new Bundle(o.mExtras);
    }
    if (o.mSourceBounds != null) {
        this.mSourceBounds = new Rect(o.mSourceBounds);
    }
}

这里我们为什么Intent要重写Object的clone方法,就与深拷贝有关。
其实我们查看Object的clone()方法源码和注释,默认的super.clone()用的就是浅拷贝:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * Creates and returns a copy of this {@code Object}. The default
 * implementation returns a so-called "shallow" copy: It creates a new
 * instance of the same class and then copies the field values (including
 * object references) from this instance to the new instance. A "deep" copy,
 * in contrast, would also recursively clone nested objects. A subclass that
 * needs to implement this kind of cloning should call {@code super.clone()}
 * to create the new instance and then create deep copies of the nested,
 * mutable objects.
 */
protected Object clone() throws CloneNotSupportedException {
    if (!(this instanceof Cloneable)) {
        throw new CloneNotSupportedException("Class doesn't implement Cloneable");
    }
 
    return internalClone((Cloneable)this);
}

这种形式属于简单形式的原型模式,如果需要创建的原型数目不固定,可以创建一个原型管理器,在复制原型对象之前,客户端先在原型管理器中查看
是否存在满足条件的原型对象,如果有,则直接使用,如果没有,克隆一个,这种称作登记形式的原型模式。
适用原型模式可以对客户隐藏产品的具体类,因此减少了客户知道的名字的数目,此外是客户无需改变
原型模式的缺陷是每个原型的子类都必须实现Cloneable接口,这个实现起来有时候比较困难。

3. 效果
(1).创建型模式
(2).运行时刻增加和删除产品
(3).改变只以指定新对象(ctrl+v,然后修改)
(4).改变结构以指定新对象。(类似2,实现不同而已)
(5).减少子类的构造