G-J February 2016

When to use "Dynamic Proxy class" or "standard proxy" pattern?

Why should one use a "Dynamic Proxy class" instead of the "standard proxy" pattern?

What are the disadvantages or advantages of both?

It seems like they both have the same end result, except that they are implemented differently.

Dynamic proxy class https://docs.oracle.com/javase/8/docs/technotes/guides/reflection/proxy.html

A dynamic proxy class is a class that implements a list of interfaces specified at runtime such that a method invocation through one of the interfaces on an instance of the class will be encoded and dispatched to another object through a uniform interface. Thus, a dynamic proxy class can be used to create a type-safe proxy object for a list of interfaces without requiring pre-generation of the proxy class, such as with compile-time tools. Method invocations on an instance of a dynamic proxy class are dispatched to a single method in the instance's invocation handler, and they are encoded with a java.lang.reflect.Method object identifying the method that was invoked and an array of type Object containing the arguments.

Standard proxy pattern https://en.wikipedia.org/wiki/Proxy_pattern

A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. In the proxy extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on

Answers


Peter Lawrey February 2016

You have appeared to answer your own question. You should use the one which is easier to implement for your use case.

You need to dynamic proxy when you do not have an implementation for each method at compile time.

For example, mocking test libraries use the dynamic proxies so that can write code to handle any method generically.

Post Status

Asked in February 2016
Viewed 2,562 times
Voted 12
Answered 1 times

Search




Leave an answer