Java – logic within enumerations
My colleagues and I are discussing the logic in enumeration My personal preference is that there is no logic in Java enumeration (although Java provides such functionality) The discussion in this case revolves around convenient methods in the enumeration of return maps:
public enum PackageType { Letter("01","Letter"),.. .. Tube("02","Packaging Tube"); private String packageCode; private String packageDescription; .. .. public static Map<String,String> toMap() { Map<String,String> map = new LinkedHashMap<String,String>(); for(PackageType packageType : PackageType.values()) { map.put(packageType.getPackageCode(),packageType.getPackageDescription()); } return map; } }
My personal preference is to pull it out for service An argument that focuses the methods in enumeration on convenience The idea is that you don't have to get the service, but you can query the enumeration directly
My argument focuses on separation and abstracting any type of logic as a service I didn't think "convenience" was a powerful argument for putting this method in enumeration
Which is better from a best practice perspective? Or is it simply a matter of personal preferences and code style?
Solution
I've done it before, but that doesn't mean it's the best practice
From my point of view, I'd rather have this logic on enumeration, because you won't move the 'toString' method to a service Logic involves only the enumeration itself and its own representation
I think it's misleading to push this method to the service - by putting it in the fact that the enumeration you're talking about has a "tomap" method Some people don't understand the service, but they may not know it by looking at this enumeration
It also helps to automate the IDE - I can play Key and immediately view the methods provided by the object