处理很多输入参数,很多输出 [英] Dealing with lots of input parameters, lots of outputs

查看:70
本文介绍了处理很多输入参数,很多输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行复杂的计算.就我而言,创建Calculator类(使用策略模式抽象)似乎是最自然的.

I need to perform a complicated calculation. In my case it seemed most natural to create a Calculator class (abstracted using a strategy pattern).

要执行计算,该类需要接受大约20个输入,其中一些是可选的,其中一些将来可能会更改,等等.调用 Calculate()方法后,大约有20个输入需要输出不同的变量.

To perform the calculation the class needs to accept about 20 inputs, some of which are optional, some of which could change in the future etc. Once the Calculate() method is called, about 20 different variables need to be outputted.

有很多方法可以实现.

  • 作为参数传递给计算方法的输入
  • 通过计算器属性传递的输入
  • 将输入包装到自己的类中,然后传递给Calculate()方法.
  • 由Calculate()返回的输出,包装在一个类中
  • 将输出填充到传递给Calculate()方法的参数中
  • 在调用Calculate()之后从计算器的公共属性中检索的输出

所有这些方法各有利弊. 你会怎么做?

There are pros and cons to all these methods. How would you do it?

更新: 感谢您的反馈.

UPDATE: Thanks for the feedback.

此计算器的目的是生成报价.输入的内容包括客户的地址,利率,目标利润,附加费用,产品ID等.输出包括报价,实际利润,更多费用等.

The purpose of this calculator is to generate a quote. The inputs are things such as the customer's address, interest rates, target profit, additional fees, product id etc The output includes the quote, the actual profit, more fees etc.

我已经继续创建了ICalculateInput和ICalculateOutput接口及其具体类,并且该系统现在运行良好.计算器类也继承自ICalculator接口(因为所涉及的计算因来源产品的公司而有很大差异).

I have gone ahead and created ICalculateInput and ICalculateOutput interfaces and their concrete classes, and the system works very well now. The Calculator class also inherits from an ICalculator interface (as the calculations involved differ enormously depending on the company the product is sourced from).

推荐答案

大多数人建议使用参数类"和结果类".我同意这种方法,但是在我看来,您的参数分为几类.也许您可以为必需的参数创建一个参数类,并为可选参数或可选参数组创建一个单独的参数类.这样,您可以根据所需的计算类型创建不同的方法;

Most people suggest using a "Parameter Class", and a "Result Class". I agree with this approach, but it seems to me that your parameters fall into several categories. Perhaps you could create a parameter class for the required parameters, and a separate parameter class for the optional parameters or groups of optional parameters. This way, you could create different methods depending on what kind of calculation you require;

Result calculate(RequiredArgs requiredArgs) {
...
}

Result calculate(RequiredArgs requiredArgs, OptionalArgs optionalArgs) {
}

Result calculate(RequiredArgs requiredArgs, OptionalArgs optionalArgs, OtherOptionalArgs oOpitonalArgs) {
}

这将使您的API易于使用.如果您不想为不同的组创建类,则也可以使用地图,但这将需要计算引擎中的更多验证代码.通常,我更喜欢参数类,但必须对您的特定问题有更多的了解才能决定性.

This will make your API easier to use. If you don't want to create classes for the different groups, you could also use Maps, but this will require more validation code in the calculation engine. Normally I would prefer parameter classes, but would have to know more about your particular problem to be decisive.

由于线程安全和对象可重用性,我不会将计算结果存储在计算引擎本身中.维护无状态的代码要容易得多.

I would not store the calculation result in the calculation engine itself, because of thread safety and object reusability. It's much easier to maintain code that is stateless.

这篇关于处理很多输入参数,很多输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆