Proxy Design Pattern

10,541 views

Published on

Structural Pattern

Published in: Design
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
10,541
On SlideShare
0
From Embeds
0
Number of Embeds
27
Actions
Shares
0
Downloads
176
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Proxy Design Pattern

  1. 1. Proxy Design Pattern<br />Anjan Kumar BollamSrikanth Gorlla<br />Design Patterns<br />Summer 09<br />
  2. 2. Example – Financial proxy<br />6/15/09<br />2<br />
  3. 3. Questions on Proxy design pattern<br />What is the intent of Proxy Design Pattern?<br />What is the motivation of Proxy Design Pattern?<br />If a Proxy is used to instantiate an object only when it is absolutely needed, does the Proxy simplify code?<br />How to handle multiple copies of the Complex Object using Proxy?<br />What are the advantages of using Proxy?<br />What are the other related patterns for Proxy?<br />When do we use Proxy Design Pattern?<br />6/15/09<br />3<br />
  4. 4. Intent<br />Provide a surrogate or placeholder for another object to control access to it.<br /><ul><li>Provides extra level of indirection
  5. 5. Protects real component from undue complexity</li></ul>Also Known as :<br /><ul><li>SURROGATE</li></ul>6/15/09<br />4<br />
  6. 6. Motivation<br />Creating objects even though they are not required may be expensive in terms of time or computer resource.<br /><ul><li>Proxy allows us to postpone this creation until we need the actual object.
  7. 7. Proxy also allows us to vary how and where an object is accessed.
  8. 8. Proxy creates each expensive object only when required.</li></ul>When to postpone object creation?<br /><ul><li>During initializations or creations.
  9. 9. When you need to represent an object that is complex or time consuming to create with a simpler one.</li></ul>6/15/09<br />5<br />
  10. 10. Motivation - Example<br />Document Editor<br /><ul><li>Need to Embed Graphical images
  11. 11. Loading of Graphical images takes time.
  12. 12. This may slow down the opening of the document.
  13. 13. But what to put instead of the actual object while initializing?</li></ul>We can use a PROXY<br />6/15/09<br />6<br />
  14. 14. Motivation - Example<br />Image Proxy<br /><ul><li>Acts as stand in for real image
  15. 15. Takes care of instantiating the real object when required by invoking the draw() function.
  16. 16. Forwards the subsequent requests directly to the image
  17. 17. Creates the real image only when the document editor asks it to </li></ul>6/15/09<br />7<br />
  18. 18. Applicability<br />Proxy is applicable whenever there is a need for more versatile or sophisticated reference to an object than simple pointer.<br />Key Words: Versatile or sophisticated<br /><ul><li>Behavior of an object depends on its state, and it must change its behavior depending on that state.</li></ul>6/15/09<br />8<br />
  19. 19. Applicable Situations<br />A remote proxy provides a local representative for an object in a different address space<br />A virtual proxy creates expensive objects on demand.<br />A protection proxy controls access to the original object.<br />A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed.<br />6/15/09<br />9<br />
  20. 20. Smart References<br />Typical uses include <br /><ul><li>counting the number of references to the real object so that it can be freed automatically when there are no more references (also called smart pointers)
  21. 21. loading a persistent object into memory when it's first referenced.
  22. 22. checking that the real object is locked before it's accessed to ensure that no other object can change it.</li></ul>6/15/09<br />10<br />
  23. 23. Structure – Class Diagram<br />6/15/09<br />11<br />Provide a surrogate or placeholder for another object to control access to it.<br />
  24. 24. Structure – Object Diagram<br />6/15/09<br />12<br />
  25. 25. Participants<br />6/15/09<br />13<br />The three participants are:<br /><ul><li>Proxy
  26. 26. Subject
  27. 27. Real Subject</li></li></ul><li>Participants<br />6/15/09<br />14<br />Proxy<br /><ul><li>Maintains a reference that lets the proxy access the real subject.
  28. 28. Provides an interface identical to subject's so that a proxy can by substituted for the real subject.
  29. 29. Controls access to the real subject and may be responsible for creating and deleting it. </li></li></ul><li>Participants<br />6/15/09<br />15<br />Subject<br /><ul><li>Defines the common interface for Real Subject and Proxy so that a Proxy can be used anywhere a Real Subject is expected. </li></li></ul><li>Participants<br />6/15/09<br />16<br />Real Subject<br /><ul><li>Defines the real object that the proxy represents. </li></li></ul><li>Participants - Example<br />6/15/09<br />17<br />
  30. 30. Collaborations<br />6/15/09<br />18<br />Proxy forwards requests to Real Subject when appropriate, depending on the kind of proxy.<br />
  31. 31. Consequences<br />The Proxy pattern introduces a level of indirection when accessing an object. This additional indirection has many uses.<br /><ul><li>A remote proxy can hide the fact that an object resides in a different address space.
  32. 32. A virtual proxy can perform optimizations such as creating an object on demand.
  33. 33. Both protection proxies and smart references allow additional housekeeping tasks when an object is accessed. </li></ul>6/15/09<br />19<br />
  34. 34. Consequences contd.<br />Another optimization that is hidden from the client is Copy-on-Write<br /><ul><li>Copying a large and complicated object can be an expensive operation. If the copy is never modified, then there's no need to incur this cost. By using a proxy to postpone the copying process, we ensure that we pay the price of copying the object only if it's modified.</li></ul>6/15/09<br />20<br />
  35. 35. Implementation<br />Overloading the member access operator in C++.<br /><ul><li>This lets us to perform additional work whenever an object is de-referenced.
  36. 36. Helps in implementing some kinds of proxy just like a pointer.</li></ul>Using “doesNotUnderstand” in Smalltalk.<br /><ul><li>Proxy class (with no super class) can redefine doesNotUnderstand so that the message forwarded to the subject.</li></ul>Proxy doesn’t always have to know the type of real subject.<br />6/15/09<br />21<br />
  37. 37. Overloading the member access pointer<br />class Image; <br /> // external function <br /> extern Image* LoadAnImageFile(const char*);<br /> class ImagePtr { <br /> public: <br />ImagePtr(const char* imageFile);<br /> virtual ~ImagePtr(); <br /> virtual Image* operator-&gt;(); <br /> virtual Image& operator*(); <br /> private: <br /> Image* LoadImage(); <br /> private: <br /> Image* _image; <br /> const char* _imageFile; <br /> };<br />6/15/09<br />22<br />
  38. 38. Overloading the member access pointer<br /><ul><li>The impementation of the methods of class ImagePtr (virtual proxy)</li></ul>ImagePtr::ImagePtr (const char* theImageFile) { <br /> _imageFile = theImageFile; <br /> _image = 0; <br /> }<br /> Image* ImagePtr::LoadImage () { <br /> if (_image == 0) <br /> _image = LoadAnImageFile(_imageFile); <br /> return _image; <br /> } <br />6/15/09<br />23<br />
  39. 39. Overloading the member access pointer<br />The overloaded -&gt; and * methods call the LoadImage() to return the _image object.<br />Image* ImagePtr::operator-&gt; () { <br /> return LoadImage(); <br /> } <br /> Image& ImagePtr::operator* () {<br /> return *LoadImage(); <br /> } <br />6/15/09<br />24<br />
  40. 40. Overloading the member access pointer<br />This approach allows us to call Image class operations through ImagePtr objects without going to the trouble of making the Image class operations part of the ImagePtr interface:<br />ImagePtr image = ImagePtr(&quot;anImageFileName&quot;); <br /> image-&gt;Draw(Point(50, 100)); <br /> //(image.operator-&gt;())-&gt;Draw(Point(50,100))<br />This isn’t a good solution to every kind of proxy, where the proxy need to know precisely which operation is called. For example, in the virtual proxy example presented in Motivation, image should be referenced when the Draw operation is called.<br />6/15/09<br />25<br />
  41. 41. Using DoesNotUnderstand in Smalltalk<br />Smalltalk provides a hook that provides automatic forwarding of messages from client to receiver.<br />It calls DoesNotUnderstand : aMessage when the client sends a message to a receiver that has no corresponding methods. <br /> How do we redirect the message to the subject in this case???<br /><ul><li>We can use a Proxy class (no superclass) that can redefine DoesNotUnderstand so that the message is forwarded to its subject.</li></ul>6/15/09<br />26<br />
  42. 42. Proxy doesn’t always have to know the type of real subject<br />If a proxy class can deal with the its subject solely through an abstract interface, then there is no need to make Proxy class for each RealSubject class.<br /><ul><li>Proxy can deal with all RealSubjects uniformly.</li></ul>If Proxy class is going to instantiate the RealSubjects (like inVirtual Proxy), then proxy should know about the concrete class. <br /> How to refer to subject before it is instantiated???<br /><ul><li>We need address space-independent object identifiers</li></ul>6/15/09<br />27<br />
  43. 43. Sample Code<br />Remote Proxy<br /><ul><li>Client – Server Example</li></ul>Virtual Proxy<br /><ul><li>ImageLoad Example</li></ul>Protection Proxy<br /><ul><li>Database Login Example</li></ul>6/15/09<br />28<br />
  44. 44. Known Uses<br />NEXTSTEP [Add94] uses proxies (instances of class NXProxy) as local representatives for objects that may be distributed. <br />World Wide Web Proxy generally runs on a firewall machine and allows people inside the firewall concurrent access to the outside world. <br />In Microsoft’s OLE DB, proxies hide whether a specific server is local or remote from a client. <br />6/15/09<br />29<br />
  45. 45. Related patterns<br />Adapter<br /> Adapter Pattern provides a different interface to its subject. Proxy provides the same interface.<br />Decorator<br /> Proxy provides the same interface. Decorator Pattern provides an enhanced interface<br />6/15/09<br />30<br />
  46. 46. References<br />Design Patterns, Elements of Reusable Object-Oriented Software, Erich Gamma, et. al., Addison-Wesley, 1994 <br />Head First Design Patterns by Eric Freeman & Elisabeth Freeman <br />http://www.dofactory.com/Patterns/PatternProxy.aspx<br />http://home.earthlink.net/~huston2/dp/proxy.html<br />http://en.wikipedia.org/wiki/Proxy_pattern<br />http://dotnetperls.com/protection-proxy<br />6/15/09<br />31<br />
  47. 47. Questions on Proxy design pattern<br />What is the intent of Proxy Design Pattern?<br />What is the motivation of Proxy Design Pattern?<br />If a Proxy is used to instantiate an object only when it is absolutely needed, does the Proxy simplify code?<br />How to handle multiple copies of the Complex Object using Proxy?<br />What are the advantages of using Proxy?<br />What are the other related patterns for Proxy?<br />When do we use Proxy Design Pattern?<br />6/15/09<br />32<br />
  48. 48. Questions?<br />6/15/09<br />33<br />

×