Hello all, I am building a webapp with MVC pattern. In my controller servlet, I need to register eventHandler classes for later use. Following are two options that I can think of: 1. Declare "private HashMap eventHandler" class level variable and in the init(...) method initialize the eventHandler variable with every event handler classes using reflection eventHandler.put(key, ((EventHandler) Class.forName(value).newInstance())). And get appropriate event handlers depending on request parameters. 2. Set up a method that returns appropriate event handler in the controller servlet using switch(): ... A major difference I can think of between those two options is number of times Class.forName(..) method is called. First option calls the Class.forName(..) method once for every eventHandler for the life of the servlet. Second option call the Class.forName(..) method every time there is request. Which one of those options is "better practice"? Is there any obvious flaw in logic? Is there "better practice" that those options? Many thanks in advance for your input.
Matthew Phillips
Ranch Hand
Joined: Mar 09, 2001
Posts: 2676
posted
0
I like option number one, but why build the MVC yourself when it has already been done for you? Have you looked into using Struts?