Android事件總線之 square.otto

ps.本篇不說源碼, 只介紹原理. 源碼很少, 可以自己去詳細觀摩...

引言

首先介紹下 otto,

An enhanced Guava-based event bus with emphasis on Android support.
Otto is an event bus designed to decouple different parts of your application while still allowing them to communicate efficiently.
---- 以上摘自官方文檔

簡言之就是很牛叉的工具, 牛到: 既能降低代碼各個模塊間的耦合, 又能保證模塊之間高效通信.
(本篇基于版本com.squareup:otto:1.3.8)
那么內部是怎么實現的呢?

先說說模塊間通信這件事兒

通常來講, 兩個類或者兩個模塊之間如何通信呢?
以Activity 和 Fragment的通信為例, 其實就是相互持有對象, 對象再調用對方的方法. (Fragment 在Activity 中創建, 因此 Activity持有 Fragment的對象; Fragment寄生于Activity 中, 通過getActivity()拿到對象)
這種最基本的通信方式可用但并不優雅, 耦合性太高.
怎么解耦? 當然需要找個"中介".
我們暫且拋開" 注解 , 反射 , 緩存, 以及其他附加功能 "不談, 先實現一個最簡單的 較低耦合的通信模塊.

簡單實現

通信示意圖
public class Sender {

    public void send() {
        Phone.getDefault().post(new Msg("這是消息內容!"));
    }
}


public class Receiver {

    public void onReceive(Msg msg) {
        if (msg != null)
            System.out.println("收到消息: " + msg.getContent());
    }
}


public class Phone {

    private static Phone sInstance = new Phone();

    public static Phone getDefault(){
        return sInstance;
    }
    /**
     * 通訊錄
     */
    private List<Receiver> contacts = new ArrayList<>();

    /**
     * 在通訊錄里添加聯系人
     * @param target
     */
    public void register(Receiver target){
        if (!contacts.contains(target)) {
            contacts.add(target);
        }
    }

    /**
     * 移除通訊錄中的某個聯系人
     * @param target
     */
    public void unregister(Receiver target){
        contacts.remove(target);
    }

    /**
     * 給通訊錄里的人群發消息
     * @param msg
     */
    public void post(Msg msg) {
        for (Receiver receiver : contacts) {
            receiver.onReceive(msg);
        }
    }
}



public class Main {

    public static void main(String[] args){
        Receiver receiver = new Receiver();
        Phone.getDefault().register(receiver);

        new Sender().send();

        Phone.getDefault().unregister(receiver);
    }
}

以上便是一個簡化版的EventBus, 消息的發送者 和 接受者 兩個模塊之間幾乎沒有耦合, 同時又能借助手機這個中介實現通信.
是不是超級簡單! 本質上就是構造一個全局對象(Phone), 利用這個共享的全局對象進行數據傳遞. 對, otto 就是這個原理. 至于其他額外的擴展需求, 譬如:

  • 自定義消息格式(不局限與Msg這個類)
  • 自定義接收消息的方法名(不局限于onReceive這個方法)
  • 消息分組/定向發送(指定消息的接受者)
  • ...

就得加上 反射 / 自定義注解 等看起來高大上的東西了....

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,532評論 25 708
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 公司:寧波大發化纖有限公司 姓名:馮玉停 期數:六項精進224期感謝二組學員,234期感謝三組志工,260期感謝一...
    塵埃wyzh閱讀 67評論 0 0
  • 靜,無聲的修持 心躁動,源于陷入太深,不是陷入迷亂的歡愛,就是陷入欲望的泥沼。 “沖動是魔鬼”,輕易被言語刺激,被...
    王相成閱讀 575評論 0 1
  • 1 我走在白茫茫的大霧里,除了四周迷茫的霧氣,還有冷,從頭到腳的冰冷····· 娘!娘!我哭喊著······尋找著...
    黃飛蝗閱讀 472評論 4 6