根据我的用例,创建自定义数据结构的理想方式




我想,这又是一个"最佳实践"问题,因为我可以想出一些不雅的方法来实现我的用例

我的用例如下
我正在编写一个MethodManager(某种)模块,它帮助最终用户通过UI处理实际的方法(函数)调用
为了这个特定的目的,我有一个methodDefinition类,它是方法(函数)在我的系统中的对象形式
我的methodDefinition成员的简要概述如下

methodName -- String
methodInputs -- ArrayList<String>
methodResultType -- enum(STRING,MAP,LIST)
methodResult  -- <<variable, on the basis of methodResultType>>

现在methodResult是可变的,可以是String、Map或List中的任何一个,基于methodResultType的设置。

我已经创建了一个MethodResultType类来说明MethodResultType,它看起来如下

public enum MethodResultType {
    LIST,
    MAP,
    STRING;
    }

现在我知道我必须写一个类来说明methodResult及其基于methodResultType的变量性质,但我想不出一种非拙劣的方法。

在这方面的任何建议/建议都将不胜感激

谢谢
p1ng

List、Map和String有一个共同的祖先类:java.lang.Object。CCD_ 2因此可以是对象。

您还可以将结果及其类型封装到MethodResult对象中,该对象将提供getType()getValueAsString()getValueAsList()getValueAsMap()等方法。如果值的类型不是该方法返回的类型,那么最后三个方法将抛出IllegalStateException。

最新更新