1. Initial knowledge of Agent mode
Everyone in life must have encountered such a situation: for example, I want to buy a towel, it may be clean lascar, that the general People's words I should not go to clean lascar factory directly to buy it, but we in the Jie Li Ya store or what supermarket ah, these places to buy, these places is actually Jie Li ya towel agent. This is actually very similar to the proxy pattern in our oo.
An IT person, the Internet should be a regular thing, then there will always be such a situation, open a Web page, the text appears first, and those relatively large resources, such as the case of the film to be displayed, this is why?? In fact, this is the proxy mode.
2. A simple example of proxy mode
Just say the example of the towel above, there will be the following class diagram:
Although consumers buy the towel produced by Jie Lai Ya factory, indeed through the Jie Li Ya store to buy towels, not directly to the manufacturers to take goods, this is the proxy mode. Let's look at the code:
1//Defines the interface 2 interface saletowelintertace 3 {4 void Selltowel (); 5} 6//Towel Production Class 7 class Towelproduce:saletowelintert Ace 8 {9 string consumername;10 towelproduce (string consumer_name) one { consumername = Consumer_ name;13 }14 void Selltowel () { Console.WriteLine ("Towel sold {0}", consumername); }18}19// The agent for selling towels class Towelsaleproxy:saletowelintertace21 { towelproduce tp; Towelsaleproxy (string Consumner_name) (+ TP = new Towelproduce (consumer_name); }27 void Selltowel (): {29 Tp.selltowel (); 31}
Look at this code, you might think, then what is the use of this agent? There seems to be no specific benefits Ah!! In fact, the main advantage is that the actual purchase of towels customers and manufacturers do not exist, that is, reduce the coupling between them. This seems to be a bit abstract, and this is another key issue. So......
3. The meaning of proxy mode
Where does the agency model benefit??
Let's talk about the three roles in proxy mode.
abstract Role: a common interface for declaring real objects and proxy objects.
Proxy Role: the Proxy object role contains a reference to the real object, allowing you to manipulate the real object while the proxy object provides the same interface as the real object to replace the real object at any moment. At the same time, the proxy object can attach other actions when performing the real object operation, which is equivalent to encapsulating the real object.
Real role: the real object represented by the proxy role is the object we end up referencing.
One of the benefits of proxy mode is to provide a unified interface method to the external, while the proxy class implements the additional operation behavior of the real class in the interface, so that the system can be extended without affecting the external invocation. That is, I want to modify the actual role of the operation, try not to modify him, but outside in the "package" layer of additional behavior, that is, the proxy class. For example: interface A has an interface method operator (), the real role: Reala Implement Interface A, you must implement the interface method operator (). Client clients call interface A's connection method operator (). Now that the new demand is coming, we need to modify the operation behavior of the operator () in Reala. What do we do? If you modify the Reala will affect the stability of the original system, but also to re-test. This is required for the proxy class to implement additional behavior actions. Create the proxy Proxya implement interface A, and inject the real object Reala in. Proxya implements the interface method operator (), plus additional behavior can be added, and then invoke the real object's operator (). Thus achieved "to modify the closure, open to expansion", to ensure the stability of the system. We see that the client call is still the interface method operator () of interface A, except that the instance becomes the Proxya class. This means that the proxy mode implements the OCP principle.
4. When to use proxy mode
When the objects we need to use are complex or take a long time to construct, then we can use proxy mode. For example, if building an object is time consuming and computer resources, Proxy mode allows us to control this situation until we need to use the actual object. A proxy usually contains the same method as the object that will be used, and once the object is started, these methods are passed to the actual object by proxy. Some scenarios in which you can use proxy mode:
An object, such as a large image, takes a long time to load.
A calculated result that takes a long time to complete and needs to show intermediate results during its calculation
An object that exists on a remote computer requires a long time to load the remote object over the network, especially during periods of network transmission.
An object has limited access rights, and proxy mode can verify the user's permissions
Proxy mode can also be used to distinguish between an object instance request and the actual access, for example: in the program initialization process may establish multiple objects, but not all immediately use, proxy mode can be loaded into the real object required. This is a program that needs to load and display a very large image, when the program starts, it must determine the image to be displayed, but the actual image can only be displayed when fully loaded! In this case, we can use the proxy mode.
Proxy mode of design mode (proxies)