首页 > 代码库 > Java设计模式应用——桥接模式
Java设计模式应用——桥接模式
性能管理系统中,数据产生后需要经过采集,汇聚,入库三个流程,用户才能查询使用。
采集可以是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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。