Proxy (proxy) in design pattern

xiaoxiao2021-04-05  244

Understand and use design model, we can cultivate our good object-oriented program habits, while in practical applications, you can get water like a fish, enjoy more fun.

Proxy is a more useful model, and there are many variants. Application occasions covers 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: on 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 authorization mechanism has different levels of users with different access rights. For example, in the JIVE Forum system, use Proxy to authorize the control mechanism control, access to the forum has two people: registered users and tourists (unregistered User), JIVE controls access to the forum through agents such as ForumProxy.

2. A client cannot operate directly to an object, but it must be interacted with that object. For two specific circumstances: (1) If that object is a big picture, you need to spend a long time Show out, then when this picture is included in the document, use the editor or browser to open this document, open the document very quickly, can't wait for the big picture processing, then you need to be a picture proxy to replace the real picture. ( 2) If that object is on a remote server in the Internet, this object directly does this object because the network speed may be slower, then we can use proxy to replace that object.

In short, the principle of overhead is very large, only when using it, this principle can save us a lot of valuable Java memory. So, some people think that Java consumes resource memory, I think this and programming ideas are also Relationship.

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 Managers 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 imports 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 mode_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 are related to the user-defined user level defined by ForumPerMissions. As the implementation of the interface forum: Forumproxy is linked to this correspondence For example, modify the name of the Forum, only the forum administrator 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 ();}} ...}

DBFORUM is the true 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 truly saves the new name to SavetoDB (); .... } ...} All programs involving the forum name, other programs are first dedicated to the forumProxy, determine whether there is permission to do something, and Forumproxy is a veritable "gateway", "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 class forumfactory, in this abstract class, calling forumfactory is implemented by 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, and return forumFactory's implementation forumFactoryProxy? The reason is obvious, you need to determine if there is permission 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 ForumProxy (newForum, authorization , permissions; else {throw new unauthorizedException ();}}

Method CreateForum returned to ForUmproxy, proxy is like a wall, other programs can only be interact with Proxy.

Note that there are two proxy: forumproxy and finalumfactoryProxy. Representing two different responsibilities: Using Forum and create forum; as for the use of objects and creation objects, this is why the use of Factory mode: Yes for "packaging" "Dispatch"; in other words, as functional as possible, easy to maintain the modification. The creation and use of other messages in the Jive Forum system is in accordance with the idea of ​​Forum.

The above discusses how to use Proxy to authorize the authorization mechanism, Proxy can also hide the user's optimization method called Copy-ON-WRITE. Copy a large and complex object is a big overhead, if copy In the process, there is no change to the original object, then such copy overhead is not necessary. Use the agent to delay this copy process.

For example: we have a big collection, such as HashTable, there are many clients to access it simultaneously. One of the special clients have continuous data acquisition, and other clients can no longer add or increase or increase or increase in HashTable or Delete stuff.

The most direct solution is: Using Collection's Lock, let this special client get this LOCK, make a continuous data acquisition, then release the Lock.Public void fofetches (HashTable HT) {synchronized (ht) {// Specific Continuous data acquisition action.

}

But this method may locking Collection will take a long time. During this time, other clients cannot access the Collection.

The second solution is CLONE COLLECTION, then lets continuous data get the Collection action for Clone. This solution is that this collect is Clone, and must have a method of providing depth clone. Hashtable provided Your own Clone method, but not the Clone of the key and value object, about clone meaning can be referred to a special article. Public void fofetches (HashTable HT) {

Hashttable new = (hashtable) ht.clone ();

}

The problem is coming again, because it is an object operation for Clone, if the original master is modified by other clients, the object operation coming out of Clone is meaningless.

Last Solution: We can wait for other client modifications to make Clone, that is, this special customer is first to perform a series of data acquisition operations by calling a method called Clone. But actually does not have real objects. Copy until some other client modified this object Collection.

Use Proxy to implement this scenario. This is a 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.

More proxy applications, see http://www.research.umbc.edu/~tarr/cs491/lectures/proxy.pdf

Sun's Explore The Dynamic Proxy API Dynamic Proxy Classes

转载请注明原文地址:https://www.9cbs.com/read-132047.html

New Post(0)