Announcement Announcement Module
Collapse
No announcement yet.
Proxy object clarification Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Proxy object clarification

    i keep listening PROXY word so often, while reading forum topic, while reading document for Spring or Hibernate. I have little understanding of this. But, can someone throw some light on What exactly PROXY means from code implementation point of view, I know the concept .... please give some example.

    Thanks

  • #2
    Well, a proxy object is an object that looks like (and can be used) like the "real" target object. Usually it delegates all invocations to the target object itself.
    Due to its existence one is able to tweak the delegation process of one or all methods. So you are able to preprocess the invocation or postprocess its result or even abort it (by throwing an exception).
    While Spring provides facilities to do all this stuff behind the scenes with declarative means it is easy to program a proxy object yourself. It's no magic in it.

    Code:
      public interface Foo {
        void bar(String str);
      }
    
      public class FooImpl implements Foo {
        public void bar(String str) {
          System.out.println("bar: " + str);
        }
      }
    
      public class FooProxy implements Foo {
        private Foo delegate;
    
        public void setDelegate(Foo delegate) {
          this.delegate = delegate;
        }
    
        public void foo(String str) {
          // here we can do something with str or throw an exception
    
          this.delegate.foo(str);
    
          // If we had a return value we could change it here
        }
      }
    Hope that helps,
    Andreas

    Comment


    • #3
      The Spring reference manual might also be useful. Proxies are talked about quite a bit so there is some good stuff in here.

      e.g.
      http://www.springframework.org/docs/...ng-aop-proxies

      Comment


      • #4
        Originally posted by karldmoore View Post
        The Spring reference manual might also be useful. Proxies are talked about quite a bit so there is some good stuff in here.
        Yes, that is always a good starting point. And it evolves continuously. As I see there are ever more overview diagrams available.

        Though I must confess that I find the second diagram (with the proxy) in the section you refer to not very enlightening. But there are code examples as well.

        Regards,
        Andreas

        Comment


        • #5
          Yeah the documentation for 2.0 was a huge improvement, its always getting better! I can see how the second diagram might not be the most intuitive. You could always JIRA it if you want to improve it even more.

          Comment


          • #6
            Originally posted by Andreas Senft View Post
            Though I must confess that I find the second diagram (with the proxy) in the section you refer to not very enlightening.
            Originally posted by karldmoore View Post
            I can see how the second diagram might not be the most intuitive.
            Sheesh, everyone's a critic. I'll have you know that that diagram took me like ~15 minutes in PowerPoint! Although, looking at it in the cold light of day, it really isn't very clear... just what is that grey arrow all about?

            I'll make a point of cleaning that one up in the 2.0.3 timeframe.

            Cheers
            Rick

            Comment


            • #7
              I started with praise and then the criticism. Take the compliment! Looking forward to another improvement in 2.0.3.

              Comment


              • #8
                Originally posted by Rick Evans View Post
                Although, looking at it in the cold light of day, it really isn't very clear... just what is that grey arrow all about?
                What about a UML sequence diagram? The grey arrows seem to be the "return" known from the sequence diagrams.

                Jörg

                Comment


                • #9
                  Thanks Rick. Very appreciated.

                  I second Karl in that the documentation has much improved since 2.0 (I clearly not intend to imply that it has been bad before).

                  Guess that you have a good share in the overhaul, so thank you for your work

                  Regards,
                  Andreas

                  Comment


                  • #10
                    Originally posted by Jörg Heinicke View Post
                    What about a UML sequence diagram?
                    I think that is a good idea. In that case the first diagram has to be changed to a (simple) sequence diagram as well in order to highlight the difference.

                    Regards,
                    Andreas

                    Comment


                    • #11
                      Thanks for your input, it made me comfortable but below description from hibernate.org "Proxy Visitor Pattern" was really helpful.

                      In Hibernate, lazy loading of persistent relationships and collections is facilitated by proxies. For example, let's say you have a LooneyBin object that has a collection of Nutter objects that is lazily loaded. If you call looneyBin.getNutters(), Hibernate will only load the ID and version number of the Nutter objects, thus saving all the work of loading individual objects until later. You think you have all the related Nutter objects, but what you really have is a collection of proxies.

                      Comment

                      Working...
                      X