1

I am learning the concepts of reflection and I am reading "Java Reflection In action" book. I have just started with the chapter "Proxy" where it says "a proxy, that supports the interface of another object, its target, so that the proxy can substitute for the target for all practical purposes" .

Can I have a real life easy example to understand this concept of proxy please?

user3840170
  • 26,597
  • 4
  • 30
  • 62
  • @gefei it has a much more specific meaning in Java: http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Proxy.html – Sean Patrick Floyd Nov 07 '12 at 10:44
  • Thanks. Suppose ,I have class Dog and there I have a method bark(). So in which situation I will need a proxy for the Dog class please? – user1805790 Nov 07 '12 at 10:45
  • From that definition it sounds like it is just talking about `implements`. – lynks Nov 07 '12 at 10:48
  • If the Dog proxy is really a front for an instance running on a remote machine. You don't have to know whether the Dog that's barking in your app lives in your address space or across the world. – duffymo Nov 07 '12 at 10:48
  • the book says ---The proxy implements the same interface as the target so that it can be used in exactly the same way. The proxy delegates some or all of the calls that it receives to its target and thus acts as either an intermediary or a substitute. In its role as an intermedi-ary, the proxy may add functionality either before or after the method is for-warded to the target. This gives the reflective programmer the capability to add behavior to objects--- does this mean Dog class must have to implement interfcae if it wants to have its Proxy? – user1805790 Nov 07 '12 at 10:53
  • please edit your question with further details (instead of doing so in a longish comment), it's easier to read and answer :-) – kleopatra Nov 07 '12 at 11:03
  • Didn't the book outline examples? – Roman C Nov 07 '12 at 11:04
  • Yes, the book did provide example but I need simple ones please. – user1805790 Nov 08 '12 at 05:28

2 Answers2

1

The classic example is an RMI proxy, where a method call made on a local proxy object result in a method call on the proxied object that resides in another JVM on another machine.

The proxy allows the caller to treat the remote object as if it was a local object ...in most respects.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Well, proxy is just a mechanism, you can do diffrent things with and a lot of framework use them in various ways.

Here are however "classical" areas where proxies can be used:

  • Access control: intercept invocations and perform some control check
  • Security: you can wrap/unwrap references as they flow across modules to perform checks and restrict certain actions
  • Persistence: only the proxy is available, and data are loaded on demand from another storage medium
  • Lazy loading: the proxy load or compute information only when needed
  • Asynchrony: the proxy is a handle for a result that will be available later (so-called transparent future)
  • Remoting: the proxy provide the illusion of having the object localy, and deals with remote communication
  • Contracts: ensure the pre- and post-conditions are met before and after an invocation
  • AOP: aspect-oriented programming rely on the ability to intercept method invocation. One technique to do so it so use proxies.

Note: these areas have some overlap.

In the paper "On the design of the ECMAScript Reflection API", the authors distighuish between two main kinds of proxies:

Generic wrappers. Proxies that wrap other objects in the same address space. Ex- ample uses include access control wrappers (e.g. revokable references), higher-order contracts [Findler and Felleisen 2002], profiling, taint tracking, etc.

Virtual objects. Proxies that emulate other objects, without the emulated objects having to be present in the same address space. Examples include remote object proxies (emulate objects in other address spaces), persistent objects (emulate objects stored in databases), transparent futures (emulate objects not yet computed), lazily instantiated objects, test mock-ups, etc.

I think it covers more or less the areas I describe before. But no list will be complete. There is not fixed case for proxies--it is a generic mechanism or principle.

Community
  • 1
  • 1
ewernli
  • 38,045
  • 5
  • 92
  • 123