演进式例解控制反转(IoC)、依赖注入(DI)之一

原文地址:http://haolloyin.blog.51cto.com/1177454/458416/


近来总是接触到 IoCInversion of Control,控制反转)、DIDependency Injection,依赖注入)等编程原则或者模式,而这些是著名 Java 框架 SpringStruts 等的核心所在。针对此查了Wikipedia 中各个条目,并从图书馆借来相关书籍,阅读后有些理解,现结合书中的讲解以及自己的加工整理如下:

 
问题描述:

开发一个能够按照不同要求生成Excel PDF 格式的报表的系统,例如日报表、月报表等等。

 
解决方案:

根据“面向接口编程”的原则,应该分离接口与实现,即将生成报表的功能提取为一个通用接口ReportGenerator,并提供生成 Excel  PDF格式报表的两个实现类 ExcelGenerator PDFGenerator,而客户Client 再通过服务提供者 ReportService 获取相应的报表打印功能。

 
实现方法:
根据上面所述,得到如下类图:

 
代码实现:
 
 
 
  1. interface ReportGenerator { 
  2.     public void generate(Table table); 
  3.  
  4. class ExcelGenerator implements ReportGenerator { 
  5.     public void generate(Table table) { 
  6.         System.out.println("generate an Excel report ..."); 
  7.     } 
  8.  
  9. class PDFGenerator implements ReportGenerator { 
  10.     public void generate(Table table) { 
  11.         System.out.println("generate an PDF report ..."); 
  12.     } 
  13.  
  14. class ReportService { 
  15.     // 负责创建具体需要的报表生成器 
  16.     private ReportGenerator generator = new PDFGenerator()
  17.     // private static ReportGenerator generator = new ExcelGenerator(); 
  18.      
  19.     public void getDailyReport(Date date) { 
  20.         table.setDate(date); 
  21.         // ... 
  22.         generator.generate(table); 
  23.     } 
  24.      
  25.     public void getMonthlyReport(Month month) { 
  26.         table.setMonth(month); 
  27.         // ... 
  28.         generator.generate(table); 
  29.     } 
  30.  
  31.  
  32. public class Client { 
  33.     public static void main(String[] args) { 
  34.         ReportService reportService = new ReportService(); 
  35.         reportService.getDailyReport(new Date()); 
  36.         //reportService.getMonthlyReport(new Date()); 
  37.     } 
 
 
问题描述:

如上面代码中的注释所示,具体的报表生成器由 ReportService 类内部硬编码创建,由此ReportService 已经直接依赖 PDFGenerator  ExcelGenerator ,必须消除这一明显的紧耦合关系。

 
解决方案: 引入容器

引入一个中间管理者,也就是容器(Container),由其统一管理报表系统所涉及的对象(在这里是组件,我们将其称为 Bean),包括 ReportService 和各个 XXGenerator 。在这里使用一个键-值对形式的 HashMap 实例来保存这些 Bean

 
实现方法:
得到类图如下:

 
代码实现:
 
 
  1. class Container { 
  2.     // 以键-值对形式保存各种所需组件 Bean 
  3.     private static Map<String, Object> beans
  4.      
  5.     public Container() { 
  6.         beans = new HashMap<String, Object>(); 
  7.          
  8.         // 创建、保存具体的报表生起器 
  9.         ReportGenerator reportGenerator = new PDFGenerator()
  10.         beans.put("reportGenerator", reportGenerator); 
  11.          
  12.         // 获取、管理 ReportService 的引用 
  13.         ReportService reportService = new ReportService(); 
  14.         beans.put("reportService", reportService); 
  15.     } 
  16.      
  17.     public static Object getBean(String id) { 
  18.         return beans.get(id); 
  19.     } 
  20.  
  21. class ReportService { 
  22.     // 消除紧耦合关系,由容器取而代之 
  23.     // private static ReportGenerator generator = new PDFGenerator(); 
  24.     private ReportGenerator generator = (ReportGenerator) Container.getBean("reportGenerator"); 
  25.  
  26.     public void getDailyReport(Date date) { 
  27.         table.setDate(date); 
  28.         generator.generate(table); 
  29.     } 
  30.      
  31.     public void getMonthlyReport(Month month) { 
  32.         table.setMonth(month); 
  33.         generator.generate(table); 
  34.     } 
  35.  
  36. public class Client { 
  37.     public static void main(String[] args) { 
  38.         Container container = new Container(); 
  39.         ReportService reportService = (ReportService)Container.getBean("reportService"); 
  40.         reportService.getDailyReport(new Date()); 
  41.         //reportService.getMonthlyReport(new Date()); 
  42.     } 
 
时序图大致如下:

 
效果:

如上面所示,ReportService 不再与具体的 ReportGenerator 直接关联,已经用容器将接口和实现隔离开来了,提高了系统组件 Bean 的重用性,此时还可以使用配置文件在 Container 中实时获取具体组件的定义。

 
 
问题描述:

然而,观察上面的类图,很容易发现 ReportService  Container 之间存在双向关联,彼此互相有依赖关系。并且,如果想要重用 ReportService,由于它也是直接依赖于单独一个 Container 具体查找逻辑。若其他容器具体不同的组件查找机制(如 JNDI),此时重用 ReportService 意味着需要修改 Container 的内部查找逻辑。

 
解决方案: 引入  Service Locator

再次引入一个间接层 Service Locator,用于提供组件查找逻辑的接口,请看Wikipedia 中的描述 或者 Java EE 对其的描述1 描述2 。这样就能够将可能变化的点隔离开来。

 
实现方法:
类图如下:

 
代码实现:
 
 
 
  1. // 实际应用中可以是用 interface 来提供统一接口 
  2. class ServiceLocator { 
  3.     private static Container container = new Container(); 
  4.      
  5.     public static ReportGenerator getReportGenerator() { 
  6.         return (ReportGenerator)container.getBean("reportGeneraator"); 
  7.     } 
  8.  
  9. class ReportService { 
  10.     private ReportGenerator reportGenerator = ServiceLocator.getReportGenerator(); 
  11.      
  12.     // ... 


 

小结:

1、虽然讲了这么大篇幅还没有进入真正的主题——IoCDI,不过已经在一步步逼近了,下一篇应该会更精彩!在这里...

2、可以很明显地看得出上面两中重新设计以解耦、隔离变化点都是通过引入间接层得以解决的。

3、在看书过程中,我感觉《Spring 攻略》一书中以“问题描述、解决方案、实现方法”方式的讲解比较容易理解和理清思路,故而也学习用这种方式来写。另,推荐该书以学习 Spring 框架(尽管目前我看得也不多)。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值