Proxy in Schema mode)

Source: Internet
Author: User

Understanding and using design patterns can cultivate good
Object-Oriented Programming habits, at the same time in practical applications, you can enjoy the fun.

Proxy is a more useful mode, and there are many variants, should
Proxy is the meaning of proxy. We may have the concepts of proxy servers. The concept of proxy can be interpreted as: there is an intermediate layer between the starting point and the destination,
Proxy.

Definition in Design Pattern
: Provides a proxy for other objects to control access to this object.

Why use proxy?




1. Authorization Mechanism
Users of different levels have different access rights to the same object. For example, in the jive forum system, the proxy is used for authorization control. There are two types of users who access the Forum: registered users and tourists (not registered for use
In jive, you can use a proxy like forumproxy to control the access permissions of these two users to the Forum.

2. A client cannot directly operate on an object, but must
Objects have interactions.
Two specific cases are given:
(1) if the object is a large image that takes a long time to be displayed, when the image is included in the document
When you use the editor or browser to open this document, it must be very fast to open the document and cannot wait until the processing of the large image is completed. In this case, you need to create an image proxy to replace the real image.

(2)
If the object is on a remote server on the Internet and it may be slow due to network speed, we can use proxy to replace the object.

In short, the principle is that for objects with high overhead
It is created only when it is used. This principle can save us a lot of valuable Java memory. therefore, some people think that Java consumes resources and memory. I think this is also related to programming ideas.

How to use proxy?




To
Jive forum system

For example, there are multiple types of users accessing the forum system: registering Common users, forum administrators, and system administrators.
Visitors, registered ordinary users can speak; Forum managers can manage their authorized forums; system managers can manage all transactions, and these permissions are divided and managed using proxy.

Forum is the core interface of jive and is displayed in forum.
Main activities related to Forum operations, such as obtaining and modifying Forum descriptions of Forum names, posting, deletion, and editing of posts.

In forumpermissions, Set
Users with various levels of permissions:

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 particle forum.
*/
Public static
Final int forum_admin = 2;

/**
*
Permission to administer a participant user.
*/
Public static final
Int user_admin = 3;

/**
*
Permission to administer a participant 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, the various operation permissions in Forum are
Forumpermissions is related to the user level defined by forumpermissions. As the implementation of the Forum interface, forumproxy associates the corresponding relationship. For example, modify
The Forum name can be modified only by forum administrators or system administrators. 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 the system or forum administrator can modify the name.
If (permissions. issystemorforumadmin ()){
 
Forum. setname (name );
}
Else {
Throw new
Unauthorizedexception ();
}
}

...

}

While dbforum is the true forum Interface
The following example shows how to modify the Forum Name:

Public class
Dbforum implements Forum, cacheable {
...

Public void
Setname (string name) throws forumalreadyexistsexception {

....

 
This. Name = Name;
// Save the new name to the database
Savetodb ();

  
....
}


...

}

Any event that involves modifying the Forum name
The program must first deal with forumproxy. forumproxy determines whether it has the permission to do the same thing. forumproxy is a real "Gateway" and "Security generation ".
Management System ".

In normal applications, it is inevitable that systems are always involved.
Authorization or security system, no matter whether you use proxy unconsciously, you are actually using proxy.

Let's continue to talk more deeply with jive.
The factory model is involved. If you do not know the factory model, please read another article: Design Patterns
Factory


We already know that using forum requires
Forumproxy and jive creates a forum in factory mode. There is a general abstract class forumfactory. In this abstract class
Forumfactory is implemented through the getinstance () method. Singleton is used here (it is also one of the design patterns. As there are many articles introduced, I will not write it.
Here
), Getinstance () returns forumfactoryproxy.

Why not return
Forumfactory, and return the forumfactory implementation forumfactoryproxy?
Original
Because it is obvious, you need to use a proxy to determine whether you have the permission to create a forum.

In forumfactoryproxy
The Code is 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 a forum
If
(Permissions. Get (forumpermissions. system_admin )){
Forum
Newforum = factory. createforum (name, description );
Return new
Forumproxy (newforum, authorization, permissions );
}
Else {
 
Throw new unauthorizedexception ();
}
}

The createforum method also returns
Forumproxy, proxy is like a wall. Other programs can only interact with the proxy.

Note that there are two
Proxy: forumproxy and forumfactoryproxy.
It represents two different responsibilities: Using forum and creating forum;
The reason for separating the object used from the created object is that the factory mode is used: Yes
In order to "encapsulate" "dispatch", in other words, the functions should be unified as much as possible to facilitate maintenance and modification.

Other posts, such as post creation and use, in the jive forum system are based on
Forum comes from this idea.

We discussed how to use proxy to grant
Proxy can also hide another optimization method called copy-on-write for users. copying a large and complex object is a costly operation.
In the process, the original object is not modified, so this copy overhead is unnecessary. Use a proxy to delay this copy process.

For example, we have a very large
Collection, such as hashtable, many clients access it concurrently. One of the special clients needs to obtain continuous data, which requires that other clients cannot
Add or delete stuff to or from hashtable.

The most direct solution is
: Use the Lock of collection to allow this special client to obtain the lock, perform continuous data acquisition, and then release the lock.
Lock.
Public void fofetches (hashtable HT ){
Synchronized (HT ){
 
// Specific actions for obtaining continuous data ..
}

}

However, this method may lock the collection.
It will take a long time. During this time, other clients will not be able to access the collection.

The second solution is
Clone the collection, and then let the continuous data obtain the cloned collection operation.
The premise of this solution is that the collection can be cloned, and the deep clone method must be provided. hashtable provides its own clone method,
But it is not the clone of the key and value objects. For details about the clone definition, refer
Special Articles

.
Public
Void fofetches (hashtable HT ){

Hashttable
Newht = (hashtable) Ht. Clone ();

}

The problem arises again, because it is for clone
Object operations. If the original parent object is modified by other client operations, the cloned object operations are meaningless.

Final Solution: we can wait for other clients to modify
After cloning, this special client first calls a method called clone to perform a series of data acquisition operations. However, it does not actually copy objects
Another client has modified this object collection.

Use proxy to implement this scheme. This is
Copy-on-write operation.

Proxy has a wide range of applications and is now a popular distributed computing method.
RMI and CORBA are both proxy-mode applications.

For more proxy applications, see http://www.research.umbc.edu /~ Tarr/cs491/lectures/proxyures


Sun
Using e the dynamic proxy API


Dynamic proxy classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.