differences between proxy and dynamic proxy patterns
Asked Answered
E

2

11

I'm trying to understand what are the differences between proxy and dynamic proxy patterns. from what I've read so far the only thing that I found out is that the proxy class byte-code is created during compile time and on dynamic proxy it's created during run-time. are there another differences that i'm missing? if not then what's the reason to prefer proxy over dynamic proxy (except performance issues)

Ellga answered 14/12, 2013 at 14:7 Comment(0)
B
18

Dynamic proxy is essentially the proxy design pattern, in which the proxy object is created dynamically during runtime.

Proxy design pattern uses a proxy, which acts as a mediator between client and underlying real object. Programmer can perform access control, validation and additional action in proxy before delegating the request to real object.

Now suppose you want to perform some generic action before calling any method of any class for example you want to keep log of all the method calls made by client. In that case, if you want to implement proxy design pattern, steps are as following:

  1. create proxy class for each class.
  2. implement proxy class in a way, that first it make a log entry of the method call made by the client, than delegate the call to real object.

The problem with the above technique is that, suppose you have 1000 classes, you'll need to write 1000 proxy classes for each class and implement all the methods in all the classes which are essentially doing the same thing (performing logging action in our case), which is very tedious process and wastage of memory.

Won't it be better, if somehow at runtime, we are able to create a proxy object based on the client's call and then perform generic action(logging action in our case) before delegating the call to the real object? Well, that is what dynamic proxies does.

The process in case of dynamic proxy is as following:

  1. client calls some action on an object.
  2. system creates a proxy object at runtime based on client's call.
  3. proxy object calls a generic method to perform a generic action in case of each call.
  4. after the action, proxy object delegates the call to real object.

So in a nutshell, if you have some generic action to perform, use dynamic proxy, but if you want each class to be treated differenlty (in some classes perform logging, in some don't, in some access control etc.) use simple proxy. Hope I helped. If you need a code example, please let me know.

Blaeberry answered 27/5, 2014 at 7:13 Comment(1)
Thanks @ugrasen-banchhor, your explaination exactly what I'm trying to archive. Something like ActorProxy<IExample>.Create() then I will got the proxy object of IExample ... so it would be nice if you sharing your code example behind your explaination.Billibilliard
A
1

I will try to give info on Java's dynamic proxy and classic proxy pattern. In fact, Java's dynamic proxy is also an example of proxy pattern.

In dynamic proxy case, proxy class is created at runtime and you use InvocationHandler's to define its behaviour. This means that dynamic proxy class uses InvocationHandler as its strategy and delegates calls to its InvocationHandler.

In classic proxy pattern, you explicitly create the proxy class and implement your logic here. You can look at here for samples codes.

Anhydrous answered 13/4, 2017 at 13:35 Comment(1)
that is right. Just to clarify though, in dynamic proxy pattern InvocationHandler needs to be implemented by the proxy class and the actual method calls will be delegated to the Inoveker() method.Motorcycle

© 2022 - 2024 — McMap. All rights reserved.