在Java中,我如何根据参数返回两种不同类型的值?

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

In Java, how can I return two different types based on an argument?

问题

假设我有一个像这样的方法:

  1. public AbstractContractApplication acceptOffer(AcceptedOfferDto dto)
  2. {
  3. // 各种应用程序逻辑
  4. Contract contract; // 与上述代码深度集成
  5. return processedApplication; // 也与上述代码深度集成
  6. }

我想重构我的方法,根据AcceptedOfferDto中提供的参数,有时返回contract而不是processedApplication。理想情况下是这样的:

  1. if (AcceptedOfferDto.getReturnConfig() == 1){
  2. return contract;
  3. }
  4. else {
  5. return processedApplication;
  6. }

有没有一种干净的方法来做到这一点,而不涉及复制和粘贴函数中大部分代码,或者重构大块极其重要的业务逻辑?

目前的思路是创建一个包含两个对象实例的类,然后从中选择正确的值。还有哪些其他选项可用?

英文:

Say that I have a method like this:

  1. public AbstractContractApplication acceptOffer(AcceptedOfferDto dto)
  2. {
  3. // various application stuff
  4. Contract contract; // deeply integrated into the above code
  5. return processedApplication; // also deeply integrated into the above code
  6. }

I would like to refactor my method to sometimes return contract instead of processedApplication based on an argument provided in the AcceptedOfferDto. Ideally something like this:

  1. if (AcceptedOfferDto.getReturnConfig() == 1){
  2. return contract;
  3. }
  4. else {
  5. return processedApplication;
  6. }

Is there a clean way to do this which does not involve copy and pasting the bulk of the code in the function or refactoring a large block of extremely important business logic?

The current line of thinking is to create a class that contains instances of both objects and just select the correct value from that. What other options might be available?

答案1

得分: 3

假设 ContractAbstractContractApplication 没有共同的有用特性可以依赖多态性,而且您无法进行太多的重构,您可以使用以下方法:

  1. public interface EitherOne<A, B> {
  2. public default boolean isA() { return false; }
  3. public default boolean isB() { return false; }
  4. public default A getA() { throw new IllegalStateException(); }
  5. public default B getB() { throw new IllegalStateException(); }
  6. public static <A, B> EitherOne<A, B> a(A a) {
  7. return new EitherOne<>() {
  8. @Override public boolean isA() { return true; }
  9. @Override public A getA() { return a; }
  10. };
  11. }
  12. public static <A, B> EitherOne<A, B> b(B b) {
  13. return new EitherOne<>() {
  14. @Override public boolean isB() { return true; }
  15. @Override public B getB() { return b; }
  16. };
  17. }
  18. }

我真的不建议任何人使用这种方法。它应该只用作复杂情况下的一个快速且不太正规的应急解决方案。

接下来,您可以尝试这样做:

  1. public EitherOne<Contract, AbstractContractApplication> acceptOffer(AcceptedOfferDto dto) {
  2. // 各种应用程序逻辑
  3. AbstractContractApplication processedApplication = ...;
  4. Contract contract = ...; // 与上述代码深度集成
  5. if (AcceptedOfferDto.getReturnConfig() == 1) return EitherOne.a(contract);
  6. return EitherOne.b(processedApplication); // 也与上述代码深度集成
  7. }
英文:

Supposing that Contract and AbstractContractApplication have nothing useful in common to rely on polimorphism and you can't refactor too much things, you could use this:

  1. public interface EitherOne&lt;A, B&gt; {
  2. public default boolean isA() { return false; }
  3. public default boolean isB() { return false; }
  4. public default A getA() { throw new IllegalStateException(); }
  5. public default B getB() { throw new IllegalStateException(); }
  6. public static &lt;A, B&gt; EitherOne&lt;A, B&gt; a(A a) {
  7. return new EitherOne&lt;&gt;() {
  8. @Override public boolean isA() { return true; }
  9. @Override public boolean getA() { return a; }
  10. };
  11. }
  12. public static &lt;A, B&gt; EitherOne&lt;A, B&gt; b(B b) {
  13. return new EitherOne&lt;&gt;() {
  14. @Override public boolean isB() { return true; }
  15. @Override public boolean getB() { return b; }
  16. };
  17. }
  18. }

I really don't recommend anyone using this. It should be used only as a quick and dirty escape hatch hack for complicated situations.

Then, you can try this:

  1. public EitherOne&lt;Contract, AbstractContractApplication&gt; acceptOffer(AcceptedOfferDto dto) {
  2. // various application stuff
  3. AbstractContractApplication processedApplication = ...;
  4. Contract contract = ...; // deeply integrated into the above code
  5. if (AcceptedOfferDto.getReturnConfig() == 1) return EitherOne.a(contract);
  6. return EitherOne.b(processedApplication); // also deeply integrated into the above code
  7. }

答案2

得分: -1

使用多态性。
contract(合同)和processedApplication(处理后的申请)的类型必须是AbstractContractApplication(抽象合同申请)的子类型。

英文:

Use Polymorphism.
The type of contract (Contract) and processedApplication (?) must be subtypes of AbstractContractApplication.

huangapple
  • 本文由 发表于 2020年3月4日 06:18:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/60516351.html
匿名

发表评论

匿名网友

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

确定