Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • 1
    I'm not really clear how a map using the type as keys is different from a switch or chain of ifs, and when it would be necessary to use instead of polymorphism. The "mechanism to populate the map automatically" seems to be the important part, but you don't really explain how that would work - are you talking about some kind of registry that types can be added to? Commented Jun 22, 2020 at 9:51
  • A map with keys is different from a switch or chain of ifs because it is more maintainable and less redundant. The switch or chain of ifs may be required in more than one place of the code. The map would simply be used, the initialization of the map would be in a central place. A possible mechanism to populate the map is described in my answer, it is java.util.ServiceLoader. Commented Jul 24, 2020 at 13:50
  • OK, so you've edited in a class name, but that means very little to anyone who's never used Java, and it still feels like the emphasis of the answer is on the wrong part. Commented Jul 24, 2020 at 15:34