- Provide a surrogate or placeholder for another object to control access to it.
- In object-oriented languages, objects do the work they advertise through their public interface.
- Clients of these objects expect this work to be done quickly and efficiently.
- However, there are situations where an object is severely constrained and cannot live up to its responsibility.
- Typically this occurs when there is a dependency on a remote resource (a call to another computer for example) or when an object takes a long time to load.
- In situations like these, you apply the Proxy pattern and create a proxy object that "stands in" for the original object.
- The Proxy forwards the request to a target object.
- The interface of the Proxy object is the same as the original object and clients may not even be aware they are dealing with a proxy rather than the real object.
- There are 3 different types of proxies:
- Remote proxies are responsible for encoding a request and for forwarding the encoded request to a real object in a different address space (app domain, process, or machine)
- Virtual proxies may cache additional information about a real object so that they can postpone accessing it (this process is known by many names, such as, just-in-time loading, on-demand loading, or lazy loading)
- Protection proxies check that the caller has the proper access permissions to perform the request.
- A Smart Reference is a proxy for a pointer.
- In .NET the Proxy pattern manifests itself in the Remoting infrastructure.
- In .NET Remoting, whenever an object requires access to an object in a different address space (app domain, process, or machine) a proxy is created that sends the request to the remote object and any data it needs.
- As is common with proxies, the client is frequently not even aware that a proxy is at work.
- Clients of WCF services also rely heavily on auto-generated proxy objects.
Participants
Proxy
(ServiceProxy)
- Maintains a reference that lets the proxy access the real subject.
Proxy
may refer to a Subject
if the RealSubject
and Subject
interfaces are the same.
- Provides an interface identical to Subject's so that a proxy can be substituted for the real subject.
- Controls access to the real subject and may be responsible for creating and deleting it.
- Other responsibilities depend on the kind of proxy:
- Remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.
- Virtual proxies may cache information about the real subject so that they can postpone accessing it.
- Protection proxies check that the caller has the access permissions required to perform a request.
Subject
(IService)
- Defines the common interface for
RealSubject
and Proxy
so that a Proxy
can be used anywhere a RealSubject
is expected.
RealSubject
(Service)
- Defines the real object that the
Proxy
represents.
IService service = new Service();
IService serviceProxy = new ServiceProxy(service);
var result = serviceProxy.Execute();
public interface IService
{
string Execute();
}
class Service : IService
{
public string Execute() => "Remote Result";
}
class ServiceProxy : IService
{
private string _resultCached;
private IService _service;
public ServiceProxy(IService service)
{
_service = service;
}
public string Execute()
{
if (string.IsNullOrEmpty(_resultCached))
_resultCached = _service.Execute();
return _resultCached;
}
}
Rules of Thumb
- The adapter provides a different interface to its subject.
- Proxy provides the same interface.
- The decorator provides an enhanced interface.
- Decorator and Proxy have different purposes but similar structures.
- Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests.