11 Replies Latest reply: Feb 9, 2010 2:34 PM by Kayaman RSS

    Will Reflection be a wise choice?

    843798
      I have thought of two approaches to design a particular part of my code where I would be deciding what object to instantiate only during runtime
      1) Use reflection to instantiate objects at runtime and use these objects to invoke respective methods.
      2) Use a HashMap containing objects that are already created and retrieve these objects during runtime and use to invoke respective methods.

      I would like to have an opinion on which approach will be better, keeping performance and memory usage in mind.
      Or if any other approach will be better?

      Any help appreciated!
      Thanks
      Lydia.
        • 1. Re: Will Reflection be a wise choice?
          EJP
          Neither. This sounds like a job for the Factory pattern, along with a common interface that all the classes concerned implement.
          • 2. Re: Will Reflection be a wise choice?
            843798
            My main objective is to avoid numerous "if" blocks and that is the reason I had considered HashMap implementation.

            If I were to follow a Factory Pattern as you said I again will end up with the numerous "if" blocks that I want to avoid.
            • 3. Re: Will Reflection be a wise choice?
              EJP
              If I were to follow a Factory Pattern as you said I again will end up with the numerous "if" blocks that I want to avoid.
              Nope. All the objects implement the same interface, so all you have to do is call the required method on the objects via the interface. Java does the rest.
              • 4. Re: Will Reflection be a wise choice?
                Alan.M
                EJB,
                Nope. All the objects implement the same interface, so all you have to do is call the required method on the objects via the interface. Java does the rest
                The factory pattern accept a discriminator to create the required object and it will return an interface which the object implements. Fine but if I have 10 classes which implements a common interface and I need a situation to create
                an object for class1 so there will be a check to see what discriminator I have sent to the factory so that the factory will create the required object for the class type which I want.
                I see reflection in this case is useful if we send a class object or class type as a discriminator so that we can construct the object without actually going through the if else

                Can you please give us your feedback about a situation where we have 10 classes which implements one interface and we want to create different objects by using a factory pattern without
                1- using if else to decide which object to create.


                Tack so mycket.
                • 5. Re: Will Reflection be a wise choice?
                  843798
                  I am not clear on how I will be able to avoid those numerous "if"s in a factory pattern. Even in a factory class I will have to write down all the conditions in if blocks to decide on what object to instantiate right?
                  • 6. Re: Will Reflection be a wise choice?
                    DrClap
                    lydiaEd wrote:
                    I am not clear on how I will be able to avoid those numerous "if"s in a factory pattern. Even in a factory class I will have to write down all the conditions in if blocks to decide on what object to instantiate right?
                    You do have to have a decision procedure to decide which object to instantiate, yes. That might involve if-statements or it might involve a HashMap or it might involve something else. But using reflection techniques just to avoid if-statements doesn't strike me as a sensible strategy.

                    Of course the actual choice of strategy would depend on the actual conditions you had to implement, and we don't know anything about those except that they exist.
                    • 7. Re: Will Reflection be a wise choice?
                      jschellSomeoneStoleMyAlias
                      lydiaEd wrote:
                      My main objective is to avoid numerous "if" blocks and that is the reason I had considered HashMap implementation.

                      If I were to follow a Factory Pattern as you said I again will end up with the numerous "if" blocks that I want to avoid.
                      So?

                      Are you adding one of these a day or one a year?
                      If the first then you might want to start with that requirement and then look for solutions.
                      If the second then you do not have a problem and the factory is the best method because it is the easiest to understand now an in the future.
                      • 8. Re: Will Reflection be a wise choice?
                        Alan.M
                        jschell,
                        Are you adding one of these a day or one a year?
                        If the first then you might want to start with that requirement and then look for solutions.
                        If the second then you do not have a problem and the factory is the best method because it is the easiest to understand now an in the future.
                        Many thanks for reminding us.

                        I do not know why we tend to hate if and else statement eventhough it is part of flow control. Yes true and agreed of how simple it is to have it from the code reading perspective and from maintainability perspective.
                        I think I should be always reminded by keeping a balance between over complexity ( reflection) and simplicity ( if else ) in the factory pattern.

                        Alan
                        London
                        • 9. Re: Will Reflection be a wise choice?
                          791266
                          alan_mehio wrote:
                          EJB,
                          I guess you are addressing ejp and not ejb?
                          Tack so mycket.
                          Why Swedish? He isn't from Sweden.

                          @Op. Why do you want to do something like this? What does the classes represent, and when/how do you know that you need a certain instance?
                          • 10. Re: Will Reflection be a wise choice?
                            Kayaman
                            alan_mehio wrote:
                            jschell,
                            Are you adding one of these a day or one a year?
                            If the first then you might want to start with that requirement and then look for solutions.
                            If the second then you do not have a problem and the factory is the best method because it is the easiest to understand now an in the future.
                            Many thanks for reminding us.

                            I do not know why we tend to hate if and else statement eventhough it is part of flow control. Yes true and agreed of how simple it is to have it from the code reading perspective and from maintainability perspective.
                            I think I should be always reminded by keeping a balance between over complexity ( reflection) and simplicity ( if else ) in the factory pattern.

                            Alan
                            London
                            I'd say there are plenty more reasons to hate reflection than if/else.. :)
                            • 11. Re: Will Reflection be a wise choice?
                              EJP
                              EJB,
                              EJP I am not an Enterprise Java Bean. And I am far from stateless ;-)
                              Can you please give us your feedback about a situation where we have 10 classes which implements one interface and we want to create different objects by using a factory pattern without using if else to decide which object to create.
                              Map<Discriminator, Class<Interface>>.