1

Wiki:

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

Hmm!, what would this look like in terms of code ?

Wiki

delegation refers to one object relying upon another to provide a specified set of functionalities

This class for example delegates its functionalities to another object:

class CustomInt: 

    def __init__(self): 
        self.obj = int() 

    def __getattr__(self, attr): 
        return getattr(self.obj, attr)   # Delegation

Since it's a class functioning as an interface for something else, can I consider it as a proxy class ?

2

A proxy has no other functionality than to forward actions. A class that delegates also adds its own functionality.
In that, effectively, a proxy is a special case of delegation.
As @Jules said, your example is one of a proxy.

  • Nope. A proxy typically adds its own functionality (for example caching answers or ensuring initialization) while a delegate usually delegates an action to something else (without adding functionality). – Johannes Overmann Dec 9 '19 at 10:27
1

Yes. Your class is a proxy, and this is the typical pattern used to implement a proxy in a dynamic language (in static languages, each method must typically be forwarded individually). Proxies also usually change the behaviour of the object they proxy on a subtle way (e.g. preventing access to certain methods, hosting the target object in a separate process or on another machine, etc.).

0

If we implement simple Proxy, it looks like this:

# Simple demonstration of the Proxy pattern.

class Implementation:
    def f(self):
        print("Implementation.f()")
    def g(self):
        print("Implementation.g()")
    def h(self):
        print("Implementation.h()")

class Proxy:
    def __init__(self):
        self.__implementation = Implementation()
    # Pass method calls to the implementation:
    def f(self): self.__implementation.f()
    def g(self): self.__implementation.g()
    def h(self): self.__implementation.h()

p = Proxy()
p.f(); p.g(); p.h()

It isn’t necessary that Implementation have the same interface as Proxy; as long as Proxy is somehow “speaking for” the class that it is referring method calls to then the basic idea is satisfied (note that this statement is at odds with the definition for Proxy in GoF). However, it is convenient to have a common interface so that Implementation is forced to fulfill all the methods that Proxy needs to call.

Of course, in Python we have a delegation mechanism built in, so it makes the Proxy even simpler to implement:

# Simple demonstration of the Proxy pattern.

class Implementation2:
    def f(self):
        print("Implementation.f()")
    def g(self):
        print("Implementation.g()")
    def h(self):
        print("Implementation.h()")

class Proxy2:
    def __init__(self):
        self.__implementation = Implementation2()
    def __getattr__(self, name):
        return getattr(self.__implementation, name)

p = Proxy2()
p.f(); p.g(); p.h();

The beauty of using getattr( ) is that Proxy2 is completely generic, and not tied to any particular implementation (in Java, a rather complicated “dynamic proxy” has been invented to accomplish this same thing).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.