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 ):