Proxy Pattern

The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.

Bates and SierraHead First Design Patterns

##Class Diagram /images/posts/design-patterns/proxy.jpg

Subject interface is implemented by both RealSubject and Proxy. So they are interchangeable and it allows the client to treat the Proxy as RealSubject.

RealSubject does the real work and the Proxy _controls the access_ to it.

Proxy handles the creation of the RealSubject keeping a reference to the Subject in order to be able to forward the request coming from the client to the real implementation.

Proxy can controls the access to the real object in different ways

  • Remote proxy.
    • It controls the access to a remote object.
    • The proxy acts as a local representative for an object that lives on different JVM.
    • The object actually lives somewhere else on the network.
  • Virtual proxy.
    • It controls the access to a resource that is expensive to create.
    • The proxy acts as a representative for an object that may be expensive to create.
    • It defers the creation of the object until it is needed.
    • Virtual proxy acts as a surrogate for the object before and while it is being created.
    • Object is expensive to create because it has to be retrieved from a database over the network.
  • Protection proxy.
    • It controls the access to a resource based on access rights.
  • Caching proxy. Similar to a virtual proxy but it caches the expensive objects it creates to reduce the latency of the forwarded request.

##Key Points

  • Proxy pattern
    • provides a surrogate or place holder for another object,
    • creates a representative object that _controls access_ to another object which can be remote, expensive to create or need secure access to have its methods used.
  • Wrapper
    • In some way the proxy wraps (HAS-A) the real object.
    • Proxy “intercepts” the requests from the client and forwards them to the real object acting a some form of control on them.
    • If the real object is remote, there is a sort of “remote proxy” receiving the request or call over the network invoking the call of the method against the real object it owns.
  • Factory method Using a factory method it is possible to wrap the real object into the proxy and return it to the client as it was the real one.

##Comparisons All proxies have in common the ability to intercept a method call that a client has done. The client always invoke method on a proxy thinking that it is the real object. Proxies always acts as surrogates.

###Proxy vs. Decorator and Adapter

  • Proxy
    • It always control the access to a class.
    • Proxy decouples the client from the real object. Controlling the access, the proxy allow the client the use of the real object only when it is available, so the client doesn’t have to wait for it.
    • The protection proxy may provides a partial interface to a real object, in this it is similar to an adapter.
    • It wraps the subject. The client doesn’t know what object has been wrapped. Virtual proxy wraps an object that even doesn’t exist at the beginning.
  • Decorator
    • It always adds behavior to a class.
    • It wraps an object.
    • It never instantiates anything.
  • Adapter
    • It forwards the request from the client to another object, but it changes the interface of the objects it adapts.
    • It is different from the proxy which always implement the same interface of the real object.