The proxy mode of C # design mode (iv)

Source: Internet
Author: User

15.7 proxy mode effects and applicable scenarios

Proxy mode is one of the commonly used structural design patterns, which provides a solution for indirect access to objects, which can control the access of objects. There are many types of proxy modes, including remote agent, virtual agent and protection agent, which are widely used in software development.

15.7.1 Mode Advantages

The common advantages of the proxy mode are as follows:

(1) It can coordinate the caller and the callee, and reduce the coupling degree of the system to some extent.

(2) The client can program the abstract topic role, add and replace the proxy class without modifying the source code, in line with the open and closed principle, the system has a good flexibility and scalability.

In addition, different types of proxy modes have unique advantages, such as:

(1) The Remote Agent provides an implementation mechanism for access to two different address space objects, which can move objects and operations with more resources to a better performance computer and improve the overall efficiency of the system.

(2) The virtual agent can save the running cost of the system to some extent by means of an object that consumes less resources to represent an object that consumes more resources.

(3) The buffer Agent provides temporary cache storage for the results of an operation, which can be used to share these results, optimize system performance, and shorten execution time.

(4) The protection agent can control access to an object and provide different levels of usage rights for different users.

15.7.2 Mode Disadvantages

The main disadvantages of the proxy mode are as follows:

(1) Because proxy objects are added between the client and the real topic, some types of proxy patterns can cause requests to be processed more slowly, such as protection agents.

(2) The implementation of Proxy mode requires additional work, and some proxy modes are more complex to implement, such as remote agents.

15.7.3 Mode for scenarios

There are many types of proxy modes, and different types of proxy modes have different advantages and disadvantages, and they are applied to different situations:

(1) A remote agent can be used when a client object needs to access an object in the remote host .

(2) When it is necessary to use an object that consumes less resources to represent an object that consumes more resources, thereby reducing the overhead and running time, you can use a virtual proxy, such as when an object takes a long time to finish loading.

(3) A buffer agent can be used when it is necessary to provide a temporary storage space for a frequently accessed operation result for multiple clients to share access to these results . By using a buffer proxy, the system does not have to re-perform the operation every time the client accesses, just get the result of the operation directly from the temporary buffer.

(4) When you need to control access to an object, you can use a protection agent when you provide different levels of access to different users .

(5) You can use smart reference proxies when you need to provide some extra action for an object's access (reference) .

Exercises

1. The application shortcut in the Windows operating system is an application instance of () mode.

A. Agent B. Combination (Composite)

C. Decoration (Decorator) D. Appearance (facade)

2. Graduates find work through employment introduction, what kind of design pattern is involved in this process, draw corresponding class diagram.

3. In an application, it is necessary to record the call log of the business method, to provide a logging proxy class for each class without modifying the existing business class, and to output the log in the proxy class, such as the Output "method () called before the Business method methods () Call , The call time is 2012-11-5 10:10:10 ", after the call, if there is no throw exception, the output" method methods () call succeeds ", otherwise the output" methods method () call failed. " Call the business method of the real business class in the proxy class, use the proxy mode to design the structure of the logging module, draw the class diagram and use the C # language programming simulation implementation.

4. A software company to develop a network image viewer based on C/s, the specific functions described as follows: The user only need to enter the Web page URL in the picture Viewer, the program will automatically download all the pictures of the page to the local, considering that some of the page pictures are more, and some of the picture file is larger, Therefore, the image will be displayed as an icon, different types of pictures using different icons, and the icon below the image of the file name, the user click on the icon to see the real picture, the interface effect 15-7 is shown. Try to design and implement the picture Viewer using virtual proxy mode. (Note: You can use a multithreaded mechanism to display small icons with one thread while starting another thread to load the original image in the background.) )

Figure 15-7 Picture Viewer interface

The proxy mode of C # design mode (iv)

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.