Java:项目不相关时有界的异构集合 [英] Java: Bounded heterogeneous collections when items are not related

查看:69
本文介绍了Java:项目不相关时有界的异构集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个异构集合,而我确切地知道我要放置的类型,那么有一种方法可以强制执行此操作。

If i have heterogeneous collection for which I know exactly the types i'm going to place is there a way to enforce this.

例如,以这种情况为例:我有一个具有字符串键和值的映射,该键和值可以是三种不相关的类型。现在我知道我将只放置ClassA和ClassB或java.lang.String

For example take this scenario say i have a map that has a String key and value which can be on of three unrelated types. Now I know that I will only put ClassA and ClassB or java.lang.String

例如,这里是代码

public HetroCollection
{
    public Map<String,Object> values;
}

public ClassA
{
}

public ClassB
{
}

public static void Main(String args[])
{
   HetroCollection collection = new HetroCollection();
   collection.values.add("first", new ClassA());
   collections.values.add("second", new ClassB());
   collections.values.add("third" , "someString");
   //BAD want to stop random adds
   collections.values.("fourth" , new SomeRandomClass());
}

我想到的选择是:


  • 让这些类实现一个通用接口并在Map上使用泛型(问题是如果这还涉及到JDK或第三方的库类,那么更改类就是

  • have the classes implement a common interface and use Generics on the Map (Problem with this is if this also involves library classes either JDK or third party then changing class is not an option

隐藏地图,并提供放置方法,这些方法可以像

hide the Map and provide put Methods which are paratemized like

put(String key,ClassA值);
put(字符串键,ClassB值);
put(字符串键,String值);
get(字符串键);

put(String key , ClassA value); put(String key , ClassB value); put(String key, String value); get(String key);

重新考虑设计,不要使用异构集合(不知道我怎么用其他方式表示)

Rethink design and not use heterogeneous collection (not sure how I would represent this any other way)

为此寻找最佳实践答案。

Looking for the best practice answer for this.

推荐答案

我认为最佳实践解决方案是

I think that the "best practice" solutions are either your first and third options, provided that circumstances allow it.

您尚未考虑的另一个选择是这样的:

Another option that you haven't considered is something like this:

public class MyMap extends HashMap<String, Object> {
    ...
    // constructors
    ...
    @Override
    public void put(String key, Object value) {
        if (value instanceof ClassA || value instanceof ClassB) {
            super.put(key, value);
        } else {
            throw new IllegalArgumentException("Verbotten!");
        }
    }
    ...
}

您可以将其与第二个选项结合使用,以便有一个静态类型的选项,甚至可以将 put(String,Object)方法标记为不建议使用,以防止使用它。

You could combine this with your second option so that there is a statically typed option, and possibly even label the put(String, Object) method as deprecated to discourage its use.

最后,可以选择不考虑问题,并依靠应用程序程序员不将随机内容放入地图中。根据情况,这甚至可能是最好的解决方案。

And finally, there is the option of just ignoring the problem, and relying on the application programmer to not put random stuff into the map. Depending on the circumstances, this might even be the best solution.

这篇关于Java:项目不相关时有界的异构集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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