版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Suubyy/article/details/82429078
业务需求
- 需求一、就是多个报表系统与多个数据源,多个报表系统与多个数据源系统组合成多种报表展示,这样就出现了二维层次结构,这样我们就可以使用桥接设计模式来设计结构。
- 需求二、当这个系统需要扩充一个数据源,新增一个Hbase源,那么当前当前接口可能跟Bbase数据源接口不兼容,这样我们就可以使用适配器模式,将原有系统的接口与需要集成的接口整合在一起,是他们可以一起工作。
实战架构图
Adapter
package com.lyz.test.adapter; /** * 系统原来的收集方法接口,如果需要兼容第三方接口就需要写一个适配器, * 使原来的收集方式与第三方的收集方式都兼容本系统 * * @Package com.lyz.test.birdge * @ClassName CollectText * @Description * @Author liyuzhi * @Date 2018-09-05 16:58 */ public interface CollectText { String collect(String s); }
package com.lyz.test.adapter; /** * 模拟JDBC收集到的数据 * * @Package com.lyz.test.adapter * @ClassName CollectJDBCImpl * @Description * @Author liyuzhi * @Date 2018-09-05 17:10 */ public class CollectJDBCImpl implements CollectJDBC { @Override public String jdbcCollect() { return "我是jdbc收集方式"; } }
package com.lyz.test.adapter; /** * 模拟文本文件方式收集到的数据 * * @Package com.lyz.test.birdge * @ClassName CollectTextIml * @Description * @Author liyuzhi * @Date 2018-09-05 17:00 */ public class CollectTextIml implements CollectText { @Override public String collect(String s) { return "我是文本文件收集方式"; } }
package com.lyz.test.adapter; /** * 这是数据收集的适配器 * 这个适配器适配了JDBC方式与普通文本方式 * * @Package com.lyz.test.adapter * @ClassName CollectAdapter * @Description * @Author liyuzhi * @Date 2018-09-05 17:11 */ public class CollectAdapter implements CollectText { private CollectJDBC collectJDBC; private CollectText collect; //根据不同的类型创建不同的收集方式 public CollectAdapter(String type) { if (type.equalsIgnoreCase("jdbc")) { collectJDBC = new CollectJDBCImpl(); } else { collect = new CollectTextIml(); } } /** * 根据不同数据收集方式没获取不同数据源的数据 * * @param type 标识 * @return 收集到的数据 */ @Override public String collect(String type) { //判断是否是jdbc的方式收集数据 if (type.equalsIgnoreCase("jdbc")) { return collectJDBC.jdbcCollect(); } else { //否则就是以文本文件方式收集数据 return collect.collect(type); } } }
Bridge
package com.lyz.test.birdge; /** * 报表展示的类 * * @Package com.lyz.test.adapter * @ClassName BirtShow * @Description * @Author liyuzhi * @Date 2018-09-05 16:52 */ public interface BirtShow { //报表展示的方法 void show(String s); }
package com.lyz.test.birdge; /** * 报表展示方式1 * * @Package com.lyz.test.birdge * @ClassName BirtShowImpl1 * @Description * @Author liyuzhi * @Date 2018-09-05 16:53 */ public class BirtShowImpl1 implements BirtShow { @Override public void show(String s) { System.out.println("报表1展示的内容为---"+s); } }
package com.lyz.test.birdge; /** * 报表展示方式2 * * @Package com.lyz.test.birdge * @ClassName BirtShowImpl2 * @Description * @Author liyuzhi * @Date 2018-09-05 16:53 */ public class BirtShowImpl2 implements BirtShow { @Override public void show(String s) { System.out.println("报表2展示的内容为---" + s); } }
package com.lyz.test.birdge; /** * 桥接抽象类。这类中的成员变量是两个维度中的其中一个维度,这个维度一关联的方式代替了继承 * 的方式使抽象类与实现类脱耦 * * @Package com.lyz.test.birdge * @ClassName BirtShowAbstract * @Description * @Author liyuzhi * @Date 2018-09-05 16:54 */ public abstract class BirtShowAbstract { private BirtShow birtShow; public BirtShowAbstract(BirtShow birtShow) { this.birtShow = birtShow; } public void showBirt(String s) { birtShow.show(s); } }
package com.lyz.test.birdge; import com.lyz.test.adapter.CollectAdapter; /** * 以JDBC为数据源来展示报表 * * @Package com.lyz.test.birdge * @ClassName BirtShowTextData * @Description * @Author liyuzhi * @Date 2018-09-05 16:56 */ public class BirtShowJdbcData extends BirtShowAbstract { public BirtShowJdbcData(BirtShow birtShow) { super(birtShow); } public void showBirt() { super.showBirt(new CollectAdapter("jdbc").collect("jdbc")); } }
package com.lyz.test.birdge; import com.lyz.test.adapter.CollectAdapter; /** * 以文本文件为数据源来展示报表 * * @Package com.lyz.test.birdge * @ClassName BirtShowTextData * @Description * @Author liyuzhi * @Date 2018-09-05 16:56 */ public class BirtShowTextData extends BirtShowAbstract { //初始化BirtShowTextData 以及调用父类的构造方法实例化成员变量 public BirtShowTextData(BirtShow birtShow) { super(birtShow); } //报表展示方法,里边调用了桥接抽象类的showBrit方法,传入具体的收集方式参数,这个参数就是我们写的适配器,适配多个数据源 public void showBirt() { super.showBirt(new CollectAdapter("text").collect("text")); } }
package com.lyz.test; import com.lyz.test.adapter.*; import com.lyz.test.birdge.*; /** * 类的注释 * * @Package com.lyz.test * @ClassName TestClass * @Description * @Author liyuzhi * @Date 2018-09-05 17:17 */ public class TestClass { public static void main(String[] args) { BirtShowTextData showText = new BirtShowTextData(new BirtShowImpl1()); showText.showBirt(); BirtShowJdbcData showJdbc = new BirtShowJdbcData(new BirtShowImpl1()); showJdbc.showBirt(); BirtShowTextData showText1 = new BirtShowTextData(new BirtShowImpl2()); showText1.showBirt(); BirtShowJdbcData showJdbc1 = new BirtShowJdbcData(new BirtShowImpl2()); showJdbc1.showBirt(); /** * 执行结果 * * * 报表1展示的内容为---我是文本文件收集方式 * 报表1展示的内容为---我是jdbc收集方式 * 报表2展示的内容为---我是文本文件收集方式 * 报表2展示的内容为---我是jdbc收集方式 * */ } }