处理常量数据的最佳方式?(C#)

本文关键字:方式 最佳 常量 数据 处理 | 更新日期: 2023-09-27 18:37:18

管理依赖于一个或多个条件变量(int,float,string等)的常量数据的最佳通用方法(速度和处理)是什么?

基本示例:

Car CreateCar(int brand)
{
    //...
    float colourValue = GetRandomFloat(0F, 1F);
    car.Coulor = GetCarCoulor(brand, colourValue);
    //...
}
//-------------
//Example for data functions:
//-------------
string GetCarCoulor(int brand, float colourValue)
{
    if (brand == 0)
    {
        if (colourValue < 0.3)
            return "red";
        else if (colourValue < 0.7)
            return "green";
        else
            return "blue";
    }
    else if (brand == 1)
    {
        if (colourValue < 0.2)
            return "red";
        else
            return "blue";
    }
    //...
    return "default";
}
float GetCarSpeed(int brand, int carModel, int gear, bool returnMin)
{
    if (brand == 0)
    {
        if (carModel == 0)
        {
            if (gear == 1)
            {
                if (returnMin)
                    return 1F;
                else
                    return 15F;
            }
            else if (gear == 2)
            {
                if (returnMin)
                    return 15F;
                else
                    return 40F;
            }
        }
        //...
    }
    else if (brand == 1)
    {
        //...
    }
    //...
    return 0F;
}

带有 if-else 构造的函数显然是最基本的形式,适用于大多数数据,但不一定看起来/处理并且对于较大的数据集表现良好。否则,会想到Dictionary<T1,T2>switch结构,但这些结构不适用于多个或某些类型的输入变量。

那么,是否有任何其他类型的构造/方法来查找常量数据或某种最佳实践规则?

我还读到使用XML文件进行数据存储。对于此类数据,这通常是可取的,还是速度较慢或增加了太多的复杂性?优点/缺点?

编辑:扩展示例以显示我想到的数据功能类型。

处理常量数据的最佳方式?(C#)

作为另一种方法,只要输入类型保持不变,您就可以拥有"规则"字典。

我实际上不建议这样做(请参阅下面的注释),但这是一种可能性。

void Main()
{
    List<RuleResultPair> rules = new List<RuleResultPair>();
    rules.Add(new RuleResultPair 
    {
        Rule = (input) => input.A == 0 && input.B < 0.3, 
        Result = "output1.1"
    });
    rules.Add(new RuleResultPair
    {
        Rule = (input) => input.A == 0 && input.B < 0.7,
        Result = "output1.2"
    });
    var test1Point1 = new Input
    {
        A = 0,
        B = 0.2F
    };
    var test1Point2 = new Input
    {
        A = 0,
        B = 0.7F
    };

    rules.First(w => w.Rule(test1Point1)).Result.Dump(); //outputs output1.1
    rules.First(w => w.Rule(test1Point2)).Result.Dump(); //outputs output1.2
}
// Define other methods and classes here
public struct Input
{
    public int A;
    public float B;
}
public class RuleResultPair
{
    public Predicate<Input> Rule;
    public string Result;
}

http://share.linqpad.net/pqouhc.linq

我相信这段代码可以大大改进,但出于示例的目的,我想保持它相对简单。

作为一般说明:

看起来这可能会导致维护问题。通常 switch 语句块或 if 语句有点代码气味。

看起来好像要扩展您的应用程序,您必须修改一个巨大的开关,if/else 块。

也许策略模式可以帮助您分解算法。但这取决于。

编辑:


从 LiWa 需要改变输入的其他示例中,这里有一篇有用的博客文章,描述了将 switch 语句重构为单独的算法:应用策略模式而不是使用 switch 语句

如果将多个输入变量封装在一个具有适当相等语义(包括哈希代码)的单个类中,字典仍然可以很好(您必须始终定义与相等语义兼容的哈希代码语义)。

我过去发现一些模式很有用,不能涵盖所有情况,但你可以在这里找到一些东西......

常量中的资源

当您有一个或多个输出作为单个值的函数(作为开关)时,您可以使用资源。

return ResourceNamespace.ResxFileName.ResourceManager.GetString("ResourceKey")

字符串组合:当结果可以通过代码的"串联"来确定时,您可以删除资源

public string Decode(string a, string b, string c)
{
    return ResourceNamespace.ResxFileName.ResourceManager.GetString(a + b + c);
}