Java如何管理用户定义的变量 [英] Java how to manage user-defined variables

查看:101
本文介绍了Java如何管理用户定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一些软件,希望允许用户创建和管理自己的变量。我想让用户能够创建双精度,整数和字符串,然后手动设置它们的值(所有这些都将通过UI完成)。现在我正试着决定一个模型来管理所有这些用户定义的变量。



我首先想到的是,拥有一个Map通过他们的名字获取变量,并使用通用的变量类...

  Map< ;字符串,变量> myVars; 

我想要定义类似这样的变量类...

  public class变量< T> 
{

私人T值;

public void setValue(T value)
{
this.value = value;
}

public T getValue()
{
返回值;
}

}

然而,我认为这种方法需要我把地图定义为 Map< String,Variable< T> > 但我不想为整个地图选择单一类型 T ,而是允许地图包含整数组合,双打和字符串。

第二个想法是创建一个容器类,它维护三个不同的映射,每个映射对应于字符串,整数和双精度。这三个地图与调用者隔离,调用者只需要向getter提供一个String(变量名),容器将从相应的地图获取该变量的值。该容器还可以处理诸如维护所有三张地图上的键的唯一性等问题。



一个存根可能看起来像这样...

  public class VariableContainer {

private Map< String,Double> doubleVars = new HashMap();
私人地图< String,String> stringVars = new HashMap();
私人地图< String,Integer> intVars = new HashMap();
$ b $ public Object get(String varName)
{
if(doubleVars.containsKey(varName)){
return doubleVars.get(varName);
} else if(stringVars.containsKey(varName)){
return stringVars.get(varName);
} else if(intVars.containsKey(varName)){
return intVars.get(varName)
} else {
// varName not found
}


但是,现在 get 方法必须使用 instanceof 来确定返回的对象类型。



在这一点上,如果需要对调用者进行检查,也许最简单的方法就是坚持使用

 地图< String,Object> 

忘记抽象变量类或一个容器类。

也许还有一种选择是返回一个通配符,如这个问题的答案中所述:
避免返回通配符类型



有没有人有任何想法或经验方法来解决这个问题?

解决方案

确定以下内容可以正常工作:

  public class Variable< T> 
{

私人T值;

public void setValue(T value)
{
this.value = value;
}

public T getValue()
{
返回值;


public String toString()
{
return value.toString();
}
}

映射< String,Variable<>> myVars = new HashMap<>();

变量< Double> v1 =新变量<>();
v1.setValue(3.0);

变量< String> v2 =新变量<>();
v2.setValue(hello);

myVars.put(doubleVar,v1);
myVars.put(stringVar,v2);

System.out.println(doubleVar =+ myVars.get(doubleVar));
System.out.println(stringVar =+ myVars.get(stringVar));

以上印刷品:

doubleVar = 3.0



stringVar = hello


I'm developing some software where I'd like to allow the user to create and manage their own variables. I'd like to give the user the ability to create doubles, integers, and strings, and then manually set their values (all of this will be done through a UI). Right now I'm trying to decide on a model that would manage all of these user-defined variables.

My first thought was that it would be nice to have a Map that fetches variables by their name and use a "one-size-fits-all" 'Variable' class...

Map<String, Variable> myVars;

I thought to define the Variable class something like this...

public class Variable<T> 
{

private T value;

public void setValue(T value)
{
    this.value = value;
}

public T getValue()
{
    return value;
}

}

However, I think this approach requires me to define the map as Map<String, Variable<T> > but I don't want to choose a single type T for the entire map, but rather allow the map to contain a combination of integers, doubles, and strings.

A second thought was to create a container class that maintains three different maps, one each for strings, integers, and doubles. The three maps are insulated from the caller, and the caller merely needs to provide a String (the variable name) to a getter and the container would get the variable's value from the appropriate map. The container could also take care of issues like maintaining uniqueness of keys across all three maps.

A stub might look something like this...

public class VariableContainer {

private Map<String, Double> doubleVars = new HashMap();
private Map<String, String> stringVars = new HashMap();
private Map<String, Integer> intVars = new HashMap();

public Object get(String varName)
{
    if (doubleVars.containsKey(varName)) {
        return doubleVars.get(varName);
    } else if (stringVars.containsKey(varName)) {
        return stringVars.get(varName);
    } else if (intVars.containsKey(varName)) {
        return intVars.get(varName)
    } else {
        // varName not found
    }
}
}

However, now the caller of the get method must do inspection using instanceof to determine what kind of Object has been returned.

On that note, if inspection on the part of the caller is required, maybe it would be easiest to just stick with

Map<String, Object>

and forget about an abstract Variable class or a container class.

Perhaps yet another option is to return a wildcard as described in the answer to this question: Avoiding Returning Wildcard Types

Does anyone have any thoughts or experience on a good way to approach this? Thanks for any input.

解决方案

OK the following appears to work:

public class Variable<T> 
{

    private T value;

    public void setValue(T value)
    {
        this.value = value;
    }

    public T getValue()
    {
        return value;
    }

    public String toString()
    {
        return value.toString();
    }
}

Map<String, Variable<?>> myVars = new HashMap<>();

Variable<Double> v1 = new Variable<>();
v1.setValue(3.0);

Variable<String> v2 = new Variable<>();
v2.setValue("hello");

myVars.put("doubleVar", v1);
myVars.put("stringVar", v2);

System.out.println("doubleVar = " + myVars.get("doubleVar"));
System.out.println("stringVar = " + myVars.get("stringVar"));

The above prints:

doubleVar = 3.0

stringVar = hello

这篇关于Java如何管理用户定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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