Вы находитесь на странице: 1из 25

Design

Design Patterns
Patterns
CE00362-3
CT070-3-3

Structural Pattern Proxy


Introduction & Overview

CT070-3-3 - Design Patterns Proxy Slide 1 (of 24)


Learning Outcomes

At the end of this session, you should be


able to;

Describe benefits of Proxy pattern


Recognise and apply Proxy pattern given a
scenario
Compare the Adapter pattern and the
Proxy pattern

CT070-3-3 - Design Patterns Proxy Slide 2 (of 24)


Proxy
Name: Proxy
Problem:
Provide a surrogate or placeholder for another object to control
access to it
Also known as Surrogate
Context
It can happen, though, that a legitimate object cannot live
up to this ordinary responsibility. This occurs most
frequently when an object takes a long time to load or
when the object is running on another computer.

CT070-3-3 - Design Patterns Proxy Slide 4 (of 24)


Description - Proxy

Applicability:
Use Proxy pattern when

an object, such as a large image, takes a long time to


load.

the object is on a remote machine and loading it over the


network may be slow, especially during peak network
load periods.

the object has limited access rights, the proxy can


validate the access permissions for that user.

CT070-3-3 - Design Patterns Proxy Slide 5 (of 24)


Definition - Proxy

A proxy is an object that is a stand-in for


another object - the system talks to the
object, when necessary, via the proxy

CT070-3-3 - Design Patterns Proxy Slide 6 (of 24)


What is a Proxy?

In human terms a proxy is a person who is


authorized to act on another persons behalf
The proxy does what is possible on his own
But at some stage communicates with the other
person before making a decision
There are many reasons to take advantage of a
proxy
One may be to delay the instantiation of an object
Maybe because the object is expensive to instantiate

CT070-3-3 - Design Patterns Proxy Slide 7 (of 24)


Proxy-Real Time Example

CT070-3-3 - Design Patterns Proxy Slide 1 (of 18)


When should I Use Proxy?

This pattern is recommended when either of the


following scenarios occur in your application:
The object being represented is external to the
system.
Objects need to be created on demand.
Access control for the original object is required
Added functionality is required when an object is
accessed.

CT070-3-3 - Design Patterns Proxy Slide 1 (of 18)


Types of Proxy Implementation
Virtual Proxies: delaying the creation and initialization
of expensive objects until needed, where the objects are
created on demand (For example creating the
RealSubject object only when the doSomething method
is invoked).
Remote Proxies: providing a local representation for an
object that is in a different address space. A common
example is Java RMI stub objects. The stub object acts
as a proxy where invoking methods on the stub would
cause the stub to communicate and invoke methods on
a remote object (called skeleton) found on a different
machine.

CT070-3-3 - Design Patterns Proxy Slide 1 (of 18)


Types of Proxy Implementation

Protection Proxies: where a proxy controls access to


RealSubject methods, by giving access to some objects
while denying access to others.

Smart References: Loading an object from database


into memory on demand.

CT070-3-3 - Design Patterns Proxy Slide 1 (of 18)


The Proxy Pattern-Class Diagram

Structure

CT070-3-3 - Design Patterns Proxy Slide 19 (of 24)


The Proxy Pattern
Solution
Proxy
Maintains a reference to access the real subject
Provides an interface identical to the Subjects so
that a proxy can be substituted for a real subject
Controls access to the real subject and may be
responsible for creating and deleting it

CT070-3-3 - Design Patterns Proxy Slide 17 (of 24)


The Proxy Pattern

Subject
Defines the common interface for RealSubject and
Proxy
a Proxy can be used anywhere a RealSubject is
expected
RealSubject
Defines the object that the proxy represents

CT070-3-3 - Design Patterns Proxy Slide 18 (of 24)


The Adapter and Proxy Patterns

How do these two patterns differ?


An adapter provides a different interface to
the object that it adapts
Whereas a proxy provides the same
interface as its subject
When a proxy is used for access protection it
may allow only a subset of the subjects
interface to some clients
Restricting access to the subject based upon
the access rights of the client
CT070-3-3 - Design Patterns Proxy Slide 20 (of 24)
An Image based Example

Loading an image is often slow, particularly if the


image is complex
Suppose you were building an interface that
involved a whole set of images displayed in
tabbed panes

CT070-3-3 - Design Patterns Proxy Slide 8 (of 24)


An Image based Example

As you click on a tab a different image is displayed


In order to build this, each of the tabbed panes has to be
constructed from an image
Each of the images has to be loaded from a file
JTabbedPane tabbedPane = new JTabbedPane();
JLabel label = new JLabel(new ImageIcon(fileName));
// filename appears as the tab
tabbedPane.add(fileName, label);
All of the tabbedPanes would be built before the frame is
displayed

CT070-3-3 - Design Patterns Proxy Slide 9 (of 24)


Displaying the tabbedPane

JFrame frame = new JFrame();


Container contentPane = frame.getContentPane();
contentPane.add(tabbedPane);
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();

Adding the images may be slow


Unless the user selects a tab the associated image will not be
seen
Why not defer construction of the images until needed?

CT070-3-3 - Design Patterns Proxy Slide 10 (of 24)


Using the ImageProxy

JLabel interface
Icon

ImageProxy ImageIcon

1 1

CT070-3-3 - Design Patterns Proxy Slide 15 (of 24)


How can we defer image
Loading?
Examine how the image is actually loaded
A JLabel is used to manage an ImageIcon
JLabel label = new JLabel(new ImageIcon(fileName));
An ImageIcon is built from a file
The ImageIcon class is a class that implements the
Icon interface
A proxy that implements the Icon interface could be
used in place of an ImageIcon
The proxy could delay building an ImageIcon until the
Jlabel object calls on the Icon interface
For example asking the proxy to paintIcon in response
to a user selecting a tab

CT070-3-3 - Design Patterns Proxy Slide 11 (of 24)


The Proxy Class - ImageProxy
public class ImageProxy implements Icon {
private String imageFile;
private ImageIcon image;

public ImageProxy(String filename) {


imageFile = filename;
image = null;
}
public int getIconHeight() {
loadImage();
return image.getIconHeight();
}
public int getIconWidth() {
loadImage();
return image.getIconWidth();
}

CT070-3-3 - Design Patterns Proxy Slide 12 (of 24)


The Proxy Class - ImageProxy

public void paintIcon(Component c, Graphics g, int x,int y){


loadImage();
image.paintIcon(c, g ,x , y);
}

private void loadImage(){


if (image == null) // i.e., image not loaded
{
System.out.println("loading image");
image = new ImageIcon(imageFile);
}
}
}

CT070-3-3 - Design Patterns Proxy Slide 13 (of 24)


Using the ImageProxy

A JLabel can now make use of an ImageProxy


JLabel label = new JLabel(new
ImageProxy(fileName));
The loading of the image is then delayed until the
image is actually needed
If it is not used then it is not loaded
This is catered for by the ImageProxy class that
looks and behaves like an ImageIcon but in addition
manages the loading of the image
The image is loaded on demand

CT070-3-3 - Design Patterns Proxy Slide 14 (of 24)


Summary

In this lecture we have:


Introduced the Proxy pattern
Discussed examples that take advantage
of the Proxy pattern
Compared the Adapter pattern and the
Proxy pattern

CT070-3-3 - Design Patterns Proxy Slide 22 (of 24)


Question and Answer Session

Q&A

CT070-3-3 - Design Patterns Proxy Slide 23 (of 24)


References

Steven John Metsker, Design Patterns Java


Workbook, Addison Wesley

Erich Gamma et. al., Design Patterns


Elements of Reusable Object-Oriented
Software, Addison Wesley

CT070-3-3 - Design Patterns Proxy Slide 24 (of 24)

Вам также может понравиться