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

前端 未结 6 918
小鲜肉
小鲜肉 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: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 ¶ms) : params_(params) { }
    
        void run();
    
      private:
        const Parameters params_; // Paramaeters don't change while algorithm
    };                            // is running. 
    

    This is what I would suggest.

提交回复
热议问题