优先使用接口,还是优先复制代码? (coupling? v.s. copying )

访问量: 1235

今天遇到个问题:  ( today I met a problem: --- exactly, yesterday.)

有两个子系统: A , B。  我希望在A中使用B的一个既有的功能。 B中的这个功能,以接口的形式提供。  ( I have 2 systems: A and B. I want A has a function that B already implemented as a HTTP interface) 

A 和B共用一个数据库(也就是说,A可以直接读取B的数据) ( A and B share a same database ) 

问题: 是应该让A通过 http 直接访问B的接口, 还是把B的代码复制到A中,让A直接调用?  ( my question is: how to choose? (1) let A visit B's http interface  (2) copy B's code to A. )

答案: (my final solution) 

最理想的情况下,如果B系统永远稳定运行,接口永远不变,那么必然使用第一种办法。 ( if B is promised to run 24x7 , and its interface will never change, let's choose option(1) ) 

但是实际情况是:B的接口不稳定,可能会当机, 为了让A自己保持稳定(与B系统解藕),所以我的选择是在B中做个重构,把代码逻辑放到 client.rb文件中,然后把这个client.rb文件复制到A中,这样A就可以直接使用这个文件了。(更合适的办法,是把client.rb这个文件 做成一个gem, 这样A和B的代码就可以做到同步了) (but in the real world, B's interface is not stable at all, it may changes, it may get down. To make sure A is decoupled from B, I firstly did some  refactoring(move method from controller to model) , then copied B's model to A. (they have the same infrastructure, so the model code could be reused without any modification)  Now A has the same code with B)  --- and another thing, if I could make that duplicated-code a rubygem, then things will be perfect. )  

结论: 思路不能教条,要根据实际情况选择最合适的 办法。  ( conclusion:  there's no silver bullet, let's choose the best approach by the real situation. ) 

订阅/RSS Feed

Subscribe

分类/category