码迷,mamicode.com
首页 > 其他好文 > 详细

调停者模式

时间:2019-05-10 15:04:50      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:nal   imm   related   sage   hide   代码   keyguard   sys   style   

一、概述

一般问题:系统内部经常会出现多个类互相耦合,形成网状结构。任意一个类发生改变,所有调用者也会受到影响,造成阅读和维护困难。

核心方案:用一个中介对象来封装多个耦合对象的交互,使各对象不需要显示地互相调用,从而使其耦合松散。

设计意图:调停者是对系统内部运转的协调,是从架构设计之初就应该考虑的问题。那么,架构设计应该面向接口,调停者模式的示意性类图如下:

技术图片

引入调停者模式的意图是将一对多抓化成一对一,降低整个系统的交互复杂度。

技术图片技术图片

 


 

二、应用场景

Android 锁屏应用采用了调停者模式,锁屏是一个特殊的与系统关系密切的App,PhoneWindowManager通过KeyguardService对锁屏有着频繁的影响(如ScreenOn/Off、BootComplete、SwtichUser等),KeyguardUpdateMonitor收集的外部信息变化(如指纹识别、sim卡状态等)也对锁屏View有着频繁影响。同时,它们也依赖锁屏提供的一些状态信息,以及View变化的回调。总之,如果没有中介者,那系统和KeyguardUpdateMonitor与锁屏View的耦合将非常严重。因此,在锁屏架构中出现了KeyguardViewMediator类作为调停者,来统一处理这些复杂交互。

KeyguardViewMediator设计图如下:

技术图片

如下是google对KeyguardViewMediator类的作用解释:

/**
     * Mediates requests related to the keyguard.  This includes queries about the
     * state of the keyguard, power management events that effect whether the keyguard
     * should be shown or reset, callbacks to the phone window manager to notify
     * it of when the keyguard is showing, and events from the keyguard view itself
     * stating that the keyguard was succesfully unlocked.
     *
     * Note that the keyguard view is shown when the screen is off (as appropriate)
     * so that once the screen comes on, it will be ready immediately.
     *
     * Example queries about the keyguard:
     * - is {movement, key} one that should wake the keygaurd?
     * - is the keyguard showing?
     * - are input events restricted due to the state of the keyguard?
     *
     * Callbacks to the phone window manager:
     * - the keyguard is showing
     *
     * Example external events that translate to keyguard view changes:
     * - screen turned off -> reset the keyguard, and show it so it will be ready
     *   next time the screen turns on
     * - keyboard is slid open -> if the keyguard is not secure, hide it
     *
     * Events from the keyguard view:
     * - user succesfully unlocked keyguard -> hide keyguard view, and no longer
     *   restrict input events.
     */

由于一般Mediator类的代码比较复杂,这里不再贴代码,只列一下KeyguardViewMediator对这些交互的一些封装列表:

// used for handler messages
        private static final int SHOW = 1;
        private static final int HIDE = 2;
        private static final int RESET = 3;
        private static final int VERIFY_UNLOCK = 4;
        private static final int NOTIFY_FINISHED_GOING_TO_SLEEP = 5;
        private static final int NOTIFY_SCREEN_TURNING_ON = 6;
        private static final int KEYGUARD_DONE = 7;
        private static final int KEYGUARD_DONE_DRAWING = 8;
        private static final int SET_OCCLUDED = 9;
        private static final int KEYGUARD_TIMEOUT = 10;
        private static final int DISMISS = 11;
        private static final int START_KEYGUARD_EXIT_ANIM = 12;
        private static final int KEYGUARD_DONE_PENDING_TIMEOUT = 13;
        private static final int NOTIFY_STARTED_WAKING_UP = 14;
        private static final int NOTIFY_SCREEN_TURNED_ON = 15;
        private static final int NOTIFY_SCREEN_TURNED_OFF = 16;
        private static final int NOTIFY_STARTED_GOING_TO_SLEEP = 17;
        private static final int SYSTEM_READY = 18;
        private static final int REQUEST_UNLOCK = 100;

三、总结

优点:集中控制交互,将一对多转化成一对一,降低了耦合度

缺点:中介类会庞大,变得复杂难以维护

总结:调停者模式是一种行为型设计模式,它的目的在于降低系统内部各组件之间的耦合,集中控制交互,所以它是内向闭合的,要与外观模式的外向公开特性区分。同时,中介类的内部设计要有条理,规范注释,避免随着系统需求不断升级而变得过于臃肿,难以维护。

 

调停者模式

标签:nal   imm   related   sage   hide   代码   keyguard   sys   style   

原文地址:https://www.cnblogs.com/not2/p/10844226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!