Docsity

Proxy Pattern: A Simple Place-holder for Complex Objects - Prof. Alan L. Sussman, Study notes of Programming Languages

Toggle sidebar
bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Proxy Pattern
Apr. 10, 2007
2
Example
• A program needs to load and
display a large image (perhaps over
a network).
– Very slow process
• When the program starts, there
must be some indication that an
image is to be displayed so that the
screen lays out correctly, but the
actual image display can be
postponed until the image is
completely loaded.
• This is particularly important in
programs such as word process ors
and web browsers that lay out text
around the images even before the
images are available.
3
A Proxy
•An image proxy can begin
loading the image in the
background, while drawing a
simple rectangle or other
symbol to represent the image’s
extent on the screen before it
appears.
• The proxy can even delay
loading the image at all until it
receives a paint request, and
only then begin the process.
4
A Proxy Pattern
• The Proxy pattern is used when you need to represent a complex object by a
simpler o ne.
• If creating an object is expensive in time or computer resources, Proxy allows
you to postpone this creation until you need the actual object.
• A Proxy usually has the same methods as the object it represents, and once the
object is loaded, it passes on the method calls from the Proxy to the actual
object.
• There are several cases where a Proxy can be useful:
– If an object, such as a large image, takes a long time to load.
– If the object is on a remote machine and loading it over the network may be slow,
especially during peak network load periods.
– If the object has limited access rights, the proxy can validate the access permissions
for that user.
• Proxies can also be used to distinguish between requesting an instance of an
object and the actual need to access it. For example, program initialization
may set up a number of objects which may not all be used right away. In that
case, the proxy can load the real object only when it is needed.
pf3
pf4

Partial preview of the text

Download Proxy Pattern: A Simple Place-holder for Complex Objects - Prof. Alan L. Sussman and more Study notes Programming Languages in PDF only on Docsity!

1

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2007

Apr. 10, 2007Proxy Pattern

Example

  • a network).display a large image (perhaps overA program needs to load and Very slow process

completely loaded.postponed until the image isactual image display can bescreen lays out correctly, but theimage is to be displayed so that themust be some indication that anWhen the program starts, there

images are available.around the images even before theand web browsers that lay out textprograms such as word processorsThis is particularly important in 3

A Proxy

An image

proxy

can begin

appears.extent on the screen before itsymbol to represent the image’ssimple rectangle or otherbackground, while drawing aloading the image in the

only then begin the process.receives a paint request, andloading the image at all until itThe proxy can even delay

A Proxy Pattern

simpler one.The Proxy pattern is used when you need to represent a complex object by a

you to postpone this creation until you need the actual object.If creating an object is expensive in time or computer resources, Proxy allows

object.object is loaded, it passes on the method calls from the Proxy to the actualA Proxy usually has the same methods as the object it represents, and once the

  • There are several cases where a Proxy can be useful: If an object, such as a large image, takes a long time to load.

especially during peak network load periods.If the object is on a remote machine and loading it over the network may be slow,

for that user.If the object has limited access rights, the proxy can validate the access permissions

case, the proxy can load the real object only when it is needed.may set up a number of objects which may not all be used right away. In thatobject and the actual need to access it. For example, program initializationProxies can also be used to distinguish between requesting an instance of an

5

Strategy

on a JPanel when it is loaded.We create a simple program to display an image

loading is completed.draw a rectangle around the image area untilclass we call ImageProxy to defer loading andRather than loading the image directly, we use a

the enclosing JPanel as we would an actual image.we would have for an Image, and that we add it toWe create the instance of the ImageProxy just as

Lets See Some Code!

7

Designing the ImageProxy Class

process within the constructor.creates a MediaTracker object to follow the loadingThe ImageProxy class sets up the image loading and

– of a number of media objects.The MediaTracker class is a utility class to track the status

loaded in preference to those with a higher ID number.that can be waited on independently. Images with a lower ID arefetched. It can also be used to identify unique subsets of the imagesidentifier controls the priority order in which the images are–^ In addition, each image can be assigned a unique identifier. Thiscall its addImage method for each image to be tracked.–^ To use a media tracker, create an instance of MediaTracker andcurrently only images are supported.^ Media objects could include audio clips as well as images, though

The Code

13

Copy-on-write

that may or may not change.You can also use proxies to keep copies of large objects

– Proxy can decide there is no reason to make a copy yet.If you create a second instance of an expensive object, a

It simply uses the original object.

the new instance.Proxy can copy the original object and make the change inThen, if the program makes a change in the new copy, the

not always change after they are instantiated.This can be a great time and space saver when objects do

Summary of Structural Patterns

The (^) Adapter (^) pattern, used to change the interface of one class to that of another one.

The (^) Bridge (^) pattern, intended to keep the interface to your client program use. You can then change the interface and the underlying class separately.constant while allowing you to change the actual kind of class you display or

The (^) Composite (^) pattern, a collection of objects, any one of which may be either itself a Composite, or just a primitive object.

The (^) Decorator (^) pattern, a class that surrounds a given class, adds new capabilities to it, and passes all the unchanged methods to the underlying class.

The (^) Façade (^) pattern, which groups a complex object hierarchy and provides a new, simpler interface to access those data.

The (^) Flyweight (^) pattern, which provides a way to limit the proliferation of class and passing it in during various execution methods.small, similar class instances by moving some of the class data outside the

The (^) Proxy (^) pattern, which provides a simple place-holder class for a more complex class which is expensive to instantiate.