Design pattern / Architecture for different charge types for a customer
Asked Answered
T

2

6

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

Thunell answered 25/1, 2013 at 16:46 Comment(9)
When do you know what parameters will be needed? If it is during construction of the ChargeType, you could place the values into properties and have Calculate access those. Otherwise you need to rethink your Calculate method in general. If it requires different parameters to function it should not be an abstract method.Walloon
if you want to do abstract class, then depending on the type of parameters you can use a list or arralistCline
You could also make a Calculate Class that would know how to do calculations for each Charge type. That way you take the responsibility off that class and can encapsulate that logic further. You could have a Calculate class that you inject into each ChargeType as well. Would give you some flexibilityWalloon
I actually know the parameters at the time of creating the charge, so for instance before I do the following call var newAssetCharge = new AssetCharges(); I have the parameters.Thunell
Then each ChargeType could have a constructor taking those Calculate arguments. Then Calculate() would just access those internally.Walloon
Why is your chargetype hardcoded to a customer?Fredric
I think you are looking for generics. public abstract class ChargeItem { public abstract List<ChargeResults> Calculate<U>(U obj); }Babar
Can you please give an example of the parameters, what they mean, and how you'd like to use them?Chronology
@Jordão parameters are types which hold the customer name and a rate used in the calculation formula, so we pass a different type of parameter object to each charge type calculation.Thunell
B
3

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:

  • Create a CalculateParams type to hold all the various arguments, only use them in seome places.
  • Place this information into the ChargeItem at construction.
  • Create a ChargeCalculator that is responsible for both of these pieces of information
  • etc...

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.

Bellis answered 29/1, 2013 at 13:20 Comment(0)
B
1

Extending the comment from @Ryan Bennett above here's what I think you could do.

  1. Create a Calculator interface with a PerformCalculation() method.
  2. Create a 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).
  3. Pass the generated 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.

Bordie answered 29/1, 2013 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.