Twe*_*les 2 java oop refactoring
所以我弄错了.
在最初为API编写签名时,我创建了以下内容:
public JellyBeanResult getJellyBeanReport();
Run Code Online (Sandbox Code Playgroud)
现在,事实证明我想重新使用更具体的JellyBeanResult对象,因为它的功能,但让其他函数返回一个为不同进程命名的类型会让人感到困惑.我可以想到有几种方法可以解决这个问题.我可以将返回类型重命名为更通用的类型:
public GenericResult getJellyBeanReport();
public GenericResult getChocolateBarReport();
Run Code Online (Sandbox Code Playgroud)
但这会破坏使用API的任何代码.我可以创建一个新的,更准确的命名类,它扩展了更接近新函数的SpecificResult:
public class ChocolateBarResult extends JellyBeanResult{};
public JellyBeanResult getJellyBeanReport();
public ChocolateBarResult getChocolateBarReport();
Run Code Online (Sandbox Code Playgroud)
但这真的非常难看,如果我想再次使用返回类型,问题仍然存在.如何在不破坏使用它们的代码的情况下清理这些签名以减少它们的混乱?
小智 6
将核心功能从JellyBeanResult移动到GenericResult并让JellyBeanResult扩展GenericResult:
public class JellyBeanResult extends GenericResult {}
public JellyBeanResult getJellyBeanReport();
public GenericResult getChocolateBarReport();
Run Code Online (Sandbox Code Playgroud)
或者如果你想完全一致:
public class JellyBeanResult extends GenericResult {}
public class ChocolateBarResult extends GenericResult {}
public JellyBeanResult getJellyBeanReport();
public ChocolateBarResult getChocolateBarReport();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
308 次 |
| 最近记录: |