针对不同型号的一种模型响应

时间:2015-08-18 05:24:12

标签: oop design-patterns groovy

我正在尝试将API端点响应转换/映射到APIResponse.groovy。如果我有像

这样的APIResponse模型,这可以简单地完成

APIResponse.groovy

Integer age
String gender
Integer zip
Education education
String marital_status
Boolean magazine_buyer
Boolean outdoor_and_adventure

我想将它们分组为人口统计,家庭和兴趣购买。像..

Demographics.groovy

Integer age
String gender
Integer zip
...

Household.groovy

Education education
Integer household_income
...

InterestPurchase.groovy

Boolean magazine_buyer
Boolean outdoor_and_adventure
...

我想到了像

这样的东西

APIResponse.groovy

inteface APIResponse implements Household, Demographics, InterestPurchase{

}

但当然,这不起作用,因为接口的成员是最终的和静态的。

1 个答案:

答案 0 :(得分:1)

我不确定您使用什么方法将api响应转换/映射到 APIResponse.groovy 。如果您能提供更多相关信息,我可以提供更好的帮助。

以下是您可以尝试的一些方法:

  1. 使用api响应单独映射 Demographics.groovy,Household.groovy和InterestPurchase.groovy ,然后将这些映射到APIResponse.groovy。

  2. 如果不需要制作APIResponse.groovy界面,则可以 Demographics.groovy,Household.groovy和InterestPurchase.groovy 抽象类。

  3. 如果你想使用接口方法,那么不要声明字段,只需在各自的接口中为这些变量设置 getter和setter方法