HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Java enum for mapping classes and specific URL

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
enumjavaforclassesspecificandmappingurl

Problem

I have a ZK project in Java 6 (ZK is like ASP.NET but with zul files in stead of aspx). For showing and creating/updating the catalogs I created an abstracted view. This all works with 1 viewmodel at the back. To go to the right catalog I provide a String what is the catalog class his name.

Of course with just that String I don't have enough data so I created a mapper. At first the mapper was an HashMap with a String as key. Then I refactored to Enum cause in mine opinion enum is lightweight, and getting the correct values should go faster.

The names of the enum are very important cause I do the lookup like this:

CatalogMapper.valueOf(currentClass.toUpperCase());


Other point is the link to the detailscreen. For this instant it is all the the same path. I didn't cut it to concatenate in the constructor for the reason that I don't have to concatenate then. (It takes a little more cpu but actually it's negligible.

Questions:

  • Is this good practice?



  • Could the mapper be done otherwise so I can make the names more correct?(adding _ to the name)



  • Should I do the concatenation in the constructor?



  • Should I use reflection? (the pojos could be in subpackages in the 'catalog' package)



  • Was the Map a better solution for Enum troubles mine insight?



```
public enum CatalogMapper {

BERANK(BERank.class, "/WEB-INF/webpages/zk/catalog/details/berankdetails.zul", BERankRepository.class, QBERank.bERank._super),
COMPONENT(Component.class, ComponentRepository.class, QComponent.component._super),
CONTINENT(Continent.class, "/WEB-INF/webpages/zk/catalog/details/continentdetails.zul", ContinentRepository.class, QContinent.continent._super),
COUNTRY(Country.class, "/WEB-INF/webpages/zk/catalog/details/countrydetails.zul", CountryRepository.class, QCountry.country._super),
FEATURE(Feature.class, FeatureRepository.class, QFeature.feature._super),
FUNCTION(Function.class, FunctionRepository.class, QFunction.function._su

Solution

I have a strong feeling that you are somewhat off track. I had not heard of the ZK framework until I read your post, but a visit to their website confirms that it should be a better way to do a Java website, albeit at a licencing cost. (I am very familiar with Spring MVC and Struts, know a little PHP and have used ASP.net for one project).

My observations are:

  • Your enum seems to be mapping a string variable (enum name) to a URL and a pointer to a model class somewhere in a tree of models & pages (correct me if I am wrong). That is bread and butter work for any web framework, and should not require the complexity you are presenting here. If ZK lives up to its advertising hype, it will provide this functionality out of the box.



  • You are stretching the intended use of enums. They are intended as type-safe constants. Once they sprout multiple attributes and overloaded constructors, they should be replaced (in my opinion) by Java classes.



  • Don't worry about performance. Looking up a value in a HashMap or looking up an enum is going to be a tiny proportion of the time used to render a page. So ignore it as a design constraint, and revisit it only if the optimiser says it is slow.



  • There is a great deal of commonality in your enum entries. All of them refer to the class type (e.g. Component.class) and most refer to a _super method. Again this points towards using standard classes rather than an enum.



Best of luck...

Context

StackExchange Code Review Q#56722, answer score: 7

Revisions (0)

No revisions yet.