Say I have two methods void chargeToIndividual(int amount, int account) and void chargeToCompany(int amount, int account). Say I have another method called void processSale(String item, Customer c). What can I do so I can pass either chargeToIndividual or chargeToCompany as an argument to processSale and how would I call it?
For example I want to be able to do
if(isIndividual(someCustomer))
{
processSale(anItem, someCustomer, chargeToIndividual)
}
else if(isCompany(someCustomer))
{
processSale(anItem, someCustomer, chargeToCustomer)
}
and inside processSale() how would I actually call chargeToIndividual or chargeToCustomer()?
You have two functions:
void chargeToIndividual(int amount, int account);
void chargeToCompany(int amount, int account);
What they have in common is that they both take two int parameters and return void. There's no functional interface in java.util.function that matches this shape. But it's easy to define our own:
interface IntIntConsumer {
void accept(int amount, int account);
}
You'd rewrite the calling code as follows:
if (isIndividual(someCustomer)) {
processSale(anItem, someCustomer, MyClass::chargeToIndividual);
} else if (isCompany(someCustomer)) {
processSale(anItem, someCustomer, MyClass::chargeToCompany);
} else { ... }
Or if chargeToIndividual and chargeToCustomer are instance methods, possibly use this::chargeToIndividual and this::chargeToCustomer. An alternate formulation would be to store the charging function in a local variable. Then you'll have only one call to processSale:
IntIntConsumer chargeFunc;
if (isIndividual(someCustomer)) {
chargeFunc = this::chargeToIndividual;
} else if (isCompany(someCustomer)) {
chargeFunc = this::chargeToCompany;
} else { ... }
processSale(anItem, someCustomer, chargeFunc);
Now in processSale the call to the charging function would look like this:
void processSale(Item item, Customer customer, IntIntConsumer func) {
...
func.accept(item.getAmount(), customer.getAccount());
...
}
Of course I've made some assumptions about where to get the amount and account arguments, but I think you can get the idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With