C++传递大量参数的设计模式
我有一个大小合理的类,它实现了几种逻辑相关的算法(来自图论)。算法需要大约 10-15 个参数作为输入。这些不被算法修改,而是用于指导算法的操作。首先,我解释实现此目的的两个选项。我的问题是这样做的常见方法是什么(无论是还是不是这两个选项之一)。
当 N 很大时,我个人不喜欢将这些值作为参数传递给函数,尤其是当我仍在开发算法时。
void runAlgorithm(int param1, double param2, ..., bool paramN);
相反,我有一个包含算法的类Algorithm
,并且我有一个包含这些参数的结构AlgorithmGlobals
。我要么将此结构传递给:
void runAlgorithm(AlgorithmGlobals const & globals);
要么将一个公共 AlgorithmGlobals 实例添加到类中:
class Algorithm {
public:
AlgorithmGlobals globals;
void runAlgorithm();
}
然后在其他地方我会像这样使用它:
int main() {
Algorithm algorithm;
algorithm.globals.param1 = 5;
algorithm.globals.param2 = 7.3;
...
algorithm.globals.paramN = 5;
algorithm.runAlgorithm();
return 0;
}
请注意,AlgorithmGlobals
的构造函数为每个参数定义了良好的默认值,因此仅需要指定非默认值的参数。
AlgorithmGlobals
未设为私有,因为它们可以在调用 runAlgorithm()
函数之前自由修改。没有必要“保护”他们。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这称为“参数对象”模式,这通常是一件好事。我不喜欢会员版本,特别是称其为“XGlobals”并暗示它在各处共享。相反,参数对象模式通常涉及创建参数对象的实例并将其作为参数传递给函数调用。
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.
其他人提到了参数对象,但还有另一种可能性:使用生成器。
Builder 允许您省略默认值合适的参数,从而简化您的代码。如果您要将算法与几组不同的参数一起使用,这尤其方便。 OTOH 它还允许您重用类似的参数集(尽管存在无意重用的风险)。这(与方法链接一起)将允许您编写如下代码
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
您应该在设计中提出几种不同的想法:
这就是我的建议。
You have several different ideas that you should be suggesting with your design:
This is what I would suggest.
我使用您已经提到过的这种技术:
但会调用类
AlgorithmParams
来代替。I use this technique that you already mentioned:
But would call the class
AlgorithmParams
instead.命名参数习惯用法在这里可能有用。
The Named Parameter Idiom might be useful here.
建议你为什么不这样做:
现在你可以这样使用它:
suggestion Why don't you do this instead:
Now you can use it as such: