We have a system which we use to charge the customers for different type of charges.
There are multiple charge types and each charge type includes different charge items.
The below is what I have come up with using the Factory Method, the problem with this one is that I need to be able to pass different parameters to each Calculate function depending on the charge type, how can I achieve this?
//product abstract class
public abstract class ChargeItem
{
public abstract List<ChargeResults> Calculate();
}
//Concrete product classes
public class ChargeType1 : ChargeItem
{
public override List<ChargeResults> Calculate()
{
return new List<ChargeResults> { new ChargeResults { CustomerId = 1, ChargeTotal = 10} };
}
}
public class ChargeType2 : ChargeItem
{
public override List<ChargeResults> Calculate()
{
return new List<ChargeResults> { new ChargeResults { CustomerId = 2, ChargeTotal = 20} };
}
}
public class ChargeType3 : ChargeItem
{
public override List<ChargeResults> Calculate()
{
return new List<ChargeResults> { new ChargeResults { CustomerId = 3, ChargeTotal = 30} };
}
}
//Creator abstract class
public abstract class GeneralCustomerCharge
{
//default constructor invokes the factory class
protected GeneralCustomerCharge()
{
this.CreateCharges();
}
public List<ChargeItem> ChargeItems { get; protected set; }
public abstract void CreateCharges();
}
public class AssetCharges : GeneralCustomerCharge
{
public override void CreateCharges()
{
ChargeItems = new List<ChargeItem> { new ChargeType1(), new ChargeType2() };
}
}
public class SubscriptionCharges : GeneralCustomerCharge
{
public override void CreateCharges()
{
ChargeItems = new List<ChargeItem> { new ChargeType3() };
}
}
public class ChargeResults
{
public int CustomerId { get; set; }
public decimal ChargeTotal { get; set; }
}
And the usage is:
var newAssetCharge = new AssetCharges();
foreach (ChargeItem chargeItem in newAssetCharge.ChargeItems)
{
foreach (var item in chargeItem.Calculate())
{
Console.WriteLine("Asset Charges For Customer Id: {0}, Charge Total: {1}", item.CustomerId, item.ChargeTotal);
}
}
I need to be able to pass different type of parameters to the chargeItem.Calculate() from within the foreach loop depending on the Calculate method I am calling, how can I achieve this?
I was planning to create different charge type parameter classes for each charge type, then determine the charge type and using some if else statements call the Calculate function passing the relevant parameter type but I don't think it is a good idea. Is there a better way of doing this, or is there are another completely different way of achieving what I am trying to do here?
Thanks
It depends. There are going to be a lot of ways to accomplish this, and choosing one will depend on more context than you'll easily be able to share here. Here's a few ideas:
CalculateParams
type to hold all the various arguments,
only use them in seome places. ChargeItem
at construction. The reason you are having this problem is that you are trying to architect from the "middle" out. A good way to get the abstractions you need is to write the code and the tests for the class that depends on those abstractions. Invent methods on the spot that are based on the needs of the tests and production code you are writing, rather than some guess at what those abstractions should look like. This is the best way to enable you to create needs-based abstractions which, as the name suggests, tend to meet your needs best.
Extending the comment from @Ryan Bennett above here's what I think you could do.
Calculator
interface with a PerformCalculation()
method. CalculatorFactory
class with a GenerateCalculator()
method that returns an appropriate Calculator
implementation. The factory constructs the Calculator
implementation object based on the argument passed into to the GenerateCalculator()
method (if/else statements).Calculator
implementation into the abstract Calculate()
method.As mentioned in the comments above you would be injecting the Calculator
on which the Calculate()
method depends. Implementations of the abstract Calculate()
method (of ChargeItem
) would only care about the Calculator
interface.
Individual implementations of Calculator
would ensure that the Calculations happen differently based on your rules.
Hope this helps.
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