The scenario of the article is taken from the daily work. First, let's give a rough description of the general workflow of a small company. The demand side needs to change what needs and add what functions, and find the programmer to change them directly.
This scenario is represented by code.
Class demand // demand side
{
// In the traditional mode, the demand side directly finds the programmer to change the function
Coder code;
Public demand (coder code)
{
This. code = code;
}
Public void GetCount ()
{
Code. GetCount ();
}
Public void GetDataInterface ()
{
Code. GetDataInterface ();
}
}
Class coder // programmer
{
Public string Name {get; set ;}
Public void GetCount ()
{
Console. WriteLine ("count the number of data items ");
}
Public void GetDataInterface ()
{
Console. WriteLine ("helping provide a data interface ");
}
}
Class Client
{
// Directly call
Coder code = new coder ();
Demand d = new demand (code );
D. GetCount ();
D. GetDataInterface ();
}
If our programmers do not want the demand side to find us and often interrupt our thinking, we can consider using the proxy mode to set up products between the demand side and the programmer.
Find a programmer for the product, and then modify or add the required function.
First:
The demand side first looks for the product, and the product looks for the programmer. The product is equivalent to the agent role.
Large companies or companies with relatively standardized processes generally use the agency model to complete the project.
Public abstract class Abstactcoder
{
Public abstract void GetCount ();
Public abstract void GetDataInterface ();
}
Class CoderA: Abstactcoder
{
Public override void GetCount ()
{
Console. WriteLine ("number obtained by proxy ");
}
Public override void GetDataInterface ()
{
Console. WriteLine ("interface for obtaining data through proxy ");
}
}
Class Proxy: Abstactcoder
{
/* The demand side cannot directly find programmers or UIS. If you want to find them, people will ignore you. You must go through the product department. The role of Proxy here is equivalent to that of the product department,
* If the demand side wants to change something, you must first contact the product department and then contact the product department to find a programmer to do the specific work.
* This avoids the demand side from finding programmers directly. The programmer's work requires concentrated energy, and the idea cannot be interrupted. the interruption of thinking requires restarting. This is terrible, so we need to act as an agent (product department)
*/
Private Abstactcoder abstactcoder;
Public Proxy ()
{
Abstactcoder = new CoderA ();
}
Public override void GetCount ()
{
Abstactcoder. GetCount ();
}
Public override void GetDataInterface ()
{
Abstactcoder. GetDataInterface ();
}
}
Class Client
{
// Call the programmer www.2cto.com by proxy
Proxy proxya = new Proxy ();
Proxya. GetCount ();
Proxya. GetDataInterface ();
Console. Read ();
}
Proxy mode application scenarios:
1. Security Proxy: add some permission judgment and control in the proxy class
2. remote proxy. If. net references webservice, some proxy files will be generated.
3. The virtual proxy provides a placeholder, but the image is not directly displayed.
Finally, the proxy mode is to find a person to work for, but not directly find a person to work for, and find a man-in-the-middle. The man-in-the-middle is not concerned with how the man-in-the-middle (agent) works, just care about the final work!
The man-in-the-middle is responsible for the reception, but does not complete the specific work tasks. He will assign the task of finding the man-in-the-work to others to complete!
In this case, the proxy is a poor job, and the proxy has nothing to do!
From Allen