Design Mode (12) PROXY mode (PROXY)

Source: Internet
Author: User

Problem focus: This mode is the last of the object structure mode. After learning, you can enter the following type of mode. The main purpose of the proxy mode is to control access to an object (for example, to control access permissions by rejecting some access requests, or to optimize loading and other time-consuming operations ). Intent: provide a proxy for other objects to control access to this object.
Alias: Surrogate
Motivation: cause: To create and initialize an object only when it is required, access control is implemented for the object. Demo: Document editor requirement: You can embed graphical objects in documents. Challenge: Some graphical objects are expensive to create, but you must open the document quickly, therefore, we should avoid creating all the objects with high overhead at one time when opening the document. Therefore, not all these documents are visible at the same time, so there is no need to create these objects at the same time. Solution: an object with high overhead should be created as needed. When an image becomes visible, this requirement is generated. In this case, an image Proxy is used to replace the real image and instantiate the image object as needed. Image Proxy creates a real image only when the document editor activates the Draw operation of the image Proxy to display the image. Implementation:

  • The file name is used as a reference to the actual object.
  • Size of the image stored by the proxy (extent)
  • The document editor uses Abstract Graphic class-defined interfaces to access embedded images. ImageProxy is an image proxy class.
  • ImageProxy saves the file name as a pointer to the image file on the disk. The file name is passed to ImageProxy as a parameter.
  • ImageProxy also stores the border of the Image and guides to the real Image instance until the proxy instantiates the real Image.
  • The ImageProxy operation transmits requests to an image only after it is instantiated. Class diagram: Applicability: Generally, Proxy is used when a common and complex object pointer is needed to replace a simple pointer. The following are some common cases that can use the Proxy mode:
    • Remote Proxy: provides a local proxy for an object in different address spaces.
    • Virtual Proxy: create objects with high overhead as needed. The ImageProxy mentioned in the previous section is an example of this kind of proxy.
    • Protection Proxy: controls access to the original object. It is used when the object should have different access permissions.
    • Intelligent Guidance: instead of a simple pointer, it performs some additional operations (reference counting, loading memory, locking) when accessing the object ). Structure:
      The following figure shows the objects of a possible proxy structure at runtime:

      Participants:
      • Proxy (ImageProxy ):
        • Save a reference so that the proxy can access the object.
        • Provides the same interface as the Subject interface, so that the proxy can be used to replace the object.
        • Controls access to an object and may be responsible for creating and deleting it.
        • Subject (Graphic ):
          • Defines the common interfaces of RealSubject and Proxy, so that Proxy can be used in any place where RealSubject is used.
          • RealSubject (Image ):
            • Define the entity collaboration represented by the Proxy: the Proxy forwards requests to RealSubject as appropriate based on its type.
              Effect: the Proxy mode introduces a certain degree of intermittent (one middle layer) when accessing objects. In this middle layer, you can perform some additional actions or optimization. CZ operator? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys/keys + qz/q + keys/z0dOz2dXi0ru/vb1_uf2zzkosztldx7/keys/L340NC/keys Authorization + authorization/OqsHjyrGjrMq1zOW9q7G7yb6z/aGjCiAgICDQp7n7o7q/ydLUtPO3 + authorization + qz/authorization/z1/fOqsq1vMq21M/authorization/e3 + authorization Release/dK7uPaz6c/zvdO/2rSmwO3L/release + release/zoaO1q8rHyOe5 + 7T6wO221M/release/zo6zEx8O0y/release + release/release "B Rush: java; "> class Graphic {public: virtual ~ Graphic (); virtual void Draw (const Point & at) = 0; virtual void HandleMouse (Event & event) = 0; virtual const Point & GetExtent () = 0; virtual void Load (istream & from) = 0; virtual void Save (ostream & to) = 0; protected: Graphic ();}; // Image class implements the Graphic interface used to display Image files. // At the same time, the Handlemouse operation is implemented so that users can adjust the image size in the interactive step. Class Image: public Graphic {public: Image (const char * file); virtual void Draw (const Point & at); virtual void HandleMouse (Event & event ); virtual const Point & GetExtent (); virtual void Load (istream & from); virtual void Save (ostream & to); protected ://....} // ImageProxy and Image have the same interface class ImageProxy: public Graphic {public: virtual ~ ImageProxy (); virtual void Draw (const Point & at); virtual void HandleMouse (Event & event); virtual const Point & GetExtent (); virtual void Load (istream & from ); virtual void Save (ostream & to); protected: Image * GetImage (); private: Image * _ image; Point _ extent; char * _ fileName ;}; // The constructor saves the local copy of the file name for storing the image, and initializes _ extent and _ imageImageProxy: ImageProxy (const char * fileName) {_ fileName = strdup (fileName ); _ exte Nt = Point: Zero; _ image = 0;} Image * ImageProxy: GetImage () {if (_ image = 0) {_ image = new Image (_ fileName) ;}// GetExtent returns the cached image size; otherwise, the Image is loaded from the file. Const Point & ImageProxy: GetExtent () {if (_ extent = Point: Zero) {_ extent = GetImage ()-> GetExtent ();} return _ extent ;} // Draw is used to load the image void ImageProxy: Draw (const Point & at) {GetImage ()-> Draw (at);} // HandleMousevoid ImageProxy :: handleMouse (Event & event) {GetImage ()-> HandleMouse (event);} // Save the cached image size and file name in a stream void ImageProxy :: save (ostream & to) {to <_ extent <_ fileName;} // Load to obtain this information and initialize the corresponding member function void ImageProxy: Load (istream & from) {from> _ extent> _ fileName;} // assume there is a client that contains the Graphic object class TextDocument {public: TextDocument (); void Insert (Graphic *);};After setting the class, We can insert the ImageProxy to a text file in the following way.
              TextDocument* text = new TextDocument;// ...text->Insert(new ImageProxy("anImageFileName"));


              Related mode:
              • Adapter mode: provides a different interface for the object it adapts. The proxy mode provides interfaces that are basically the same as the actual object (because the access protection proxy may refuse to perform operations on the actual object, so its interface may actually be a subset of the object interface ).
              • Decorator mode: The purpose is different. The decorator adds one or more functions to the object, while the proxy mode controls the access to the object.
                Reference: Design Pattern: Basis for reusable Object-Oriented Software

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.