外觀模式

定義

要求一個(gè)子系統(tǒng)的外部和其內(nèi)部的通信必須使用一個(gè)統(tǒng)一的對象進(jìn)行,外觀模式提供一個(gè)高層次的接口,使得子系統(tǒng)更易于使用

外觀模式本質(zhì)上是在子系統(tǒng)和客戶端之間加了一層訪問入庫層(中間層),中間層可以由子系統(tǒng)提供,也可以由客戶端自己封裝。如果子系統(tǒng)提供,則該層負(fù)責(zé)兩件事:1.控制子系統(tǒng)的訪問范圍 2.封裝自己實(shí)現(xiàn)細(xì)節(jié),簡化客戶端使用難度。 如果由客戶端提供,則一般是用作隔離和適配,通過減少直接依賴和增加中間層適配來簡化后期切換子系統(tǒng)時(shí)的難度。

角色

門面角色:客戶端可以調(diào)用這個(gè)角色來訪問子系統(tǒng),該角色了解子系統(tǒng)所有的功能,其接收客戶端請求,并將其轉(zhuǎn)發(fā)給子系統(tǒng)內(nèi)部模塊處理,本質(zhì)是一個(gè)委托類。
子系統(tǒng)角色:子系統(tǒng)涵蓋多個(gè)類,自我實(shí)現(xiàn)功能模塊,其本身并不知曉門面的存在,門面角色僅僅是其一個(gè)客戶端而已

代碼

public final class ARouter {
    // Key of raw uri
    public static final String RAW_URI = "NTeRQWvye18AkPd6G";
    public static final String AUTO_INJECT = "wmHzgD4lOj5o4241";

    private volatile static ARouter instance = null;
    private volatile static boolean hasInit = false;
    public static ILogger logger;

    private ARouter() {
    }

    /**
     * Init, it must be call before used router.
     */
    public static void init(Application application) {
        if (!hasInit) {
            logger = _ARouter.logger;
            _ARouter.logger.info(Consts.TAG, "ARouter init start.");
            hasInit = _ARouter.init(application);

            if (hasInit) {
                _ARouter.afterInit();
            }

            _ARouter.logger.info(Consts.TAG, "ARouter init over.");
        }
    }

    /**
     * Get instance of router. A
     * All feature U use, will be starts here.
     */
    public static ARouter getInstance() {
        if (!hasInit) {
            throw new InitException("ARouter::Init::Invoke init(context) first!");
        } else {
            if (instance == null) {
                synchronized (ARouter.class) {
                    if (instance == null) {
                        instance = new ARouter();
                    }
                }
            }
            return instance;
        }
    }

    public static synchronized void openDebug() {
        _ARouter.openDebug();
    }

    public static boolean debuggable() {
        return _ARouter.debuggable();
    }

    public static synchronized void openLog() {
        _ARouter.openLog();
    }

    public static synchronized void printStackTrace() {
        _ARouter.printStackTrace();
    }

    public static synchronized void setExecutor(ThreadPoolExecutor tpe) {
        _ARouter.setExecutor(tpe);
    }

    public synchronized void destroy() {
        _ARouter.destroy();
        hasInit = false;
    }

    /**
     * The interface is not stable enough, use 'ARouter.inject();';
     */
    @Deprecated
    public static synchronized void enableAutoInject() {
        _ARouter.enableAutoInject();
    }

    public static boolean canAutoInject() {
        return _ARouter.canAutoInject();
    }

    /**
     * The interface is not stable enough, use 'ARouter.inject();';
     */
    @Deprecated
    public static void attachBaseContext() {
        _ARouter.attachBaseContext();
    }

    public static synchronized void monitorMode() {
        _ARouter.monitorMode();
    }

    public static boolean isMonitorMode() {
        return _ARouter.isMonitorMode();
    }

    public static void setLogger(ILogger userLogger) {
        _ARouter.setLogger(userLogger);
    }

    /**
     * Inject params and services.
     */
    public void inject(Object thiz) {
        _ARouter.inject(thiz);
    }

    /**
     * Build the roadmap, draw a postcard.
     *
     * @param path Where you go.
     */
    public Postcard build(String path) {
        return _ARouter.getInstance().build(path);
    }

    /**
     * Build the roadmap, draw a postcard.
     *
     * @param path  Where you go.
     * @param group The group of path.
     */
    @Deprecated
    public Postcard build(String path, String group) {
        return _ARouter.getInstance().build(path, group);
    }

    /**
     * Build the roadmap, draw a postcard.
     *
     * @param url the path
     */
    public Postcard build(Uri url) {
        return _ARouter.getInstance().build(url);
    }

    /**
     * Launch the navigation by type
     *
     * @param service interface of service
     * @param <T>     return type
     * @return instance of service
     */
    public <T> T navigation(Class<? extends T> service) {
        return _ARouter.getInstance().navigation(service);
    }

    /**
     * Launch the navigation.
     *
     * @param mContext    .
     * @param postcard    .
     * @param requestCode Set for startActivityForResult
     * @param callback    cb
     */
    public Object navigation(Context mContext, Postcard postcard, int requestCode, NavigationCallback callback) {
        return _ARouter.getInstance().navigation(mContext, postcard, requestCode, callback);
    }
}

總結(jié)

優(yōu)點(diǎn):

減少系統(tǒng)的相互依賴。使用門面模式,所有的依賴都是對門面對象的依賴,與子系統(tǒng)無關(guān)

提高了靈活性。不管子系統(tǒng)內(nèi)部如何變化,只要不影響門面對象,任你自由活動。

缺點(diǎn):

不符合開閉原則,對修改關(guān)閉,對擴(kuò)展開放

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、模式定義 外觀模式(Facade Pattern):外部與一個(gè)子系統(tǒng)的通信必須通過一個(gè)統(tǒng)一的外觀對象進(jìn)行,為子...
    QuantRuu閱讀 818評論 0 50
  • 一、模式定義 外觀模式(Facade Pattern):外部與一個(gè)子系統(tǒng)的通信必須通過一個(gè)統(tǒng)一的外觀對象進(jìn)行,為子...
    端木軒閱讀 4,026評論 2 2
  • 模式定義 外觀模式(Facade Pattern):外部與一個(gè)子系統(tǒng)的通信必須通過一個(gè)統(tǒng)一的外觀對象進(jìn)行,為子系統(tǒng)...
    lijun_m閱讀 443評論 0 0
  • 【學(xué)習(xí)難度:★☆☆☆☆,使用頻率:★★★★★】直接出處:外觀模式梳理和學(xué)習(xí):https://github.com/...
    BruceOuyang閱讀 854評論 0 0
  • 中午吃過飯,沿著河邊小路閑散的走著,此時(shí)內(nèi)外兩股暖意和身體做著親密的互動,頓覺困意。于是踅進(jìn)街角公園,只見...
    天天向上韓建平閱讀 181評論 0 0

友情鏈接更多精彩內(nèi)容