Java – is there a reason for the “pseudo typedef antipattern”?

I have a relatively complex generic type (such as map < long, map < integer, string >), which is used internally in the class (there is no external visibility, it is just an implementation detail.) I want to hide it in typedef, but Java doesn't have such a function

Yesterday I rediscovered the following idiom and was disappointed to learn that it was considered an anti pattern

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 */

}

Is there any reason to do this when types are hidden and do not form part of the library API (in my opinion, Goetz is mainly opposed to using it)?

Solution

The real problem is that this idiom establishes a high coupling between your pseudo typedef and your client code However, since you use foobarmap privately, there are no real coupling problems (they are implementation details)

NB

Modern Java ides should help with complex generic types

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>