In another language (Clipper, Visual dBASE) we can store code blocks in an array, then do something like: eval(arrayOfCodeBlokcs[caseNumber]) In cases where there are a lot of cases to check this is really fast! Any idea to implement this idea in Java?
You can achieve similar by using a delegation model. In java, everything is view as object. Thus your code blocks are objects. Now you have to implement a standard interface for all the objects. For each object implementing the interface you'll have to register them to a Factory class. U can use a hashMap to do that. Now everytime you like to get a code block (object in Java), the Factory class will just return the Interface and hence you're able to invoke the method defined in the interface. Hope this help.
Cute trick. Those using it should understand the tradeoffs. For this to work, you need to have objects in your factory. Either these objects get created as needed (and may or may not be recycled, depending on whether or not they retain state), or they may all be created ahead of time, and stored. In the former case, created JIT-style, you take an object creation performance hit, which may overshadow the savings from the switch. In the latter case, you take a startup hit, as well as increated memory use; this option makes sense when you will be reusing the "switch" repeatedly, and can recycle stateless objects from within the factory. There are also maintenence and code size issues to consider.
Bottom line, don't naively assume a fancy trick like this will already be in your best interest. its clever and useful, but if you're not careful, you can get burned. --Mark email@example.com
Mark Herschberg, author of The Career Toolkit
What does a metric clock look like? I bet it is nothing like this tiny ad: