代码之家  ›  专栏  ›  技术社区  ›  Steven Evers

如何清理此API的建议

  •  1
  • Steven Evers  · 技术社区  · 14 年前

    对于一个有趣的项目,我正在尝试实现BitTorrent规范,现在我正在研究它的bencoding部分。

    编码基本上可以从int/string/dictionary->字符串进行编码以进行传输。我已经将所有不同的编码编写/测试/工作为重载编码(…)方法,并将各个解码方法编写/测试/工作为解码字符串(…),解码(…)等。

    我想不出一种方法可以让所有编码都有一个解码方法,以便尽可能保持编码/解码的API的清洁(目前有两个公共方法,top)。

    请注意,我有一个方法可以获得解码后的字符串将具有的结果类型。

    客户端代码,现在每次他们想要解码消息时都必须看起来像这样:

    string s = ...; // Encoded string
    Type t = Encoder.GetDecodedType(s);
    if (t == typeof(int))
        process(Encoder.DecodeInt(s));
    else if (t == typeof(string))
        process(Encoder.DecodeString(s));
    else if (t == typeof(Dictionary<string, string>))
        process(Encoder.DecodeStringDictionary(s));
    else if (t == typeof(Dictionary<string, int>))
        process(Encoder.DecodeIntDictionary(s)):
    

    我想把它清理得更像:

    string s = ...; // Encoded string
    process(Encoder.Decode(s));
    

    其中,在这两种情况下,进程(…)都可能在客户端使用4种类型的解码值重载函数。

    4 回复  |  直到 14 年前
        1
  •  3
  •   Timwi    14 年前

    你可以让DLR为你做这件事。

    public static void Process(int i) { ... }
    public static void Process(string s) { ... }
    public static void Process(Dictionary<string, string> dic) { ... }
    public static void Process(Dictionary<string, int> dic) { ... }
    
    [...]
    
    public dynamic Decode(string input)     // or 'object' if you prefer
    {
        var t = GetDecodedType(input);
        if (t == typeof(int))
            return DecodeInt(input);
        else if (t == ...)
            // ...
    }
    
    [...]
    
    string s = ...; // Encoded string
    Process(Encoder.Decode(s));            // if you used 'dynamic' above
    Process((dynamic)Encoder.Decode(s));   // if you used 'object' above
    
        2
  •  1
  •   Joel Martinez    14 年前

    如果您正在编写库/框架…这将是您努力工作中最宝贵的资源:—)我有硬拷贝,并从封面到封面阅读:
    Design Guidelines for Developing Class Libraries 来自Microsoft

        3
  •  0
  •   Woot4Moo    14 年前

    我想说你应该遵循里斯科夫替代原则 here 并为每个数据类型创建一个方法。这样,当您开始传递自定义对象时,就不会一直增加使用typeof的痛苦。再次阅读完问题后,您已经知道要传递给它的类型,从而进一步支持删除typeof操作的需要。

        4
  •  0
  •   Aaron McIver    14 年前

    我很困惑。为什么不简单地在public decode方法中执行getdecodedType逻辑并确定类型,然后在确定后进行不同的调用呢?