Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Design Pattern for Passing a Large Number of Parameters

I have a reasonably-sized class that implements several logically-related algorithms (from graph theory). About 10-15 parameters are required as input to the algorithm. These are not modified by the algorithm, but are used to guide the operation of it. First, I explain two options for implementing this. My question is what is a common way to do so (whether it is or isn't one of the two options).

I personally don't like to pass these values as parameters to the function when N is large, especially while I'm still developing the algorithm.

void runAlgorithm(int param1, double param2, ..., bool paramN);

Instead I have a class Algorithm that contains the algorithms, and I have a struct AlgorithmGlobals that contains these parameters. I either pass this struct to:

void runAlgorithm(AlgorithmGlobals const & globals);

Or I add a public AlgorithmGlobals instance to the class:

class Algorithm {
public:
    AlgorithmGlobals globals;
    void runAlgorithm();
}

Then elsewhere I'd use it like this:

int main() {
    Algorithm algorithm;
    algorithm.globals.param1 = 5;
    algorithm.globals.param2 = 7.3;
    ...
    algorithm.globals.paramN = 5;

    algorithm.runAlgorithm();

    return 0;
}

Note that the constructor of AlgorithmGlobals defines good defaults for each of the parameters so only the parameters with non-default values need to be specified.

AlgorithmGlobals are not made private, because they can be freely modified before the runAlgorithm() function is called. There is no need to "protect" them.

like image 936
Alan Turing Avatar asked Apr 05 '11 12:04

Alan Turing


3 Answers

This is called the "Parameter object" pattern, and it's generally a good thing. I don't like the member version, especially calling it "XGlobals" and implying that it's shared all over the place. The Parameter Object pattern instead generally involves creating an instance of the Parameter Object and passing it as a parameter to a function call.

like image 191
Ernest Friedman-Hill Avatar answered Oct 18 '22 20:10

Ernest Friedman-Hill


Others have mentioned Parameter Object, but there is also another possibility: using a Builder.

Builder allows you to omit the parameters whose default values are suitable, thus simplifying your code. This is especially handy if you are going to use your algorithm with several different sets of parameters. OTOH it also allows you to reuse similar sets of parameters (although there is a risk of inadvertent reuse). This (together with method chaining) would allow you to write code such as

Algorithm.Builder builder;

Algorithm a1 = builder.withParam1(1).withParam3(18).withParam8(999).build();
...
Algorithm a2 = builder.withParam2(7).withParam5(298).withParam7(6).build();
like image 24
Péter Török Avatar answered Oct 18 '22 20:10

Péter Török


You have several different ideas that you should be suggesting with your design:

  1. The parameters are purely inputs.
  2. The parameters are specific to your algorithm.
  3. The paramaters have default values that are sane.

class Algorithm {
  public:
    class Parameters { // Nested class, these are specific to your algorithm.
      public:
        Parameters() : values(sensible_default) { }
        type_t values; // This is all about the data.
    };

    Algorithm(const Parameters &params) : params_(params) { }

    void run();

  private:
    const Parameters params_; // Paramaeters don't change while algorithm
};                            // is running. 

This is what I would suggest.

like image 21
Omnifarious Avatar answered Oct 18 '22 19:10

Omnifarious