proxy pattern

20
Pattern Structural Pattern

Upload: hueseyin-ergin

Post on 28-Jun-2015

1.886 views

Category:

Technology


0 download

DESCRIPTION

Proxy Pattern Presentation based on GoF Book

TRANSCRIPT

Page 1: Proxy Pattern

Pattern

Structural Pattern

Page 2: Proxy Pattern

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

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

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

5

Proxy Pattern Structure

Page 6: Proxy Pattern

6

Concrete Structure

Page 7: Proxy Pattern

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

8

Object & Sequence Diagrams

Page 9: Proxy Pattern

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

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

11

Variants Remote Proxy:

Provides a local represent of an object in different address space (or maybe in same address)

Page 12: Proxy Pattern

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

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

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

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

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

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

18

Thanks

Questions?

Page 19: Proxy Pattern

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

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