结构型模式--适配器模式

1 前言

适配器模式是结构性模式中非常重要的一个模式,使用也比较广泛。因此有必要重点掌握

2 适配器模式简介

适配器模式主要是将一个的类的接口转换为客户希望的另一个类的接口,Adapter模式使得原本接口不兼容的类可以一起协同工作。
适配器又分为两种,类适配器模式,对象适配器模式
有关这二者的区别,请参考UML类图。

定义:将一个的类的接口转换为客户希望的另一个类的接口

UML:
这里写图片描述
这里写图片描述

可以看到,类适配器和对象适配器的区别就在于Adapter这个类到底是继承Adaptee还是引用Adaptee
这二者之间有一些优缺点:
1 类适配器优点时可以直接引用Adaptee中的方法,不需要新引入变量来引用。并且可以重定义Adaptee的行为,因为是其子类。缺点就是无法匹配Adaptee的子类。
2 对象适配器的优点时允许一个Adapter与多个Adaptee匹配。缺点是无法重定义Adaptee的行为。

适用场景:
1 你想使用一个已经存在的类,而它的接口不符合你的需求
2 你想创建一个可以复用的类,该类可以与其他的类协同工作
3 你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配他们的接口,这个时候你可以使用对象适配器模式(应用所有子类的父对象即可)

示例
这里以Android中ListView中ListAdapter mAdapter 为例来说明Adapter设计模式
首先说明,这是一个对象适配器模式,相关的UML图如下:
这里写图片描述
我们以AbsListView中View obtainView(int position, boolean[] outMetadata) 这个接口为例,来分析一下

Adapter中接口定义如下:

    /**
     * Get a View that displays the data at the specified position in the data set. You can either
     * create a View manually or inflate it from an XML layout file. When the View is inflated, the
     * parent View (GridView, ListView...) will apply default layout parameters unless you use
     * {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}
     * to specify a root view and to prevent attachment to the root.
     * 
     * @param position The position of the item within the adapter's data set of the item whose view
     *        we want.
     * @param convertView The old view to reuse, if possible. Note: You should check that this view
     *        is non-null and of an appropriate type before using. If it is not possible to convert
     *        this view to display the correct data, this method can create a new view.
     *        Heterogeneous lists can specify their number of view types, so that this View is
     *        always of the right type (see {@link #getViewTypeCount()} and
     *        {@link #getItemViewType(int)}).
     * @param parent The parent that this view will eventually be attached to
     * @return A View corresponding to the data at the specified position.
     */
    View getView(int position, View convertView, ViewGroup parent);

而AbsListView中接口定义如下:

View obtainView(int position, boolean[] outMetadata) {
        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "obtainView");

        outMetadata[0] = false;

        // Check whether we have a transient state view. Attempt to re-bind the
        // data and discard the view if we fail.
        final View transientView = mRecycler.getTransientStateView(position);
        if (transientView != null) {
            final LayoutParams params = (LayoutParams) transientView.getLayoutParams();

            // If the view type hasn't changed, attempt to re-bind the data.
            if (params.viewType == mAdapter.getItemViewType(position)) {
                final View updatedView = mAdapter.getView(position, transientView, this);

                // If we failed to re-bind the data, scrap the obtained view.
                if (updatedView != transientView) {
                    setItemViewLayoutParams(updatedView, position);
                    mRecycler.addScrapView(updatedView, position);
                }
            }

            outMetadata[0] = true;

            // Finish the temporary detach started in addScrapView().
            transientView.dispatchFinishTemporaryDetach();
            return transientView;
        }

        final View scrapView = mRecycler.getScrapView(position);
        final View child = mAdapter.getView(position, scrapView, this);
        if (scrapView != null) {
            if (child != scrapView) {
                // Failed to re-bind the data, return scrap to the heap.
                mRecycler.addScrapView(scrapView, position);
            } else if (child.isTemporarilyDetached()) {
                outMetadata[0] = true;

                // Finish the temporary detach started in addScrapView().
                child.dispatchFinishTemporaryDetach();
            }
        }

        if (mCacheColorHint != 0) {
            child.setDrawingCacheBackgroundColor(mCacheColorHint);
        }

        if (child.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
            child.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
        }

        setItemViewLayoutParams(child, position);

        if (AccessibilityManager.getInstance(mContext).isEnabled()) {
            if (mAccessibilityDelegate == null) {
                mAccessibilityDelegate = new ListItemAccessibilityDelegate();
            }
            if (child.getAccessibilityDelegate() == null) {
                child.setAccessibilityDelegate(mAccessibilityDelegate);
            }
        }

        Trace.traceEnd(Trace.TRACE_TAG_VIEW);

        return child;
    }

它是将Adapter中的 View getView(int position, View convertView, ViewGroup parent)这个接口与obtainView(int position, boolean[] outMetadata)进行适配,适配的核心代码就是调用了mAdapter.getView(position, transientView, this)。

其他的例子源码这里不分析了,可以参考源码

注意事项:
1 适配器模式的核心是接口转换,需要弄清楚将什么接口转为为另一种接口可以协同工作的接口
2 注意适配器模式与桥接模式,装饰器模式,代理模式的区别
桥接模式着重强调接口部分与实现部分的分离。
装饰器模式着重强调对原有的对象的功能的增强。
代理模式强调的是在不改变原有接口的条件下,为另一个对象定义代理。
3 使用Adapter模式需要考虑以下因素
Adapter的匹配程度 这需要考虑两个接口之间的差异程度。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值