我有一个相对复杂的泛型类型(比如Map<Long,Map<Integer,String>>
),我在类内部使用它。(没有外部可见性;它只是一个实现细节。)我想将它隐藏在 typedef 中,但 Java 没有这样的工具。
昨天我重新发现了以下成语,并很失望地得知它被认为是一种反模式。
class MyClass
{
/* "Pseudo typedef" */
private static class FooBarMap extends HashMap<Long,Map<Integer,String>> { };
FooBarMap[] maps;
public FooBarMap getMapForType(int type)
{
// Actual code might be more complicated than this
return maps[type];
}
public String getDescription(int type, long fooId, int barId)
{
FooBarMap map = getMapForType(type);
return map.get(fooId).get(barId);
}
/* rest of code */
}
当类型被隐藏并且不构成库 API 的一部分时(在我的阅读中这是 Goetz 对使用它的主要反对意见),这是否有任何理由?