Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
202
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

Proxy

Also known as Surrogate

Pattern Properties

Type: Structural

Level: Component

Purpose

To provide a representative of another object, for reasons such as access, speed, or security.

Introduction

Your career is taking off and your social life is humming, so your Personal Information Manager has to manage many appointments and dates. Your address book contains all the addresses of all the people you’ve ever socialized with or had professional contact with, including information on their families, hobbies, and other potentially valuable data. The number of contacts started out small but is now in the thousands.

However, you often pull out your PIM just to change a meeting time, make a note to buy beer, or something equally simple. Being presented with the whole address book object every time you use the PIM would be unnecessary and annoying. Just opening the book is a very expensive operation, unnecessarily delaying activities that don’t require its use.

As a user, you don't care about any of that—you just want the address book to be available when you need to use it. (Ideally, the address book should be there even before you know you need to use it.) And when you use it, you don’t always need all of it. For example, you just want to know how many contacts you have in your address book, or you want to add a new contact to your addressbook, without seeing and being able to edit the whole thing. You just need a small part of the address book.

The solution is a placeholder object that provides the an interface to the address book, or a part of it. The placeholder looks like the address book, but doesn’t involve the overhead of running it. However, when you do need the whole address book to perform a task like updating a colleague’s address, the placeholder object creates the real address book, to perform address book tasks assigned to it. That placeholder object is a Proxy.

Applicability

Use the Proxy pattern when you need a more elaborate reference to an object instead of just a regular one:

Remote proxy – When you need a local representative for an object in another address space (JVM).

Virtual proxy – Acts as a placeholder and delays creating expensive objects. (This is the version described in the Introduction section.)

Protection proxy – Determines access rights to the real object.

Description

A proxy (or stub) is a representative for another object. To enable the proxy to represent the real object, the proxy has to implement the exact same interface as the real object. Furthermore, the proxy keeps a reference to the real object. The proxy needs the reference to the real object so that it can call methods on the real object if necessary. The clients will be interacting with the proxy, but the proxy can delegate the execution to the real object. The proxy implements the same interface as the real object, but can perform tasks that the real object does not, such as remote communication or security.

The proxy is a sort of stand-in for the real object. You can compare the Proxy pattern to the system of filming dangerous stunts for movies. The proxy is the body double for the real object, the movie star. During the dangerous stunts the proxy jumps out of the plane instead of the real object. Because the proxy implements the same interface as the real object, audiences cannot tell the difference, and think that real object jumped. But when the camera switches to close-up (when the real object is needed for a full complement of movie star tasks), the proxy calls the real object to perform the acting work.

134