Design Pattern proxy

Source: Internet
Author: User
Proxy) Provides a proxy for other objects to control access to this object.


The hot summer is approaching. Are you enjoying this summer's watermelon? If you want to know what you can do in summer? So it must be the ice-cold ice watermelon! In the hot summer, it was the most pleasant way for many people to dig out half of the ice-town watermelon that had just been taken out of the refrigerator with a spoon. I believe everyone has done this too...
Today, I am not talking about how to eat watermelon, but where to buy it. I believe everyone has seen the roadside vendors drag a truck of watermelon to sell at a low cost. Generally, you can also buy watermelon in the supermarket. But where did these watermelons come from? Vendors? Supermarket? Of course not. You can see it later...


Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace proxy {// watermelon interface public interface iwatermelon {// method for producing watermelon void product (); // Method for selling watermelon void goods ();} // public class watermelonfactory: iwatermelon {public void product () {console. writeline ("farmer's uncle planted watermelon in the ground... ");} public void resume () {console. writeline ("farmer's uncle sells watermelon... ") ;}}// watermelon agent public class watermelonproxy: iwatermelon {// Buy watermelon private iwatermelon watermelon from the farmer's uncle; Public watermelonproxy (iwatermelon watermelon) {This. watermelon = watermelon;} public void product () {This. watermelon. product (); console. writeline ("the agent buys watermelon from the farmer's uncle... ");} public void resume () {console. writeline ("the agent buys watermelon at a low price from farmers and then sells it at a higher price to earn a certain price difference... ") ;}} class program {static void main (string [] ARGs) {// create a watermelon production plant iwatermelon watermelonfactory = new watermelonfactory (); // obtain the proxy object instance iwatermelon watermelonproxy = new watermelonproxy (watermelonfactory); // The agent produces watermelon (actually the farmer's uncle planted watermelon) watermelonproxy. product (); // The agent sells watermelon (Buy watermelon at a low price and then sell it at a higher price to earn a difference) watermelonproxy. else ();}}}




Class Diagram

The proxy mode includes the following three roles:
Subject (Abstract role): Declare the common interface of the real and proxy themes
Realsubject (real role): The target object that actually processes the request. The client can indirectly call the operations defined in the real topic role through the proxy role.
Proxy): The proxy object role contains references of real objects, so that the proxy object can convert requests to real objects for processing. At the same time, you can add additional operations before and after the proxy object executes the real object operation.


Main advantages:1. It can coordinate callers and callers, reducing the Coupling Degree of the system to a certain extent.
2. The client can program the abstract topic role. Adding or replacing the proxy class does not need to modify the source code, and complies with the open and closed principles. The system has good flexibility and scalability.

 
Main disadvantages:1. Because proxy objects are added between the client and the real topic, some types of proxy modes may cause slow request processing,
2. Implementing the proxy mode requires additional work, and the implementation process of some proxy modes is complicated.




Usage:1. remoteproxy provides a local proxy for an object in different address spaces.
2. virtual proxy (virtualproxy) if an object is time-consuming, it can be called through the proxy object. A fake call is returned before the real object is created, at this time, the response method call of a real object is returned to the client.
3. protectionproxy controls access to the original object.
4. smartreference replaces simple pointers and performs some additional operations when accessing objects.




Related mode:

Decorator: the decorator focuses on the modifier with additional behavior added.

Proxy: The focus of proxy is to replace my job and reduce operations on actual objects.












Design Pattern proxy

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.