Understand and use design mode, we can cultivate our good facial program habits, while in practical applications, you can get water like a fish, enjoy the fun of you.
Proxy is a more useful model, and there are many variants. Application occasions cover the big structure from the small structure to the entire system. Proxy is the meaning of the agent. We may have a proxy server and other concepts. The agent concept can be explained as: at the starting point There is a middle layer between the destination, meaning the agent.
Definition in design mode: Provide a proxy for other objects to control access to this object.
Why use proxy? 1. The license mechanism has different access rights to the same item. For example, in the JIVE Forum system, you will use Proxy to authorize the authorization mechanism control. There are two people in the access forum: registered users and tourists (unregistered User), JIVE controls the access permission of these two users through agents such as ForumProxy.
2. A certain user cannot operate directly to an object, but it must be interacted with that item. Example two specific situations: (1) If the object is a big picture, it takes a long time to display it, then when this picture is included in the document, open this document using the editor or browser, open The document must be very fast, can't wait for the big picture processing, then you need to be a picture proxy to replace the real picture. (2) If the object is on a remote server on the Internet, this object directly operates this object because the network speed may be slower, then we can use proxy to replace the object.
In summary, for objects that overhead, only create when using it, this principle can save us a lot of valuable Java memory. Therefore, some people think that Java consumes resource memory, I think this is also a certain relationship with the program.
How to use proxy? Take the JIVE Forum system as an example, users accessing the forum system have multiple types: registered ordinary user forum managers system managers, registered ordinary users can speak; forum managers can manage his authorized forum; system management All transactions can manage all transactions, these permissions division and management are done using Proxy.
Forum is the core interface of Jive. The main behavior of the forum operation is displayed in the Forum, such as the acquisition and modification described in the Forum Name Forum, the post is deleted editing, etc.
Users who define various level permissions in ForumPermissions:
. Public class ForumPermissions implements Cacheable {/ *** Permission to read object * / public static final int READ = 0;. / *** Permission to administer the entire sytem * / public static final int SYSTEM_ADMIN = 1; / *** . Permission to administer a particular forum * / public static final int FORUM_ADMIN = 2; / *** Permission to administer a particular user * / public static final int USER_ADMIN = 3;. / *** Permission to administer a particular group *. / public static final int GROUP_ADMIN = 4; / *** Permission to moderate threads * / public static final int MODERATE_THREADS = 5;.. / *** Permission to create a new thread * / public static final int CREATE_THREAD = 6; / . *** Permission to create a new message * / public static final int CREATE_MESSAGE = 7; / *** Permission to moderate messages * / public static final int MODERATE_MESSAGES = 8;. ..... public boolean isSystemOrForumAdmin () { Return (Values [forum_admin] || VALUES [system_admin]);} .....} Therefore, various operation permissions in Forum is related to the user level defined by ForumPerMissions, as an interface forum implementation: forump Roxy is linked to this correspondence. For example, modify the name of Forum, only the forum manager or system manager can modify, the code is as follows:
public class ForumProxy implements Forum {private ForumPermissions permissions; private Forum forum; this.authorization = authorization; public ForumProxy (Forum forum, Authorization authorization, ForumPermissions permissions) {this.forum = forum; this.authorization = authorization; this.permissions = permissions ;} ..... public void setName (String name) throws UnauthorizedException, ForumAlreadyExistsException {// only is the system or forum administrator can change the name if (permissions.isSystemOrForumAdmin ()) {forum.setName (name);} else {Throw new unauthorizedException ();}} ...}
And DBFORUM is the real implementation of the interface forum, to modify the name of the forum as an example:
Public Class DBforum Implements Forum, Cacheable {... public void setname (String name) throws forumalreadyexistSexception {.... this.name = name; // This really saves the new name to SaveToDB (); ... Anything involves modifying this incident on the forum name, other programs have to be deal with forumproxy, and the forumProxy decides whether there is permissions to do something, and Forumproxy is a veritable "gateway", "security agent", "security agent" system".
In the usual application, it is inevitable to always involve the system's authorization or security system, whether you have unbearable use proxy, you are actually using Proxy.
We will continue to combine JIVE to talk to a little bit, and below should be involved in the factory mode. If you don't understand the plant mode, please see another article: Design mode Factory
We already know that using the forum needs to create a forum in a forumproxy, Jive is using Factory mode, there is a total abstract category forumfactory, in this abstract category, call forumfactory is implemented through the getInstance () method, here use Singleton (also One of the design patterns, because of the introduction article, I will not write, see here), getInstance () returned to ForumFactoryProxy.
Why not return forumFactory, return ForumFactory's implementation forumFactoryProxy? The reason is obvious, you need to use the proxy to determine whether there is permissions to create Forum.
In ForumFactoryProxy we see the code as follows:
public class ForumFactoryProxy extends ForumFactory {protected ForumFactory factory; protected Authorization authorization; protected ForumPermissions permissions; public ForumFactoryProxy (Authorization authorization, ForumFactory factory, ForumPermissions permissions) {this.factory = factory; this.authorization = authorization; this.permissions = permissions;} public Forum createForum (String name, String description) throws UnauthorizedException, ForumAlreadyExistsException {// only the system administrator can create forum if (permissions.get (ForumPermissions.SYSTEM_ADMIN)) {Forum newForum = factory.createForum (name, description); return New Forum, Authorization, Permissions;} else {throw new unauthorizedException ();}}
Method CreateForum returned to ForumProxy, proxy is like a wall, and other programs can only be interactive with Proxy.
Note that there are two proxy: Forumproxy and ForumFactoryProxy here. Represents two different responsibilities: use forum and create Forum; why do you use objects and creating items to separate, this is why the use of Factory mode: It is for "packaging" "assignment"; in other words, single function Convenient maintenance. Others in the JIVE Forum system, create and use, in accordance with the idea of Forum.
We discussed how to use Proxy to enable access to authorization mechanisms, and Proxy can also hide the user's optimization method called Copy-ON-WRITE. Copying a huge and complex object is a big overhead. If the copy is changed, there is no need to modify the original object, then such copy overhead is not necessary. Delay this copy of the copy with a proxy.
For example: We have a big collection, such as Hashtable, there are many users who simultaneously access it. One of the special clients should be subjected to continuous data acquisition. At this time, other subscribers are required to increase or delete stuff in HashTable.
The most direct solution is to use the Collection's Lock, let this special user get this LOCK, make a continuous data, and then release the LOCK. Public void fofetches (HashTable HT) {synchronized (ht) {// The specific continuous data acquisition action. . }
}
But this method may locking Collection will take a long time. During this time, other users cannot access the Collection.
The second solution is the CLONE COLLECTION, which allows the continuous data to get the Collection operations for Clone. The premise of this program is that this Collection is Clone, and must have a method of providing depth Clone. Hashtable provides a Clone method for yourself, but not a Key and Value objects, about Clone Meanings can refer to special articles. Public void fofetches (havehtable ht) {
Hashttable new = (hashtable) ht.clone ();
}
The problem is coming, since it is an object operation for Clone, if the original mother is modified by other client operation, then the object operation of Clone is meaningless.
Last Solution: We can wait for other client modifications to perform Clone, that is, this special user is in the first part of the information acquisition operation by calling a method called Clone. However, there is actually no real copy of the object until there are other users modified this object Collection.
Use Proxy to implement this solution. This is the Copy-ON-WRITE operation.
The Proxy application range is wide, and now popular distribution calculation methods RMI and CORBA are all applications of Proxy mode.