1 / 7

Smart Reference Proxy

Structural Pattern: Proxy. Chapter 4 – Page 117. At times, a client needs to interact with an object without accessing it directly. The Proxy Pattern creates a surrogate object, which serves as an intermediary between the client and the target object. Remote Proxy

abel
Download Presentation

Smart Reference Proxy

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structural Pattern: Proxy Chapter 4 – Page 117 At times, a client needs to interact with an object without accessing it directly. 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 Firewall Proxy Protects targets from bad clients (and vice versa) Synchronization Proxy Provides multiple accesses to a target object Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object)

  2. The Proxy Pattern Chapter 4 – Page 118 The Subject defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected. The RealSubjectdefines 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.

  3. Proxy Examples Chapter 4 – Page 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. 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. 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. 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. 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: 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.

  4. C++ Example – No Proxy Pattern Chapter 4 – Page 120 #include <iostream> using namespace std; class Image { intimageID; static intnextID; public: Image() { imageID= nextID++; cout<< " constructing image " << imageID << endl; } ~Image() { cout << " destroying image " << imageID << endl; } voiddraw() { cout << " drawing image " << imageID << endl; } }; int Image::nextID = 1; void main() { Image images[5]; intindex; 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.

  5. C++ Example – w/Proxy Pattern Chapter 4 – Page 121 #include <iostream> using namespace std; classRealImage { intimageID; public: RealImage(intindex) { imageID= index; cout<< " constructing image " << imageID << endl; } ~RealImage() { cout << " destroying image " << imageID << endl; } voiddraw() { 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; intimageID; static intnextID; public: Image() { imageID= nextID++; // Initialized to null theRealImage= 0; } ~Image() { deletetheRealImage; }

  6. Chapter 4 – Page 122 voiddraw() { // When a request comes in, the real // object is created "on first use" if(!theRealImage) theRealImage= newRealImage(imageID); // The request is always delegated theRealImage->draw(); } }; int Image::nextID = 1; void main() { Image images[5]; intindex; 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; } }

  7. Proxy Pattern Advantages Chapter 4 – Page 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.

More Related