2 Replies Latest reply on Jan 21, 2010 1:20 AM by jschellSomeoneStoleMyAlias

    Reflection Performance / Best Practice

    843798
      Hi List

      Is reflection best practice in the followng situation, or should I head down the factory path? Having read http://forums.sun.com/thread.jspa?forumID=425&threadID=460054 I'm now wondering.

      I have a Web servlet application with a backend database. The servlet currently handles 8 different types of JSON data (there is one JSON data type for each table in the DB).

      Because JSON data is well structured, I have been able to write a simple handler, all using reflection, to dynamically invoke the Data Access Object and CRUD methods. So one class replaces 8 DAO's and 4 CRUD methods = 32 methods - this will grow as the application grows.

      Works brilliantly. It's also dynamic. I can add a new database table by simply subclassing a new DAO.

      Question is, is this best practice? Is there a better way? There are two sets of Class.forName(), newInstance(), getClass().getMethod(), invoke() ; one for getting the DAO and one for getting the CRUD method.....

      What is best practice here. Performance is important.

      Thanks, Len
        • 1. Re: Reflection Performance / Best Practice
          843798
          bocockli wrote:
          What is best practice here. Performance is important.
          I'm going to ignore the meat of your question (sorry, there are others who probably have better insights there) and focus on this point, because I think it's important.

          A best practice, when it comes to performance is: have clear, measurable goals.

          If your only performance-related goal is "it has to be fast", then you never know when you're done. You can always optimize some more. But you almost never need to.

          So you need to have a goal that can be verified. If your goal is "I need to be able to handle 100 update requests for Foo and 100 update requests for Bar and 100 read-only queries for Baz at the same time per second", then you have a definite goal and can check if you reached it (or how far away you are).

          If you don't have such a goal, then you'll be optimizing until the end of time and still won't be "done".
          • 2. Re: Reflection Performance / Best Practice
            jschellSomeoneStoleMyAlias
            bocockli wrote:
            Because JSON data is well structured, I have been able to write a simple handler, all using reflection, to dynamically invoke the Data Access Object and CRUD methods. So one class replaces 8 DAO's and 4 CRUD methods = 32 methods - this will grow as the application grows.

            Works brilliantly. It's also dynamic. I can add a new database table by simply subclassing a new DAO.

            Question is, is this best practice? Is there a better way?
            Use code generation.