使用泛型重载(不覆盖)的 Java 擦除 [英] Java erasure with generic overloading (not overriding)

查看:28
本文介绍了使用泛型重载(不覆盖)的 Java 擦除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的域中有 FinanceRequests 和 CommissionTransactions.如果我有一个 FinanceRequests 列表,每个 FinanceRequest 可能包含多个需要收回的 CommisionTransactions.不要担心这是如何完成的.

I have FinanceRequests and CommisionTransactions in my domain. If I have a list of FinanceRequests each FinanceRequest could contain multiple CommisionTransactions that need to be clawed back. Dont worry how exactly that is done.

下面的类(最底部)让我感觉很模糊和温暖,因为它简洁并且很好地重用了现有的代码.一个问题类型擦除.

The class below (very bottom) makes me feel all fuzzy and warm since its succint and reuses existing code nicely. One problem Type erasure.

public void clawBack(Collection<FinanceRequest> financeRequestList)  
public void clawBack(Collection<CommissionTrns> commissionTrnsList)

它们在擦除后都具有相同的签名,即:

They both have the same signature after erasure, ie:

Collection<FinanceRequest> --> Collection<Object>  
Collection<CommissionTrns> --> Collection<Object>  

所以eclipse抱怨:
方法clawBack(Collection) 与CommissionFacade 类型中的另一个方法具有相同的擦除clawBack(Collection)

So eclipse complainst that:
Method clawBack(Collection) has the same erasure clawBack(Collection) as another method in type CommissionFacade

有什么建议可以重构它,使其仍然是一个优雅的解决方案,可以很好地重用代码?

Any suggestions to restructure this so that it still an elegant solution that makes good code reuse?

public class CommissionFacade
{
    /********FINANCE REQUESTS****************/
    public void clawBack(FinanceRequest financeRequest)
    {
        Collection<CommissionTrns> commTrnsList = financeRequest.getCommissionTrnsList();           
        this.clawBack(commTrnsList);
    }

    public void clawBack(Collection<FinanceRequest> financeRequestList)
    {
        for(FinanceRequest finReq : financeRequestList) 
        {
            this.clawBack(finReq);
        }           
    }

    /********COMMISSION TRANSACTIOS****************/
    public void clawBack(CommissionTrns commissionTrns)
    {
        //Do clawback for single CommissionTrns         
    }

    public void clawBack(Collection<CommissionTrns> commissionTrnsList)
    {
        for(CommissionTrns commTrn : commissionTrnsList) 
        {
            this.clawBack(commTrn);
        }
    }

}

推荐答案

要么重命名方法,要么使用多态:使用接口,然后要么将回拨代码放在对象本身中,要么使用双调度(取决于你的设计范式和品味).

Either rename the methods, or use polymorphism: use an interface, and then either put the clawback code in the objects themselves, or use double-dispatch (depending on your design paradigm and taste).

对象中的代码:

public interface Clawbackable{
    void clawBack()
}


public class CommissionFacade
{

    public <T extends Clawbackable> void clawBack(Collection<T> objects)
    {
        for(T object: objects) 
        {
            object.clawBack();
        }           
    }
}

public class CommissionTrns implements Clawbackable {

    public void clawback(){
       // do clawback for commissions
    }
}

public class FinanceRequest implements Clawbackable {

    public void clawBack(){
      // do clwaback for FinanceRequest
    }

}

我更喜欢这种方法,因为我相信您的域应该包含您的逻辑;但我不完全了解你的确切愿望,所以我会留给你.

I prefer this approach, since I'm of the belief your domain should contain your logic; but I'm not fully aware of your exact wishes, so I'll leave it up to you.

通过双重调度,您可以将ClawbackHandler"传递给回拨方法,并在处理程序上根据类型调用适当的方法.

With a double dispatch, you would pass the "ClawbackHandler" to the clawback method, and on the handler call the appropriate method depending on the type.

这篇关于使用泛型重载(不覆盖)的 Java 擦除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆