smart reference proxy provides additional actions whenever an object is referenced (e.g., counting...

7
Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy Protects targets from bad clients (and vice versa) Synchronization Proxy Provides multiple accesses to a target object Structural Pattern: Proxy At times, a client needs to interact with an object without accessing it directly. Chapter 4 – Page 1 The Proxy Pattern creates a surrogate object, which serves as an intermediary between the client and the target object. Remote Proxy Provides a reference to an object that is located in a different address space Virtual Proxy Delays the creation of a memory-intensive object until it is absolutely necessary Protection Proxy Provides different clients with different levels of access to a target object Cache Proxy Provides temporary storage of expensive target operations so multiple clients can share the results

Upload: domenic-todd

Post on 11-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy

Smart Reference ProxyProvides additional actions

whenever an object is referenced (e.g., counting the number of

references to the object)

Firewall Proxy

Protects targets from bad clients (and vice

versa)

Synchronization Proxy

Provides multiple accesses to a target object

Structural Pattern: ProxyAt times, a client needs to interact with an object without accessing it directly.

Ch

ap

ter 4

– Pag

e

1

The Proxy Pattern creates a surrogate object, which serves as an intermediary between the client and the target object.Remote Proxy

Provides a reference to an object that is located in a

different address space

Virtual ProxyDelays the

creation of a memory-

intensive object until it is

absolutely necessary

Protection ProxyProvides

different clients with different

levels of access to a target

object

Cache ProxyProvides temporary storage of expensive target operations so multiple clients can

share the results

Page 2: Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy

The Proxy PatternC

hap

ter 4

– Pag

e

2

The Subject defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.The RealSubject defines the real object that the proxy represents.The Proxy maintains a reference that allows it to access the RealSubject, with an interface identical to the RealSubject’s so the Proxy can, in fact, be substituted for the RealSubject.

The Proxy controls access to the RealSubject and may be responsible for creating and deleting it.

Proxy

Request()

RealSubject

Request() +realSubject

ClientSubject

Request()

realSubject.Request()

Page 3: Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy

Proxy ExamplesC

hap

ter 4

– Pag

e

3Firewall Proxy

While primitive firewalls protect internal

networks from external networks by merely

inspecting addresses and port numbers, more

thorough traffic inspection might be

needed to guard against such violations as

improper commands.

Remote Proxy

A machine has several

utility services

running as daemons on well-known ports, but

various client

machines need to be

able to access these services as if

they were local objects.

Virtual ProxyA large

collection object (e.g., a

hash table) needs to be

accessed concurrently by multiple clients, and one client wants to perform several

consecutive fetch

operations without

letting any other client

add or remove objects.

Cache ProxyAn Internet Service

Provider notices that many of its

clients are frequently

accessing the same web pages, resulting in

multiple copies of the web documents being transmitted through its server.

Solution:Use a lock

object for the collection; have the

client implement a

method which obtains the

lock, performs its fetches and then releases

the lock.

Solution:The ISP's server

can cache recently accessed pages and

when a client request arrives, the server can check to see if the document

is already in the cache and then

return the cached copy. The ISP's

server accesses the target web server

only if the requested

document is not in the cache or is out

of date.

Solution:Translate the

client’s queries into remote calls, receive the results of the query from the remote

object, and forward

them to the client.

Solution:Firewall proxy servers force both ends of a

connection to conduct the session through the proxy by creating and

running a process on the firewall that mirrors a

service as if it were running on the end host.

Page 4: Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy

C++ Example – No Proxy Pattern

Ch

ap

ter 4

– Pag

e

4

#include <iostream>using namespace std;

class Image{ int imageID; static int nextID; public: Image() { imageID = nextID++; cout << " constructing image " << imageID << endl; } ~Image() { cout << " destroying image " << imageID << endl; } void draw() { cout << " drawing image " << imageID << endl; }};

int Image::nextID = 1;

void main(){ Image images[5]; int index; cout << "Enter 0 for Exit, 1-5 for Image: "; cin >> index; while (index != 0) { images[index - 1].draw(); cout << "Enter 0 for Exit, 1-5 for Image: "; cin >> index; }}

Note the start-up and shut-down overhead, even for images that were never accessed.

Page 5: Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy

C++ Example – w/Proxy Pattern

Ch

ap

ter 4

– Pag

e

5

#include <iostream>using namespace std;

class RealImage { int imageID; public: RealImage(int index) { imageID = index; cout << " constructing image " << imageID << endl; } ~RealImage() { cout << " destroying image " << imageID << endl; } void draw() { cout << " drawing image " << imageID << endl; }};

// Design an "extra level of indirection" wrapper classclass Image { // The wrapper class holds a pointer to the real class RealImage *theRealImage; int imageID; static int nextID; public: Image() { imageID = nextID++; // Initialized to null theRealImage = 0; } ~Image() { delete theRealImage; }

Page 6: Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy

Ch

ap

ter 4

– Pag

e

6 void draw() { // When a request comes in, the real // object is created "on first use" if (!theRealImage) theRealImage = new RealImage(imageID); // The request is always delegated theRealImage->draw(); }};

int Image::nextID = 1;

void main(){ Image images[5]; int index; cout << "Enter 0 for Exit, 1-5 for Image: "; cin >> index; while (index != 0) { images[index - 1].draw(); cout << "Enter 0 for Exit, 1-5 for Image: "; cin >> index; }}

Page 7: Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy

Proxy Pattern Advantages

Ch

ap

ter 4

– Pag

e

7

• The Proxy Pattern provides an additional level of indirection to support distributed, controlled, or intelligent access to the target object, protecting the target from undue complexity.

• While the Adapter Pattern provides a different interface to its subject and the Decorator Pattern provides an enhanced interface to its subject, the Proxy Pattern provides the same interface that is normally used by the subject.

• Proxies are useful whenever there is a need for a more sophisticated reference to an object than can be provided by a simple pointer.