2 Replies Latest reply on Aug 6, 2008 4:04 PM by guy bashan

    Factory question

    guy bashan Apprentice

      Hi,


      I am trying to figure out a general concept of the @Factory patter.


      I have a backing bean in which I have a method getVideo. Up until now this is what I was doing to implement getVideo method:



      private Video video;
      
      public Video getVideo()
      {
        if (video == null)
        {
          video = myDAO.getVideoFromDB(getRequestParameter("vid")))
        }
        return video;
      }



      As ar as I understand by using Factory I can do:


      @Factory
      public Video getVideo()
      {
        return myDAO.getVideoFromDB(getRequestParameter("vid")))}
      }
      



      Is it a correct usage of Factory?


      Now, my question is: can I do: getVideo() in other methods and be sure that the code for fetching the Video object won't be done more than once?


      Thanks,
      Guy.

        • 1. Re: Factory question
          Samuel Mendenhall Apprentice

          The two ways to use Factory are described here: http://docs.jboss.com/seam/2.0.3.CR1/reference/en-US/html_single/#d0e3905


          You are using the first way.  As for directly accessing the method, sure, it is just a Java call, but if you directly access it it will be like any other Java call, called each time you make the call.  Try just doing #{video} which accesses it via the Factory.

          • 2. Re: Factory question
            guy bashan Apprentice

            So as far as I understand the Factroy pattern won't help me if I will be having to use the method getVideo() in my backing bean itself. for example, I want to do in my bean:



            private List<Video> relatedVideos;
            
            public List<Video> getRelatedVideos()
            {
              if (relatedVideos == null)
              {
                relatedVideos = myDAO.getRelatedVideos(getVideo())
              }
            
              return relatedVideos;
            }




            Of course I can't access the video member directly since I am risking in null pointer exception...


            Am I right? no neat solution to this issue in Seam?


            Thanks,
            Guy.