简体   繁体   English

如何设计此方法的签名

[英]How to design the signature of this method

I need to define a method, which takes a request and returns a response: 我需要定义一个方法,该方法接受一个请求并返回一个响应:

  public MyResponse submit(MyRequest request){                      A
      ....
  }

This looks simple. 这看起来很简单。 However: 然而:

MyRequest can has a number of child classes MyRequest可以有许多子类

 public class HolidayRequest extends MyRequest {}
 public class FundingRequest extends MyRequest {}
 public class BudgetRequest extends MyRequest {}

The same applies to the response class: 这同样适用于响应类:

 public class HolidayResponse extends MyResponse {}
 public class FundingResponse extends MyResponse {}
 public class BudgetResponse extends MyResponse {}

A user of the method submit can pass in a request of any type and get a corresponding response object, eg: Pass in HolidayRequest will return HolidayResponse. 该方法的用户submit可以通过在任何类型的请求,并获得相应的响应对象,例如:在HolidayRequest传递将返回HolidayResponse。

Although the interface defined above still works, the user has to cast the return value of submit from MyResponse to a specific response type. 尽管上面仍然定义的接口工作,用户必须投的返回值submit从MyResponse到一个特定的响应类型。

I then think to change the method to: 然后,我认为将方法更改为:

  public <T extends MyResponse, S extends MyRequest>                B
  T submit(S request){
      ....
  }

This allows the return type to be a specific type rather than the parent type. 这使返回类型可以是特定类型,而不是父类型。 However, a user can still write code like: 但是,用户仍然可以编写如下代码:

  HolidayResponse response = submit(new FundingRequest());

and the code will still compile but fail at runtime. 并且代码仍将编译,但在运行时失败。

Then I realise the submit method does not need to know which type of request it is, so there is no need to use type parameter for MyRequest, I think I can maybe further simplify it to: 然后,我意识到Submit方法不需要知道请求的类型,因此无需为MyRequest使用类型参数,我想我可以将其进一步简化为:

  public <T extends MyResponse>                                    C
  T submit(MyRequest request){
      ....
  }

But again, this one has the same problem as mentioned above, user can pass in a request type and expect another response type. 但是同样,该问题与上面提到的问题相同,用户可以传递请求类型并期望其他响应类型。

My question is that how I shall properly define the signature of this method? 我的问题是如何正确定义此方法的签名? Which one of the A, B and C definition above will you use? 您将使用上面A,B和C定义中的哪一个? Or there might be more appropriate way to define it? 还是可能有更合适的方法来定义它?

Thank you very much. 非常感谢你。

You have to link your Request to your Response . 您必须将您的Request到您的Response

For example, change the MyRequest class to 例如,将MyRequest类更改为

public abstract class MyRequest<T extends MyResponse> {

Now your HolidayRequest would look like: 现在,您的HolidayRequest如下所示:

public class HolidayRequest extends MyRequest<HolidayResponse> {

Now your method signature can force the MyReponse to correspont: 现在,您的方法签名可以强制MyReponse进行MyReponse

public <R extends MyResponse, S extends MyRequest<R>> R submit(S request){

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM