• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Difference between Proxy and Delegate  RSS feed

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading about different design patterns and I am confused about Proxy and Delegate.

Delegate: you instantiate the object in the class and then call the method of the object you instantiated.
Eg.

public Class Secretary {
public void work () {
S.O.P ("Work done...");
}

public class Boss {
public void work() {
Secretary sec = new Secretary();
sec.work() // Delegation
}

Proxy:
The proxy and the actual class must implement the same interface and we instantiate the actual class in the proxy and call the function (similar to delgate)

Please correct me if I am wrong.
 
Bartender
Posts: 2292
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hum... are we talking about the proxy pattern and the business delegate patterns?
 
Kashyap Hosdurga
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is correct. Sorry if I was not clear earlier.
 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the idea of the Proxy pattern is that you have an object that stays in front of another object and has the same interface as this object. So, if some operation has to be performed before another operation, you can make use of a proxy, to abstract the caller and simplify its code. For example, we have an object that has a getter method that retrieves a list of Strings, but we have to go to the database to retrieve this list. If there's a proxy, the caller simply invokes this getter method on the proxy object, and the proxy verifies whether the target object already has the list. If not, it goes to the database, retrieves the list, sets it on the target object and returns to the caller. Therefore, the proxy object can execute code before and after the code of the actual targeted object.

The original idea of the Business Delegate was to abstract the presentation layer from knowing how to look EJB objects up. The presentation layer (i.e. a Servlet) would have to know how to retrieve EJB objects that would execute business logic. You could then make use of a Business Delegate, so the Servlet could simply invoke a method on it, which would then retrieve the appropriate EJB object and delegate the execution of the business logic to it, simplifying the code of the presentation layer.

I have to confess that I have worked 95% of the time with Spring, but I believe that the Business Delegate pattern is retired nowadays. And yes, both patterns delegate the execution of things to other objects, but their purpose are different.
 
Kashyap Hosdurga
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!