пятница, 19 сентября 2014 г.

Pattern Proxy (Surrogate)

Proxy Design Pattern Tutorial (video)

Proxy Design Pattern Tutorial

The proxy design pattern allows you to provide an interface to other objects by creating a wrapper class as the proxy. The wrapper class, which is the proxy, can add additional functionality to the object of interest without changing the object's code. Below are some of the common examples in which the proxy pattern is used,

Adding security access to an existing object. The proxy will determine if the client can access the object of interest.
Simplifying the API of complex objects. The proxy can provide a simple API so that the client code does not have to deal with the complexity of the object of interest.
Providing interface for remote resources, such as web service or REST resources.
Coordinating expensive operations on remote resources by asking the remote resources to start the operation as soon as possible before accessing the resources.
Adding a thread-safe feature to an existing class without changing the existing class's code.
In short, the proxy is the object that is being called by the client to access the real object behind the scene.

When to use proxy pattern?

Remote Proxy – Represents an object locally which belongs to a different address space. Think of an ATM implementation, it will hold proxy objects for bank information that exists in the remote server.
( RMI, CORBA and Jini)

Virtual Proxy (Lazy Load Proxy) – In place of a complex or heavy object, use a skeleton representation. When an underlying image is huge in size, just represent it using a virtual proxy object and on demand load the real object. You feel that the real object is expensive in terms of instantiation and so without the real need we are not going to use the real object. Until the need arises we will use the virtual proxy.

Protection Proxy – Are you working on a MNC? If so, we might be well aware of the proxy server that provides us internet by restricting access to some sort of websites like public e-mail, social networking, data storage etc. The management feels that, it is better to block some content and provide only work related web pages. Proxy server does that job. This is a type of proxy design pattern.

A smart reference - a replacement for a bare pointer that performs additional actions when an object is accessed.
Provides additional actions whenever a target
object is referenced such as counting the number of references to the object

Adding a thread-safe feature to an existing class without changing the existing class's code.

Cache Proxy - Provides temporary storage of the results of expensive target
operations so that multiple clients can share the results

Firewall Proxy - Protects targets from bad clients (or vice versa)

java.rmi.*, the whole API actually.

Java Remote Method Invocation (RMI)

In java RMI an object on one machine (executing in one JVM) called a client can invoke methods on an object in another machine (another JVM) the second object is called a remote object. The proxy (also called a stub) resides on the client machine and the client invokes the proxy in as if it is invoking the object itself (remember that the proxy implements the same interface that RealSubject implements). The proxy itself will handle communication to the remote object, invoke the method on that remote object, and would return the result if any to the client. The proxy in this case is a Remote proxy.

Adapter vs Proxy Design Pattern

Adapter design pattern provides a different interface from the real object and enables the client to use it to interact with the real object. But, proxy design pattern provides the same interface as in the real object.

Decorator vs Proxy Design Patter

Decorator design pattern adds behaviour at runtime to the real object. But, Proxy does not change the behaviour instead it controls the behaviour.

Комментариев нет:

Отправить комментарий