Overview
In a software system, some objects sometimes fail to or do not want to directly access another object because they span the network or other obstacles. If they are directly accessed, unnecessary complexity is brought to the system, at this time, an intermediate layer can be added between the client program and the target object, so that the proxy object can replace the target object and break everything. This is the Proxy mode described in this article.
Intention
Proxy mode provides a Proxy for an object, and the Proxy object controls the reference to the original object.
Proxy mode is called Proxy or Surrogate in English and can be translated into "Proxy" in Chinese ". The so-called proxy means one person or one institution takes action on behalf of another person or another institution. In some cases, a client does not want or cannot directly reference an object, and the proxy object can play a mediation role between the client and the target object.
<Design Pattern> Proxy Pattern structure
Figure 1 Structure of Proxy Mode
Proxy type
If the proxy is divided by purpose, there are the following types of proxies:
Remote Proxy: provides a Local Area Representative object for an object in a different address space. This address space can be in this machine, but also in another machine. Remote proxy is also called Ambassador ).
Virtual Proxy: creates an object with high resource consumption as needed, so that the object is created only when necessary.
Copy-on-Write Proxy: a virtual proxy. The replication (clone) operation is performed only when the client needs it.
Protection (Protect or Access) proxy: controls Access to an object. If necessary, different users can be granted different levels of Access 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: protects the target from malicious users.
Synchronization Proxy: enables several users to simultaneously use an object without conflict.
Smart Reference Proxy: provides some additional operations when an object is referenced, 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.
Examples in Journey to the West
Although the eight precepts were not called at that time, they were still called here for convenience.
Story of Gao laozhuang
However, during the Spring Festival, Wukong took a white horse and traveled west with Tang Miao. One day later, the sky will be late, far from seeing a village people, this is Gao Lao Zhuang, pig Ba Jie's man Gao Taigong's home. In order to save Miss Gao's three masters, Wukong decided to dress up as Miss Gao, and later met the Monster:
"The Walker turned to the magic and changed his body, just like the woman. She sat alone in the room and waited for the goblin. In a few moments, there is a storm of wind ...... When the storm passed, I saw a fairy in the air, and it was really ugly: black face, short hair, long-beam ears, wearing a green, blue, blue, no blue shuttle straight, fasten a cloth towel ...... When you walk into the room, you have to put your eyes on it ...... "
The appearance of Miss Gao's three gods and herself
Wu kong's start is to separate the God appearance of Miss Gao's three masters from her, which is similar to the principle of "Opening and Closing. In this way, "Miss Gao jia" becomes the specific implementation of "Miss Gao Jia San Shen appearance", and "Miss Gao Jia San Shen appearance" becomes an abstract role, as shown in.
Wukong assumes and replaces Miss Gao's three roles
Wukong cleverly realized "the appearance of Miss Gao's three gods", that is, it also became a subclass of "the appearance of Miss Gao's three gods. Wukong can assume the role of Miss Gao's three roles and replace Miss Gao's three roles with Miss Ba Jie. Its static structure is shown in.
Wukong replaces "Miss Gao's three masters" to meet with pig. Obviously, this is the application of proxy mode. Specifically, this is an application that protects the proxy mode. Client requests are sent to the real theme object only when the proxy object is considered appropriate.
Bajie cannot tell true and false wife
From the description of Journey to the West, we can see that pig cannot identify the "Miss Gao jia" and "Miss Gao jia" played by Wukong ". The client cannot identify the proxy topic object and the real topic object. This is a proxy mode.
Important purpose.
Figure 5 shows the object map of Miss gokuai in place of Miss gokuai.
Example Image Design:
For example, when you are in college, the school will hold a New Year's Day Gala every year, but the school is the organizer and the student union is the organizer. This mode is our agency mode. The example is shown below:
Code Design:
Create the IActivity. cs interface first:
01 |
public interface IActivity |
06 |
/// <returns></returns> |
11 |
/// <returns></returns> |