android设计模式-责任链模式与View Touch Event分发

来源:互联网 发布:uva 知乎 编辑:程序博客网 时间:2024/06/06 03:33

背景与问题

差旅费申请场景
组员申请1w->组长(0~3k)->主管(0~5k)->经理(0~8k)->老板(0~2w)
组员只与组长具备关联,并不关心具体处理者是谁。
在开发中,许多对象都可能接受到请求,如何避免请求者和处理者耦合在一起?

解决方案

责任链模式

  1. 定义
    多个对象都能接受请求,并有机会处理请求,避免请求发送者与接受者的耦合关系。
  2. 使用场景
    多个对象可以接受同一请求,具体处理请求的对象在运行时动态决定。
    在请求的处理对象不明确时,向多个对象中的一个提交该请求。
  3. UML类图
    这里写图片描述

    • Handler:抽象处理者角色,声明一个请求处理的方法,并保持一个对下一个处理节点Handler对象的引用。
    • ConcreteHandler:具体处理者,对请求进行处理,不能处理则将请求转发。
  4. 实现示例
    代码比较简单,感兴趣的见最后github

  5. 应用示例分析
    Android系统对Touch事件的分发

    • View中负责处理touch事件的主要为dispatchTouchEvent()onTouchEvent()两个方法以及OnTouchListener接口
 /**     * 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.总结

  • 优点:请求者和处理者解耦。
  • 缺点:遍历,影响性能。

参考文献:

  1. android源码设计模式解析与实战
  2. http://www.cnblogs.com/linjzong/p/4191891.html
  3. http://blog.csdn.net/lmj623565791/article/details/39102591/

demo源码:
https://github.com/rLight121/android-design-pattern

原创粉丝点击