减少方法的耦合性,同时保持其灵活性。

huangapple go评论68阅读模式
英文:

Minimizing a method's coupling while still keeping it flexible

问题

以下是翻译好的内容:

我有一个方法,今天需要使用两个参数来完成计算。这两个参数未来变为50的可能性非常大。鉴于此,什么是好的呢?

  1. 今天编写带有两个整数参数的 func(int a, int b) 方法。当参数超过7时,转换为下面的第2或第3种情况。
  2. 今天编写带有 RootOfObjectGraphWith50Fields a 参数的方法 func(RootOfObjectGraphWith50Fields a)RootOfObjectGraphWith50Fields 在代码库的其他部分中使用)。这样,当需要更多参数时,方法签名无需更改。但这会增加耦合性(方法需要了解更多信息才能完成业务逻辑)。
  3. 今天编写带有 InterfaceWith50Fields a 参数的方法(仅使用2个字段),并使 RootOfObjectGraphWith50Fields 实现 InterfaceWith50Fields 接口。这样,方法签名在每次需要新值来完成计算时无需更改。该方法与整个 RootOfObjectGraphWith50Fields 不是紧密耦合的,而只与其感兴趣的字段耦合(在 InterfaceWith50Fields 中表示)。

除了推荐意见外,我还可以为您提供关于这些推荐背后的基本设计原则的指导。

英文:

I have a method that takes 2 parameters today to complete the computation. The prospects of these 2 parameters becoming 50 in the future is pretty good. Given this, what is good?

  1. Write func(int a, int b) today. When parameters go beyond 7, then transition to 2 or 3 below.
  2. Write func(RootOfObjectGraphWith50Fields a) today(RootOfObjectGraphWith50Fields is used in other parts of the code base). This way when more parameters are needed, the method signature need not change. But this increases coupling (the method needs to know more to complete its business).
  3. Write func(InterfaceWith50Fields a) today (with only 2 fields) and have RootOfObjectGraphWith50Fields implement InterfaceWith50Fields. This way the method signature does not have to change every time a new value is required to complete the computation. The method is not coupled to the entire RootOfObjectGraphWith50Fields but only to those fields that it is interested in (expressed in InterfaceWith50Fields).

Besides recommendations, I appreciate pointers to basic design principles behind the recommendations.

答案1

得分: 1

我实际上会推荐两个选项:选项1和选项3。我之所以推荐第一个选项,是因为如果您当前的要求是该方法接受两个参数,那么func(int a, int b)是完全合适的,至少在目前是这样。如果以后这个方法需要50个参数,那么您可以随时在类中添加一个重载的func(InterfaceWith50Fields a)版本。

需要注意的是,第三个选项与第二个选项相比,除了避免更改方法签名外,没有太多优势。因为您传递的POJO实际上只是用来封装各种类型的50个字段,任何实现类中的相关方法/行为都将是获取器。由于接口通常不知道其实现类可能拥有的数据,因此在从50个参数升级到51个参数时,您可能仍需要重写一些代码。

英文:

I'm actually going to recommend both options 1 and 3. I recommend the first option because if your current requirement is that the method take 2 parameters, then func(int a, int b) is completely appropriate, at least right now. If later on this method would require 50 parameters, then you can always just add an overloaded func(InterfaceWith50Fields a) version to the class.

Note that the third option does not offer very much over the second one other than that it avoids having to change the method signature. As the POJO you are passing in really only exists to wrap 50 fields of various types, the relevant methods/behaviors in any implementing class here would be the getters. As interfaces typically don't know anything about the data which their implementing classes would have, you might still have to rewrite some code as you upgrade from 50 to say 51 parameters.

huangapple
  • 本文由 发表于 2020年10月3日 21:30:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64184819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定