1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used...

19
1 The Proxy Design Pattern • Problem: Defer the cost of object creation and init. until actually used • Applicability (possible contexts): Virtual Proxy: Create an expensive object on demand (lazy construction) Cache Proxy (PLoPD 2): Hold results temporarily Remote Proxy: Use a local representative for a remote object (different address space) Protection Proxy: Control access to shared object

Transcript of 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used...

Page 1: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

1

The Proxy Design Pattern

• Problem: Defer the cost of object creation and init. until actually used

• Applicability (possible contexts):– Virtual Proxy: Create an expensive object on

demand (lazy construction)– Cache Proxy (PLoPD 2): Hold results temporarily– Remote Proxy: Use a local representative for a

remote object (different address space)– Protection Proxy: Control access to shared object

Page 2: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

Intent and Motivation• Intent

– Provide a surrogate of placeholder for another object to control access to it.

• Motivation– Deferring object creation – lazy evaluation, on

demand creation– Separate this from the actual object

• Client treats the Proxy as if the real object was created • Proxy interprets the on-demand evaluation policy.

Page 3: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

Proxy MotivationProxy Motivation

Page 4: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

Proxy Motivation -2Proxy Motivation -2

Page 5: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

Proxy Applicability• Remote proxy provides a local representation of the

remote object in different address spaces• Virtual proxy creates expensive objects on demand• Protection proxy controls access to the original object,

for instance when implementing access rights to objects in a separate layer

• Smart reference instead of a simple pointer to count object references including– Counting references– Loading persistent objects into memory on first reference– Checking object locking on access

Page 6: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

Proxy StructureProxy Structure

Page 7: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

Proxy Structure - 2Proxy Structure - 2

Page 8: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

Proxy Participants• Proxy (ImageProxy)

– Maintains a reference by which to access the real subject.– Provides an interface identical to Subject, so it can be substituted for the real

subject– Controls access to the real subject and may be responsible for creating it.– Also:

• For remote proxy – encoding and decoding messages (I.e., RPC)• For virtual proxy – caching the real subject• For protection proxy – check callers access rights.

• Subject (Graphic)– Defines the common interface for RealSubject and Proxy so that a Proxy can

be used anywhere a RealSubject can.• RealSubject (Image)

– defines, the real object that the proxy represents.

Page 9: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

Proxy• Collaborations

– Proxy forwards requests to RealSubject when appropriate, depending on the kind of proxy.

– May perform some operations before like a mediator, or after.

• Consequences– Remote proxy – hides the fact that RealSubject is remote– Virtual proxy – optimizes such as on-demand creation– Protection proxy and smart reference – allow additional

housekeeping chores when subject is accessed.– Copy-on-write – postpones creation of a copy of an object

until it is necessary (if at all changed from the original).

Page 10: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

10

Sample Context: Word Processor

Paragraph

Document

Paragraph

Image

Document

Draw() GetExtent() Store() Load()

Glyph

Textextent

Draw() GetExtent() Store() Load()

Paragraph

fileName extent

Draw() GetExtent() Store() Load()

Image

contentextent

Draw() GetExtent() Store() Load()

Table

Page 11: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

11

Forces

1. The image is expensive to load2. The complete image is not always necessary

2MB 2KB

optimize!

Page 12: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

12

A Bad Solution

Obviously, the document should not be aware to the optimization, nor to the image contents, etc.

document::draw(){ switch (glyph.type) { case image: if (cannot_optimize) load_full_image(); else

optimize(glyph); break; case paragraph: . . .

Page 13: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

Applicability

• Behaviour of an object depends on its state, and it must change its behaviour depending on that state.

• Operations have large multipart conditional statements ...

Page 14: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

14

Proxy Solution (example)

Image Proxy|| file_name : String

|| get_image( )Draft( )DrawDetailed( )

Document

Real Image

|| ImageData

Glyph

Draw( ) 0..n

real_image

Solution: Provide a “surrogate” or place-holder which provides transparent access to another object

Page 15: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

15

Proxy Solution (example):Object Diagram

aDocumentimage

Proxy

theBitmap: RealImage

1: DrawDetailed ( )

3: new (fileName)

2: get_image ( )

4: DrawDetailed( )

Page 16: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

16

Proxy Solution (general form)

RealSubjectRequest( ) Proxy

Request( )

SubjectRequest( )

client

realSubject

Page 17: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

Proxy Motivation 1

image

aTextDocument

fileName

animageProxy

data

animage

in memory on disc

Page 18: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

Proxy Motivation 2DocumentEditor

Draw() GetExtent() Store() Load()

Graphic*

imageImp extent

Draw() GetExtent() Store() Load()

Image

fileName extent

Draw() GetExtent() Store() Load()

ImageProxy if (image==0) { image=LoadImage(fileName); } image->Draw()

if (image==0) { return extent; } else { return image->GetExtent(); }

image

Page 19: 1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:

Proxy StructureClient

... realSubject -> Request() ; ...

Request() ...

RealSubject

Request() ...

Subject

Request() ...

ProxyrealSubject

aRealsubjectrealSubject

aProxySubject

aClient

Provide a surrogate or placeholder for another object to control access to it