Design Mode
---- Proxy (
Proxy
)
Mode
Gof
:Provides a proxy for other objects to control access to this object. Unlike flyweight, proxy itself means proxy. The reason for access control on an object is that it is used only when we really need this object or some functions of this object. In a forum system (such as the famous "Tianya community"), there are two basic users who browse the Forum. One is a registered user who can post an article, modify your own articles, delete your own articles, and reply to others' articles. The other is for tourists (non-registered users): they only have the permission to read articles. In such a system, you can use the proxy mode to control the permissions of these two users. According to the content I have seen in various references, there are many variants of the proxy mode, which applies to many places. In the gof book, the following scenarios of using the proxy mode are described: 1. remote proxy: provides a local representation of an object in different address spaces. 2. Virtual Proxy: Creates objects with high overhead as needed. For example, when you look at images on a website, you usually first display small images. When you want to see large images, you can click small images. 3. Protection Proxy: controls access to the original object. Protection proxy is used when the object should have different access permissions. For example, the Forum system we mentioned above. 4. The smart reference replaces simple pointers and performs some additional operations when accessing objects. His typical purposes include: to count the reference to the actual object, so that when the object is not referenced, it can be automatically released (also known as smart pointers. This seems to have a special explanation in C ++). When a persistent object is referenced for the first time, load it into the memory. before accessing an actual object, check whether it has been locked, to ensure that other objects cannot be changed. A simple example is provided to illustrate the proxy mode. In the entertainment industry, stars want to perform performances. To advertise these things, they do not talk to the merchants themselves, but have their own economic man who acts as agents for all foreign affairs of the stars. For example, signing a contract with a certain movie company or signing an advertisement with a certain manufacturer. This economic man is the role of an agent. When he negotiates with a merchant and thinks it is feasible, let the stars perform performances or take advertisements, if the negotiation fails, he will cancel the contact activity between the merchant and the star. To some extent, he will control the activity of the star. The following is a simple code:
Package proxy;
Public abstract class musicperson
...{
Public abstract void age ();
Public abstract void requital (); // remuneration
Public abstract void date ();
} // End class musicperson
Star appearance:
Package proxy;
Public class musicstar extends musicperson
...{
Public void date ()
...{
System. Out. println ("the music person's date ");
} // End f ()
Public void requital ()
...{
System. Out. println ("the music person's retu.pdf ");
} // End g ()
Public void age ()
...{
System. Out. println ("The musicperson's age ");
} // End H ()
} // End class musicstar
AGENT:
Package proxy;
Public class musicstarproxy extends musicperson
...{
Private musicperson implementation;
Public musicstarproxy ()
...{
Implementation = new musicstar ();
}
Public void date ()
...{
Implementation. Date ();
} // End date ()
Public void requital ()
...{
Implementation. Requiter ();
} // End requesters ()
Public void age ()
...{
// Implementation. Age ();
System. Out. println ("sorry I can't tell you the musicstar's age! ");
} // End age ()
} // End class musicstarproxy
Use of the porxy mode:
Package proxy;
Public class proxypattern
...{
Private musicperson P = new musicstarproxy ();
Public void showproxypattern ()
...{
P. Date ();
P. requesters ();
P. Age ();
} // End showproxypattern ()
Public static void main (string [] ARGs)
...{
System. Out. println ("----------------------------");
System. Out. println ("the proxy pattern ");
Proxypattern pp = new proxypattern ();
Pp. showproxypattern ();
System. Out. println ("----------------------------");
} // End main (...)
} // End class proxypattern
The following is a UML diagram of the proxy mode:
The porxy mode gives me the feeling that it is relatively simple. The simple reason may be that I have not understood it well, and I have not looked at other related variants. I will wait for a chance and have time to study it again.
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 987253