何时使用 "Dynamic Proxy class" 或 "standard proxy" 模式?
When to use "Dynamic Proxy class" or "standard proxy" pattern?
为什么要使用 "Dynamic Proxy class" 而不是 "standard 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.
标准代理模式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 the real object are invoked. For
the client, usage of a proxy object is similar to using the real
object, because both implement the same interface.
您似乎回答了自己的问题。您应该使用更容易为您的用例实现的那个。
当编译时没有每个方法的实现时,你需要动态代理。
例如,模拟测试库使用动态代理,以便可以编写代码来通用地处理任何方法。
为什么要使用 "Dynamic Proxy class" 而不是 "standard 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.
标准代理模式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 the real object are invoked. For the client, usage of a proxy object is similar to using the real object, because both implement the same interface.
您似乎回答了自己的问题。您应该使用更容易为您的用例实现的那个。
当编译时没有每个方法的实现时,你需要动态代理。
例如,模拟测试库使用动态代理,以便可以编写代码来通用地处理任何方法。