问题描述
我正在为服务编写客户端包装。 哪些已经给客户端实现。 具有两个类的层次结构:
- 帐户
- 产品
这两个类不共享公共的父接口。 但是他们在使用它们时具有类似的实现:
一段代码执行的例子
帐户服务需要以下代码步骤:
final AccountsCustomBatchRequest content = new AccountsCustomBatchRequest();
content.setEntries(request);
final AccountsCustomBatchResponse response = this.service.accounts().custombatch(content).setDryRun(this.isDryRun)
.execute();
return response.getEntries();
产品类似:
final ProductsCustomBatchRequest content = new ProductsCustomBatchRequest();
content.setEntries(request);
final ProductsCustomBatchResponse response = this.service.products().custombatch(content).setDryRun(this.isDryRun)
.execute();
return response.getEntries();
有没有办法概括代码? 我不能使用通用模式,因为它们都不支持公共父类。 我可以喜欢:
T extends Account or Product ?
1楼
他们共享这种方法: custombatch(content)
我将创建一个包含custombatch
方法的接口,并进行类似T extends custombatchInterface
2楼
如果两个类不共享相同的层次结构,则无法将它们组合在一起。
一个可能的方法是创建一个新的接口CustomBatchInterface
与方法custombatch(content)
,并创建两个classes
,其extends
的ProductsCustomBatchResponse
和AccountsCustomBatchResponse
也implements
您的CustomBatchInterface
。