Proxy

From CSSEMediaWiki
Revision as of 03:38, 16 July 2009 by Michal Connole (Talk | contribs)
Jump to: navigation, search

The Proxy design pattern is used to provide a stand-in for a real class. The proxy implements the same interface as the real class and forwards method calls to it. The GoF specify four types of proxies that can follow the pattern:

  • A remote proxy can be used to represent an object which is not contained in the application. For example, an object that is on another networked computer.
  • A virtual proxy is used to implement lazy initialization. Useful for creating expensive objects when needed rather than at startup.
  • A protection proxy is used to have greater control over the access rights to the object.
  • A smart reference can use a proxy so that when the real object is accessed it can perform additional actions such as creating it on first access and counting references so that it can be deleted when it is no longer referenced (not obvious that this would be important in languages with garbage collection, but there might be additional actions that need to be performed on deletion).

Structure

Diagram from the GoF:

Proxy.png

Consquences

The proxy design pattern introduces a level of indirection to an object. This has many uses for performance optimization as well as greater flexibility on how references are kept and what kind of objects these references can be made to.

See Also

Personal tools