结构型模式--适配器模式
2017-11-26 21:15
204 查看
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的匹配程度 这需要考虑两个接口之间的差异程度。
相关文章推荐
- 结构型模式之适配器模式实现
- 设计模式(结构型)之适配器模式(Adapter Pattern)
- Java设计模式之适配器模式Adapter(结构型)
- java设计模式(结构型)之适配器模式
- 设计模式 - 结构型模式 - 适配器模式
- 结构型设计模式(1):适配器模式
- 设计模式(6) 结构型模式和适配器模式(ADAPTER)
- 设计模式笔记(7)---适配器模式(结构型)
- 白话设计模式--结构型模式之 适配器模式(Adapter)
- 23种设计模式(6)_结构型_适配器模式(Adapter Pattern)
- 结构型模式——适配器模式
- 结构型模式—适配器模式
- 结构型模式:适配器模式
- 结构型模式:Adapter 适配器模式
- 设计模式学习(结构型模式)—适配器模式(Adapter)
- 设计模式(七):Adapter 适配器模式 -- 结构型模式
- JAVA设计模式(6) —<结构型>适配器模式(Adapter)
- 设计模式--适配器模式Adapter(结构型)
- (结构型模式)ADAPTER——适配器模式
- 7 结构型模式-----适配器模式