Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Proxy pattern..

 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone tell what a proxy pattern is used for? I know a proxy
object represents another object.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two general reasons to use it. One is in a distributed system. When the actual object resides on another machine, and you want to invoke a method on it, the proxy is the local object that receives the method invocation and then marshalls the parameters and sends them across the network, then unmarshalls the result when it comes back. The second general use is in database ORM systems when you have a deeply nested object and you choose to represent a contained object as a proxy that only contains the foreign key -- you would retrieve and instantiate the actual object only when it is referenced through a method call.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The virtual proxy (in GoF terms) variant is also quite common.
A virtual proxy allows the initialization cost of an 'expensive' (in terms of memoery usage, for example) object to be defered up to the moment it becomes necessary for the actual object to be fully initialized - creation on demand.

Edit: I just realized that Kyle Brown already described this use, though it's not limited to ORM solutions.
[ July 24, 2008: Message edited by: Jelle Klap ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic