Upcoming SlideShare
×

# Proxy Pattern

2,430 views
2,277 views

Published on

Proxy Pattern Presentation based on GoF Book

Published in: Technology
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
2,430
On SlideShare
0
From Embeds
0
Number of Embeds
255
Actions
Shares
0
107
0
Likes
3
Embeds 0
No embeds

No notes for slide

### Proxy Pattern

1. 1. PatternStructural Pattern
2. 2. Warm-up You want to impress your girlfriend by carrying a 250 lbs box (what an impression). It is not easy as you see! What to do? He he Create another box of exactly the same look, but lighter. Now you can go! The ‘not that heavy’ box is the proxy of the normal box. But still makes the same effect  2
3. 3. Another Example – This time Serious Drawback money from the bank! We will not go to a bank branch. Just go to an ATM. Get the cash! So ATM is some kind of a bank.  With reduced functionalities. ATM is also a proxy to the bank! 3
4. 4. Proxy Pattern Proxy pattern comes into play when we have HeavyWeight objects and  We want to implement a simpler version of a HeavyWeight object.  We don’t need the whole functionality of the HeavyWeight object.  We want to limit the access to the HeavyWeight object. Because there may be a time-delay or complex mechanism in creating instances of HeavyWeight objects. Proxy pattern is also known as «surrogate».  Means to put something into place of another 4
5. 5. Proxy Pattern Structure5
6. 6. Concrete Structure6
7. 7. Participants Subject (Bank)  The common interface for the proxy and the real subject Real Subject (Bank Branch)  The concrete subject that implements the interface. Proxy (ATM)  Provides the same interface as Real Subject (or a subset)  Maintains a reference to the Real Subject  Since it doesnot have all the data as Real Subject (a lightweight), it can do many things faster than Real Subject.  Then invokes Real Subject (if needed). 7
8. 8. Object & Sequence Diagrams8
9. 9. Consequences A level of indirection while accessing real subject.  We can think this as another tier between real subject and us. We want to support heavyweight objects, but we want to create only when they are requested.  Memory will not get full.  Programs will run faster. It is like giving a small example of the same subject.  Most of the times, proxy will have same interfaces as subject.  Programmers can declare variables without caring whether a proxy or the heavyweight will be put. 9
10. 10. Disadvantages Do you think of any disadvantages? Identity Comparison  We can’t do identity comparison since we don’t know exact real subject, just a surrogate. Inambiguity  Client may not be aware that the real subject it is accessing now is not same as the previous one.  Because client doesn’t know what else proxy is doing, other than calling real subject. 10
11. 11. Variants Remote Proxy:  Provides a local represent of an object in different address space (or maybe in same address) 11
12. 12. Variants – cont’d Virtual Proxy:  When we have so many heavyweight objects, we can create virtual proxies for objects to load program faster.  Program can start with proxies and real subjects may be downloaded on background (or when requested). 12
13. 13. Variants – cont’d Access Control Proxy:  Act as an controller between the real subject and clients.  Example: Normal proxy server in modern web 13
14. 14. Variants – cont’d Smart Reference Proxy:  Provides additional actions whenever a real subject is referenced.  Counting the number of accesses  Locking it so that only one object can change at a time.  Deleting an object after a certain number of accesses. Cache Proxy:  Provides temporary storage of results of the expensive target operations.  Make some certain operations faster! 14
15. 15. Relation with Other Patterns Adapter Pattern  Implements a difference interface to the object it adapts Proxy Pattern  Implements same interface as its subject 15
16. 16. Relation with Other Patterns – cont’d Decorator Pattern vs Proxy Pattern  Both patterns describe a level of indirection to an object  Implementations of both keeps a reference to another object  But they are intended for different purposes  Decorator is adding more responsibilities to the object while decorating  Proxy is controlling access to it 16
17. 17. Demo Time An image displayer First shows the proxy with a default image. When load pressed, it will load the actual image. (Bigger and HD) UML diagram is on the right. See the proxy pattern? Demo from Design Patterns in Java 2nd Edition Steven John Metsker, William C. Wake 17
18. 18. Thanks Questions?18
19. 19. References GoF Design Patterns Book The ‘not that strong’ man: http://www.revolutionhealth.com/articles/proper-lifting- technique/hw206944 The impressed girl: http://www.shutterstock.com/pic-10016596/stock-photo-surprised- girl-pointing-at-copy-space-portrait-of-a-beautiful-young-business-woman-looking- up.html The bank: http://chinarsuvidhaacenter.org/ The ATM Machine: http://www.clipartheaven.com/show/clipart/money/atm-gif.html Variant images: O’Reilly Head First Design Patterns book Access Control Proxy image: http://www.democracybroadcastingnews.com/?p=12329 http://www.site.uottawa.ca:4321/oose/index.html#proxy Anti-pattern from: The patterns handbook: techniques, strategies, and applications By Linda Rising (google books has this book) http://blog.decayingcode.com/post/anti-pattern-god-object.aspx http://www.oodesign.com/proxy-pattern.html http://www.javacamp.org/designPattern/proxy.html Demo from book: http://www.informit.com/store/product.aspx?isbn=0321333020 19
20. 20. Bonus: Introducing Anti-Patterns Anti-patterns are a way of making the things wrong. Remember Edison?  Tried hundreds of possible materials for flament till building the first electric light.  At each failure, he knows what he shouldn’t do next time! Designs may end up with an anti-pattern by:  Developer or designer’s not knowing better  Not having sufficient experience for specific problems  Or applying a perfectly good pattern in the wrong context. One example is «God Object»  An object that knows too much or does too much  Result of a mis-use of facade pattern 20