`

Refactor integration code

 
阅读更多

      在开发过程中经常会遇到不同系统的集成integraion。例如:A系统的一个版本可能会支持B系统的若干版本,而B系统的不同版本间的接口有可能会更改(好的系统设计一般不会改接口的,至少也会向下兼容)或者处理流程有一些改变。

      对于这种情况,在开发初期为了赶进度,代码中充斥着if...else...的版本判断

      if (B.version == 1.0) {

           //do something here

      } else (B.version == 1.1) {

          //do something here

      } else {

         //do something here

     }

 

     这样的代码不够优雅,也不够灵活,日后若需要支持新的版本,还要增加else语句。

 

     因此,需要对上述代码进行重构,其中重构的方法之一就是在用代理模式proxy pattern。

    比如系统集成的接口是 BSystemIntegraion, 在BSystemIntegraionImpl中有一些if...else...的代码。

    现在可以定义一个BSystemIntegraionImplProxy 类,也实现BSystemIntegraion接口。A系统对于B系统不同版本的公共功能的方法实现放在BSystemIntegraionImpl类中,不同版本若有不同于公共实现的处理,可以放在各自的实现类BSystemIntegraionImplVer10/BSystemIntegraionImplVer11/BSystemIntegraionImplVer12中,这些类继承了BSystemIntegraionImpl类,也可以

BSystemIntegraionImpl<--BSystemIntegraionImplVer10<--BSystemIntegraionImplVer11<--BSystemIntegraionImplVer12。

(根据实际情况确定不同版本实现类之间的继承关系。)

 

在BSystemIntegraionImplProxy 类中定义不同版本实现的实例,比如:

private BSystemIntegraion bSysIntegraionVer10;

private BSystemIntegraion bSysIntegraionVer11;

private BSystemIntegraion bSysIntegraionVer12;

private BSystemIntegraion bSysIntegraionImpl;

 

在BSystemIntegraionImplProxy 类中定义一个private方法,用于根据不同的版本返回不同的实现。

private BSystemIntegraion getBSystemIntegraionImpl(args) {

       if (B.version==1.0) return    bSysIntegraionVer10;

       if (B.version==1.1) return    bSysIntegraionVer11;

       if (B.version==1.2) return    bSysIntegraionVer12;

 

       return bSysIntegraionImpl;

}

在BSystemIntegraionImplProxy 类实现BSystemIntegraion接口的方法中,可以这样写

@Override

public BusinessObject  bizFunction1(args) {

    return this.getBSystemIntegraionImpl(args).bizFunction1(args);

}

 

在Spring配置文件中,分别定义BSystemIntegraionImplVer10、BSystemIntegraionImplVer11、BSystemIntegraionImplVer12 bean,并把他们注入到 BSystemIntegraionImplProxy bean定义中,把原来spring配置文件中引用BSystemIntegraionImpl ben的地方改成引用BSystemIntegraionImplProxy bean。

 

这样代码就清晰多了。

以后新增B系统版本的支持,需要

1)定义新的实现类,比如BSystemIntegraionImplVer13,

2)修改BSystemIntegraionImplProxy 类,并增加BSystemIntegraion bSysIntegraionVer13属性,修改getBSystemIntegraionImpl(args)方法,

3)修改spring配置文件。

 

这里是工作中的一些记录,如果各位有更好的方法,欢迎指教。谢谢!

分享到:
评论

相关推荐

    refactor clean code design pattern书单

    refactor clean code design pattern aaaaaaaaaa

    refactor(重构-改善既有代码的设计)

    refactor(重构-改善既有代码的设计),中文版共15章

    CodeRush with Refactor! Pro 2.5.1

    CodeRush™ with Refactor!™ Pro Powerful. Efficient. Fast. &lt;br&gt;Today, software consumers demand more than ever before from developers. The pressure to deliver more features with ...

    hw01_code_refactor-main-源码.rar

    hw01_code_refactor-main-源码.rar

    Refactor pro

    Refactor pro 1.0.31

    code_refactor

    code_refactor 前端和初级开发人员最常见的任务之一是采用现有代码并对其进行重构,以满足特定的标准集或实施新技术。 Web 可访问性对于企业来说是一个越来越重要的考虑因素,确保残疾人或社会经济限制的人可以访问...

    iOS Code Testing

    iOS Code Testing offers helpful instruction to teach iOS developers to retrospectively fit tests to legacy code, refactor legacy code so as to make the code more testable, install and configure a ...

    01_Code-Refactor

    作业1 在有关我更改的内容和原因的段落中添加项目符号。 语义html元素在其代码库中发现差异任务是重构网站以确保其符合可访问性标准针对搜索引擎进行了优化地址接受标准项目:-项目符号特点:-更改在代码中注释-元素...

    code-refactor

    一种。 应用说明 重构是一种重组现有代码的技术,该代码易于阅读,更易于其他Web开发人员进行调试,并且修改成本较低,同时又保持了现有代码的外部行为。 我认为,在合并现有的不重复开发人员(DRY)的编码开发人员...

    CodeRush 和 Refactor Pro的最新版本11.2.8

    CodeRush 和 Refactor Pro的最新版本11.2.8,我从网站上下载的,要注册才能下载,要的朋友就不用注册了,免费下载了。

    js2-refactor.el, emacs的JavaScript重构库.zip

    js2-refactor.el, emacs的JavaScript重构库 js2-refactor.el emacs的JavaScript重构库。这是一个小型重构函数的Collection,以进一步了解从js2-mode开始的Emacs中的JavaScript IDE 。在 0.8.0中更改添加 e

    todo-thrower-js:确保您的待办事项在过期时抛出异常来完成! 对于 Meteor 和 NPM

    TODO JS 确保您的待办事项在过期时抛出异常来完成! 对于 Meteor 和 NPM。 安装 流星meteor add mantarayar:todo ...句法 您可以使用TODO 、 FIXME和XXX作为需要完成功能的指标。...XXX ( 'Refactor this code

    Code-Refactor:HTML CSS Git

    代码重构资料库该存储库(代码重构)是CARL-OTT-FSF-PT-02-20121-UC课程中的一项教育作业。... 还有一个CSS组件,使学生可以更加了解自己的内在联系。部署的网站链接任务任务是帮助市场营销机构重构现有代码,使其符合...

    How we refactor, and how we know it

    How we refactor, and how we know it.pdf

    Code-Refactor

    作业指导 每周,您的讲师都将针对该周的家庭作业的说明添加到课程回购中,并宣布已准备就绪。 您的讲师和助教还将在课堂上演示家庭作业。 如何做功课 作业旨在为您做好准备,以迎接开发人员遇到的两种情况: ...

    BuildSql Refactor

    NULL 博文链接:https://weigang-gao.iteye.com/blog/2200089

    refactor(FrameWork4.0)

    对版本进行了升级,对版本进行了升级,对版本进行了升级,对版本进行了升级

    sublime-text-refactor, 用于Javascript代码的sublime text 重构插件.zip

    sublime-text-refactor, 用于Javascript代码的sublime text 重构插件 用于 sublime-text-2和 3的 Javascript重构插件 [[Package Control] ( https://packagecontrol.herokuapp.com/downloads/JavaScript%2

    horiseon-refactor-code:Horiseon代码库进行重构,以遵循可访问性标准进行搜索引擎优化

    霍里森重构码目的Horiseon网站代码库进行重构,以遵循可访问性标准来优化搜索引擎。 对代码进行重组以遵循语义结构以及适当的缩进和注释,以使代码易于阅读。... 在CSS中添加了注释对辅助功能进行了测试,以查看平均...

    代码的重构,改善代码设计 refactor

    重构 改善代码 代码清晰 clean code 开发必备 重构必备

Global site tag (gtag.js) - Google Analytics