Search code examples
c#.netlistfunctioncommand-pattern

Create list of Methods to run on Dynamic Objects


So let's say I have some number methods that are already defined for a class.

For Example:

private int X(int a, int b)
{
    return a + b;
}

private int Y(int a, int b)
{
    return a - b;
}

private int Z(int a, int b)
{
    return a * b;
}

Now the standard way to take an input in the main body of the class and run it through all the functions would obviously be to just call every function like this:

public void processNormal(int a, int b)
{
    //accumulate results
    int acc = 0;
    if (true)
    {
        //if want to add to current
        acc += X(a, b);
        acc += Y(a, b);
        acc += Z(a, b);
    }
    else
    {
        //if want to replace current if larger
        int k = X(a, b);
        if (k > acc)
            acc = k;

        k = Y(a, b);
        if (k > acc)
            acc = k;

        k = Z(a, b);
        if (k > acc)
            acc = k;
    }
}

Now I was curious, since as the number of functions grows, depending on how many things you may want to do with the function results grows, if that doesn't become a bit cumbersome. And you could instead create a static list of the functions you'll want to run, and then use that in a for loop to shorten things down, which I would imagine would look something like this:

//List with all methods
List<Object> methods = new List<Object>();
//add all methods to list

public void processWithList(int a, int b)
{
    //accumulate results
    int acc = 0;
    foreach (Object j in methods)
    {
        if (true)
        {
            //if want to add to current
            acc += j(a, b);
        }
        else
        {
            //if want to replace current if larger
            int k = j(a, b);
            if (k > acc)
                acc = k;
        }
    }
}

Now with this second process, you still have to define the list somewhere, but I imagine it is easier to keep track of as things grow. All the functions would take the same inputs, and return the same objects so in that regard it's not an issue.

I'm basically wondering if

A) I'm stupidly over complicating things and if this would ever be a useful thing to do.

B) What would such a thing look like in C#? Could I use the predefined class methods in a list or would I have to generate the list in the class' initialization with a bunch of delegate functions?

Thank you for your time.


Solution

  • Am I stupidly over complicating things and if this would ever be a useful thing to do?

    No, You're doing just fine. This is a common approach.

    What would such a thing look like in C#?

    I think the simplest option that matches your sample code is using a List<Func<int, int, int>>.

    Here is a sample implementation:

    public class YourClass
    {
        private List<Func<int, int, int>> methods;
    
        public yourClass()
        {
            methods = new List<Func<int, int, int>>()
            {
                (a,b) => X(a,b),
                (a,b) => Y(a,b),
                (a,b) => Z(a,b)
            };
    
        }
    
        public int SumMethods(int a, int b)
        {
            var result = 0;
            foreach(var m in methods)
            {
                result += m(a, b);
            }
    
            return result;
        }
    
        private int X(int a, int b) { throw new NotImplementedException(); }
        private int Y(int a, int b) { throw new NotImplementedException(); }
        private int Z(int a, int b) { throw new NotImplementedException(); }
    
    }