简体   繁体   English

通用重载(不覆盖)的Java擦除

[英]Java erasure with generic overloading (not overriding)

I have FinanceRequests and CommisionTransactions in my domain. 我的域中有FinanceRequests和CommisionTransactions。 If I have a list of FinanceRequests each FinanceRequest could contain multiple CommisionTransactions that need to be clawed back. 如果我有一个FinanceRequests列表,则每个FinanceRequest都可以包含多个需要撤回的CommisionTransactions。 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>  

So eclipse complainst that: 所以日食抱怨:
Method clawBack(Collection) has the same erasure clawBack(Collection) as another method in type CommissionFacade 方法clawBack(Collection)具有与类型CommissionFacade中的另一个方法相同的擦除clawBack(Collection)

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). 重命名方法,或使用多态性:使用接口,然后将clawback代码放入对象本身,或使用两次调度(取决于您的设计范例和口味)。

With code in objects that would be: 使用对象中的代码将是:

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. 但我不完全了解您的确切意愿,因此请您自行决定。

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. 使用双重调度,您可以将“ ClawbackHandler”传递给clawback方法,并在处理程序上根据类型调用适当的方法。

I think your best option is to simply name the method differently. 我认为您最好的选择是简单地以不同的方式命名该方法。

public void clawBackFinReqs(Collection<FinanceRequest> financeRequestList) {

}

public void clawBackComTrans(Collection<CommissionTrns> commissionTrnsList) {

}

In fact, it's not too bad, since you don't get anything extra out of having the same name on them. 实际上,这还不错,因为在它们上使用相同的名称不会给您带来任何额外的好处。

Keep in mind, that the JVM will not decide which method to call at runtime. 请记住,JVM 不会决定在运行时调用哪种方法。 As opposed to virtual methods / method overriding resolution of overloaded methods are done at compile time. 与虚拟方法/方法相反,重载方法的重载解析是在编译时完成的。 The Java Tutorials on method overloading even points out that "Overloaded methods should be used sparingly..." . 有关方法重载的Java教程甚至指出“应谨慎使用重载的方法...”

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

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