Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy."— Presentation transcript:

1

2 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. C h a p t e r 4 – P a g e 117 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

3 The Proxy Pattern C h a p t e r 4 – P a g e 118 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.

4 Proxy Examples C h a p t e r 4 – P a g e 119 Firewall 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 Proxy A 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 Proxy An 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.

5 C++ Example – No Proxy Pattern C h a p t e r 4 – P a g e 120 #include 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.

6 C++ Example – w/Proxy Pattern C h a p t e r 4 – P a g e 121 #include 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 class class 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; }

7 C h a p t e r 4 – P a g e 122 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; }

8 Proxy Pattern Advantages C h a p t e r 4 – P a g e 123 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.


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

Similar presentations


Ads by Google