Design Patterns: Proxy (Surrogate)

Posted by Monik, 27 October 2010.
Programming Code Design Design Patterns
Book summary
Summary on basis of the book ,,Design Patterns: Elements of Reusable Object-Oriented Software'', by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Addison-Wesley, 1995.

Proxy pretends being an object when in fact it's not. There are several types of proxies:
  1. Remote proxy ("Ambassador") - we have client and server, instead of communicating between server's and client's classes we create a proxy on client side, and a proxy on server side, and only the proxies communicate
  2. Virtual proxy - creates expensive object on first demand; it can also hold information about the object which is not instantiated yet (e.g. height and width of an image)
  3. Protection proxy - to control access
  4. Smart proxy - just an addition to an object, to have more control, e.g. lock/unlock, conuting number of references (to later destroy if no references)
** Proxies do not connect with other proxies as their objects, in such case we'll have Decorator pattern, not Proxy.

** "copy-on-write" method - we have same proxy object referenced by different classes, when one proxy state changes, it copies itself. in such case we have to remember of counting number of references as not to leave the last proxy not destroyed

Comments


Comments: