Java設計模式應用——橋接模式

來源:互聯網
上載者:User

標籤:靈活   適合   選擇   java   java設計   log   橋接   null   company   

效能管理系統中,資料產生後需要經過採集,匯聚,入庫三個流程,使用者才能查詢使用。

採集可以是snmp採集,也可以是ems採集;匯聚可以使storm匯聚,也可以是spark匯聚;入庫可以是hdfs入庫,也可以是mppdb入庫。

針對不同情境,我們可以靈活選擇不同的採集,匯聚,入庫方式。這種一個功能需要多種服務支援,每種服務又有不同類型的實現,使用橋接模式再適合不過。

(註:這裡僅僅是橋接模式的例子,實際應用中,採集、匯聚、入庫時非同步執行的,他們之間通過訊息通訊)

橋接模式,顧名思義,就是把每種服務看做一座橋,我們可以根據實際情境選擇不同的橋。

上述例子表示資料產生到可以使用之前需要經過三座橋:採集橋->匯聚橋->入庫橋。每座橋可以選擇不同的構造。

下面來看具體代碼

1、 採集橋

package com.coshaho.learn.bridge.collect;/** *  * CollectionService.java Create on 2017年6月1日 下午11:00:10     *     * 類功能說明:   採集服務 * * Copyright: Copyright(c) 2013  * Company: COSHAHO * @Version 1.0 * @Author coshaho */public abstract class CollectionService {    abstract void execute();    public void run()    {        execute();    }}

2、 匯聚橋

package com.coshaho.learn.bridge.aggregate;import com.coshaho.learn.bridge.collect.CollectionService;/** *  * AggregationService.java Create on 2017年6月1日 下午11:00:23     *     * 類功能說明:   匯聚服務 * * Copyright: Copyright(c) 2013  * Company: COSHAHO * @Version 1.0 * @Author coshaho */public abstract class AggregationService {    public void run()    {        if(null != collectionService)        {            collectionService.run();        }        execute();    }        abstract void execute();    CollectionService collectionService;    public AggregationService(CollectionService collectionService)    {        this.collectionService = collectionService;    }}

3、 入庫橋

package com.coshaho.learn.bridge.store;import com.coshaho.learn.bridge.aggregate.AggregationService;/** *  * StoreService.java Create on 2017年6月1日 下午11:08:57     *     * 類功能說明:   入庫服務 * * Copyright: Copyright(c) 2013  * Company: COSHAHO * @Version 1.0 * @Author coshaho */public abstract class StoreService {    public void run()    {        if(null != aggregationService)        {            aggregationService.run();        }        execute();    }        abstract void execute();    AggregationService aggregationService;    public StoreService(AggregationService aggregationService)    {        this.aggregationService = aggregationService;    }}

4、 EMS採集橋

package com.coshaho.learn.bridge.collect;public class EMSCollectionService extends CollectionService{    @Override    void execute()     {        System.out.println("EMS collection.");    }}

5、 SNMP採集橋

package com.coshaho.learn.bridge.collect;public class SNMPCollectionService extends CollectionService{    @Override    void execute()     {        System.out.println("SNMP collection.");    }}

6、 Storm匯聚橋

package com.coshaho.learn.bridge.aggregate;import com.coshaho.learn.bridge.collect.CollectionService;public class StormAggregationService extends AggregationService{    public StormAggregationService(CollectionService collectionService)     {        super(collectionService);    }    @Override    void execute()     {        System.out.println("Storm aggregation.");    }}

7、 Spark匯聚橋

package com.coshaho.learn.bridge.aggregate;import com.coshaho.learn.bridge.collect.CollectionService;public class SparkAggregationService extends AggregationService{    public SparkAggregationService(CollectionService collectionService)     {        super(collectionService);    }    @Override    void execute()     {        System.out.println("Spark aggregation.");    }}

8、 MPPDB入庫橋

package com.coshaho.learn.bridge.store;import com.coshaho.learn.bridge.aggregate.AggregationService;public class MPPDBStoreService extends StoreService{    public MPPDBStoreService(AggregationService aggregationService)    {        super(aggregationService);    }    @Override    void execute()     {        System.out.println("MPPDB store.");    }}

9、 HDFS入庫橋

package com.coshaho.learn.bridge.store;import com.coshaho.learn.bridge.aggregate.AggregationService;public class HDFSStoreService extends StoreService{    public HDFSStoreService(AggregationService aggregationService)     {        super(aggregationService);    }    @Override    void execute()     {        System.out.println("HDFS store.");    }}

10、 測試類別

package com.coshaho.learn.bridge;import com.coshaho.learn.bridge.aggregate.AggregationService;import com.coshaho.learn.bridge.aggregate.StormAggregationService;import com.coshaho.learn.bridge.collect.CollectionService;import com.coshaho.learn.bridge.collect.SNMPCollectionService;import com.coshaho.learn.bridge.store.HDFSStoreService;import com.coshaho.learn.bridge.store.StoreService;/** *  * BridgeTest.java Create on 2017年6月1日 下午10:59:43     *     * 類功能說明:   橋接模式測試 * * Copyright: Copyright(c) 2013  * Company: COSHAHO * @Version 1.0 * @Author coshaho */public class BridgeTest {    public static void main(String[] args)    {        CollectionService snmpService = new SNMPCollectionService();        AggregationService stormService = new StormAggregationService(snmpService);        StoreService hdfsService = new HDFSStoreService(stormService);        hdfsService.run();    }}

11、 測試結果

SNMP collection.Storm aggregation.HDFS store.

 

Java設計模式應用——橋接模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.