What if classes with the same interface have a similar but different method signature?
Let's say I have a project for calculating various costs (finally, to get the full cost).
My program has several calculator classes, namely ACostCalculator , BCostCalculator , etc. When the calculate() method is called to calculate the cost, the cost container is also transferred to these cost calculators. In a good scenario, I can create a CostCalculator interface for each cost calculator.
However, different resources were required to calculate different costs. In my current program, it looks like this:
//getResource() are costly method while several costs need this. So do it outside calculate() method. ResourceA resourceA = getResourceA(); ResourceB resourceB = getResourceB(); CostContainer costContainer = new CostContainer(); CostCalculator aCostCalculator = new ACostCalculator(); ... CostCalculator eCostCalculator = new ECostCalculator(); aCostCalculator.calculate(costContainer); bCostCalculator.calculate(costContainer) cCostCalculator.calculate(costContainer, resourceA); dCostCalculator.calculate(costContainer, resourceA); eCostCalculator.calculate(costContainer, resourceA, resourceB);
If the signature is exactly the same, I can make the loop convenient for this right away. However, since they are similar, but different, I can’t even create a good interface.
I am not sure if there are good ways to do this. What I can imagine is a generalization of the entire calculate() method to
calculate(CostContainer costContainer, List<Object> resources);
Any ideas? Thanks for answering.