Design Mode learning notes-proxy Mode

Source: Internet
Author: User
Tags dedicated server

1. What is proxy?
The so-called proxy mode is to add an intermediate layer (proxy class) to manipulate another object we actually want to manipulate, just like a singer or a professional athlete's agent, the manipulated object can be constructed only after a long time, or because it is distributed in other locations of the network, we need to use a proxy to solve the problem of how to use these objects.
2. Our example
For example, in a remote network, a dedicated server is used to provide professional weather forecast information. We need to obtain the weather forecast of the cities we are interested in elsewhere on the network, we need to create a proxy class locally on the client to interact with the services on the remote weather forecast server, and open the Service locally according to the customized interface, we use the services opened by these interfaces to obtain the desired information. The following is our implementation.
1. Define the interface iweather. its role: When we call the weather method in the proxy class weatherproxy, the weather class does not necessarily implement all the methods. To force the weather class to implement all the methods, at the same time, in order to operate objects more transparently, we add an abstraction layer on the basis of the weather class and weatherproxy class, that is, they all implement interfaces with iweather.

Code
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace proxypatternweather
{
// Define an Interface
// Both the weather class and weatherproxy class must implement the imath interface.
Public interface iweather
{
String gettemperature (string City );
String getwind (string City );
String getairquality (string City );
}
}

2. Define the original implementation class weather

Code
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace proxypatternweather
{
Public class weather: iweather
{
// Obtain the corresponding data in the server database and return the result based on the parameter passing
// Implement the iweather interface in the weather class
Public String gettemperature (string City)
{
// Database operations
Return City + "the temperature is 23 degrees Celsius ";
}

Public String getwind (string City)
{
// Database operations
Return City + "southeast Level 2 ";
}
Public String getairquality (string City)
{
// Database operations
Return City + "excellent air quality ";
}
}
}

3. Define the proxy class weatherproxy

Code
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace proxypatternweather
{
Public class weatherproxy: iweather
{
Weather localweather;
Public weatherproxy ()
{
// Obtain data remotely through complex network operations
Localweather = new weather ();
}
// The iweather interface must also be implemented in the weatherproxy proxy class.
Public String gettemperature (string City)
{

Return localweather. gettemperature (city );
}

Public String getwind (string City)
{

Return localweather. getwind (city );
}
Public String getairquality (string City)
{

Return localweather. getwind (city );
}
}
}

4. Use a proxy to obtain relevant information

Code
Using System;
Using
System. Collections. Generic;
Using
System. LINQ;
Using
System. text;

NamespaceProxypatternweather
{
Class
Program
{
Static void main (string
[] ARGs)
{
Weatherproxy Wp = new
Weatherproxy ();
String city = "Beijing"
;
Console. writeline ("using weatherproxy to get weather conditions in Beijing on the current day :"
);
Console. writeline (WP. getairquality (city ));
Console. writeline (WP. gettemperature (city ));
Console. writeline (WP. getwind (city ));
CONsole. Readline ();

}
}
}

Running result:

Summary:
1. Proxy classification: based on the purpose of use
Remote proxy: Provides a local area for objects in different address spaces. This address space can be in this machine, but also in another machine. Remote proxy is also called ambassador ).
Virtual Proxy:Create an object with high resource consumption as needed, so that the object can be created only when necessary.
Copy-on-write Proxy:A type of virtual proxy. The replication (clone) operation is performed only when the client needs it.
Protection (protect or access) proxy:Control access to an object. If necessary, different users can be granted different levels of permissions.
Cache Proxy:Provides a temporary storage space for the results of a target operation so that multiple clients can share the results.
Firewall Proxy:Protection Target to prevent malicious users from approaching.
Synchronization Proxy:Allows several users to use an object at the same time without conflict.
Smart reference Proxy:When an object is referenced, additional operations are provided, such as recording the number of calls to this object.
In all types of proxy modes, virtual proxy, remote proxy, smart reference proxy, and protect or access) proxy is the most common proxy mode.
2. Examples of proxy modes:
(1) An object, such as a large image, needs to be loaded for a long time.
(2) A calculation result that takes a long time to complete, and the intermediate result must be displayed during the calculation process.
(3) It takes a long time to load a remote object on a remote computer, especially during peak network transmission.
(4) An object has only limited access permissions. The proxy mode can verify the user's permissions.

Go to: Design Mode Study Notes List

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.