android设计模式-责任链模式与View Touch Event分发
来源:互联网 发布:uva 知乎 编辑:程序博客网 时间:2024/06/06 03:33
背景与问题
差旅费申请场景
组员申请1w->组长(0~3k)->主管(0~5k)->经理(0~8k)->老板(0~2w)
组员只与组长具备关联,并不关心具体处理者是谁。
在开发中,许多对象都可能接受到请求,如何避免请求者和处理者耦合在一起?
解决方案
责任链模式
- 定义
多个对象都能接受请求,并有机会处理请求,避免请求发送者与接受者的耦合关系。 - 使用场景
多个对象可以接受同一请求,具体处理请求的对象在运行时动态决定。
在请求的处理对象不明确时,向多个对象中的一个提交该请求。 UML类图
- Handler:抽象处理者角色,声明一个请求处理的方法,并保持一个对下一个处理节点Handler对象的引用。
- ConcreteHandler:具体处理者,对请求进行处理,不能处理则将请求转发。
实现示例
代码比较简单,感兴趣的见最后github应用示例分析
Android系统对Touch事件的分发- View中负责处理touch事件的主要为
dispatchTouchEvent()
、onTouchEvent()
两个方法以及OnTouchListener
接口
- View中负责处理touch事件的主要为
/** * Pass the touch screen motion event down to the target view, or this * view if it is the target. * * @param event The motion event to be dispatched. * @return True if the event was handled by the view, false otherwise. */ public boolean dispatchTouchEvent(MotionEvent event) { ... boolean result = false; ... ListenerInfo li = mListenerInfo; if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED && li.mOnTouchListener.onTouch(this, event)) { result = true; } if (!result && onTouchEvent(event)) { result = true; } } ... return result; } /** * Interface definition for a callback to be invoked when a touch event is * dispatched to this view. The callback will be invoked before the touch * event is given to the view. */ public interface OnTouchListener { /** * Called when a touch event is dispatched to a view. This allows listeners to * get a chance to respond before the target view. * * @param v The view the touch event has been dispatched to. * @param event The MotionEvent object containing full information about * the event. * @return True if the listener has consumed the event, false otherwise. */ boolean onTouch(View v, MotionEvent event); } /** * Register a callback to be invoked when a touch event is sent to this view. * @param l the touch listener to attach to this view */ public void setOnTouchListener(OnTouchListener l) { getListenerInfo().mOnTouchListener = l; } /** * Implement this method to handle touch screen motion events. ... * </ul> * * @param event The motion event. * @return True if the event was handled, false otherwise. */ public boolean onTouchEvent(MotionEvent event) {
因此,view中touch事件的分发流程如下:
- ViewGroup继承于View,负责处理touch事件的除了dispatchTouchEvent()
、onTouchEvent()
两个方法外,还包括onInterceptTouchEvent()
/** * Implement this method to intercept all touch screen motion events. This * allows you to watch events as they are dispatched to your children, and * take ownership of the current gesture at any point. ... * @param ev The motion event being dispatched down the hierarchy. * @return Return true to steal motion events from the children and have * them dispatched to this ViewGroup through onTouchEvent(). * The current target will receive an ACTION_CANCEL event, and no further * messages will be delivered here. */ public boolean onInterceptTouchEvent(MotionEvent ev) {
因此,ViewGroup中touch事件的分发流程如下:
6.总结
- 优点:请求者和处理者解耦。
- 缺点:遍历,影响性能。
参考文献:
- android源码设计模式解析与实战
- http://www.cnblogs.com/linjzong/p/4191891.html
- http://blog.csdn.net/lmj623565791/article/details/39102591/
demo源码:
https://github.com/rLight121/android-design-pattern
阅读全文
0 0
- android设计模式-责任链模式与View Touch Event分发
- 设计模式--[5]责任链模式和Android事件分发
- 设计模式:view.post() 责任链模式
- Java与设计模式-责任链模式
- Android设计模式-责任链模式
- Android设计模式之责任链模式
- Android设计模式应用-责任链模式
- Android设计模式之责任链模式
- Android 设计模式 笔记 - 责任链模式
- Android设计模式~~~责任链模式
- Android设计模式-责任链模式
- Android设计模式实战---责任链模式
- Android的设计模式-责任链模式
- 责任链设计模式
- 责任链设计模式
- 设计模式---责任链
- 责任链设计模式
- 设计模式----责任链
- C语言程序开发宝典-数据结构
- HashCode和equals方法重写
- 数据库系统概念(机械工业出版社,第六版)复习——第八章:关系数据库设计
- 使用eclipse maven插件实现上传和下载jar包
- 包装类
- android设计模式-责任链模式与View Touch Event分发
- SDUT-1523 矩阵输出
- Fluent NHibernate的初学者
- git图形化工具GitKraken的使用——分支的创建与合并
- 数据库系统概念(机械工业出版社,第六版)复习——第十章:数据存储和数据存取
- Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示
- 重构后的Ajax
- PHP的核心Zend Engine
- 处理对象