Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9241085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:18:22+00:00 2026-06-18T08:18:22+00:00

We have a system which we use to charge the customers for different type

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:18:23+00:00Added an answer on June 18, 2026 at 8:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application in which we use a hand-made build system.The reason for
I have a MFC dialog based application in which I use ::system() function to
I have a system which is receiving log files from different places through http
I have a system which contains multiple applications connected together using JMS and Spring
I am creating a generic app which will have different builds for different customers.
I have a system which is using Spring for dependency injection. I use annotation-based
I have non system headers, which I use to compile a program via SCons.
I have a system which receives leads (they are piped in via email) and
Currently we have a system which we paid for that pushes information into Excel
I need suggestion about YAMI library . I have a system which receives Json

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.