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

前端 未结 6 916
小鲜肉
小鲜肉 2020-12-25 13:44

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. Thes

相关标签:
6条回答
  • 2020-12-25 14:19

    suggestion Why don't you do this instead:

    class Algorithm {
    public:
    Algorithm::Algorithm(AlgorithmGlobals const & globals) : globals_(globals) {}
    
        void runAlgorithm(); // use globals_ inside this function
    
       private:
        const AlgorithmGlobals globals_;
        };
    

    Now you can use it as such:

    AlgorithmGlobals myglobals;
    myglobals.somevar = 12;
    
    Algorithm algo(myglobals);
    
    0 讨论(0)
  • 2020-12-25 14:21

    I use this technique that you already mentioned:

    void runAlgorithm(AlgorithmGlobals const & globals);
    

    But would call the class AlgorithmParams instead.

    0 讨论(0)
  • 2020-12-25 14:34

    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();
    
    0 讨论(0)
  • 2020-12-25 14:35

    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.

    0 讨论(0)
  • 2020-12-25 14:40

    The Named Parameter Idiom might be useful here.

    a.runAlgorithm() = Parameters().directed(true).weight(17).frequency(123.45);
    
    0 讨论(0)
  • 2020-12-25 14:43

    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.

    0 讨论(0)
提交回复
热议问题