Proxy Pattern
-
Upload
hueseyin-ergin -
Category
Technology
-
view
1.886 -
download
0
description
Transcript of Proxy Pattern
![Page 1: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/1.jpg)
Pattern
Structural Pattern
![Page 2: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/2.jpg)
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?
Create another box of exactlythe 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
He he
![Page 3: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/3.jpg)
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!
![Page 4: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/4.jpg)
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
![Page 5: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/5.jpg)
5
Proxy Pattern Structure
![Page 6: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/6.jpg)
6
Concrete Structure
![Page 7: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/7.jpg)
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).
![Page 8: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/8.jpg)
8
Object & Sequence Diagrams
![Page 9: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/9.jpg)
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.
![Page 10: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/10.jpg)
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.
![Page 11: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/11.jpg)
11
Variants Remote Proxy:
Provides a local represent of an object in different address space (or maybe in same address)
![Page 12: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/12.jpg)
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).
![Page 13: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/13.jpg)
13
Variants – cont’d Access Control Proxy:
Act as an controller between the real subject and clients.
Example: Normal proxy server in modern web
![Page 14: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/14.jpg)
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!
![Page 15: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/15.jpg)
15
Relation with Other Patterns Adapter Pattern
Implements a difference interface to the object it adapts
Proxy Pattern Implements same interface as its subject
![Page 16: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/16.jpg)
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
![Page 17: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/17.jpg)
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 fromDesign Patterns in Java 2nd EditionSteven John Metsker, William C. Wake
![Page 18: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/18.jpg)
18
Thanks
Questions?
![Page 19: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/19.jpg)
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
![Page 20: Proxy Pattern](https://reader036.fdocuments.net/reader036/viewer/2022062308/558fca921a28abf2388b45ca/html5/thumbnails/20.jpg)
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