Java erasure with generic overloading (not overriding)

前端 未结 2 572
旧时难觅i
旧时难觅i 2020-11-27 07:26

I have FinanceRequests and CommisionTransactions in my domain. If I have a list of FinanceRequests each FinanceRequest could contain multiple CommisionTransactions that need

2条回答
  •  情话喂你
    2020-11-27 08:07

    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).

    With code in objects that would be:

    public interface Clawbackable{
        void clawBack()
    }
    
    
    public class CommissionFacade
    {
    
        public  void clawBack(Collection 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.

提交回复
热议问题