Agent mode (proxy):
The proxy design pattern is the structure pattern of an object, and the principle is to provide a proxy object to an object, and the proxy object to control the reference to the Wish object. In some cases, a party cannot or does not want to refer directly to an object, and the proxy object can act as an intermediary between the two parties, which can greatly reduce the coupling relationship and improve each other's flexibility. The following is an abbreviated structure class diagram for the proxy mode:
There are three roles for the proxy mode: Abstract roles, proxy roles, and specific roles. Specific as follows:
Abstract role: A common interface that declares the role of a proxy and a specific role, and enables the use of proxy roles where specific real roles can be used.
Specific roles: Define the specific roles to be represented by the proxy role.
Proxy role: Contains a reference to a specific role, so that you can manipulate the specific role object at any time, and the agent role provides the same interface as the specific role, making it easy to override the specific role object at any time.
Let's give a practical example to illustrate the use of proxies. In the project, we want to implement a search function, before searching the data, we need to verify whether the user is a login or a member, if not a member, then prompt and return to the non-system query, in addition, we also need to record the number of search records members, so that can help the operation of the post-promotion statistics Oh! Of course, this is just a simulation implementation, the validation failed to prompt permission is not enough, the number of records displayed as the number of searches (each search record can), and the final search results, directly displayed as the search keywords (normal is not such oh).
The following is a detailed class diagram structure relationship for my search feature:
Well, the preparation of the work has been good, next see the specific code implementation, as follows:
Prosearcher:
Public interface Prosearcher {
/**
* to declare an abstract method
*/
Stringdosearch (String userName, stringpassword, string key);
}
Prorealsearcher:
Public class Prorealsearcher implements Prosearcher {
Public Prorealsearcher () {
Super ();
}
/**
* The Real query operation is done here
*/
@Override
Public String Dosearch (StringuserName, string password, stringkey) {
// Parametric Encapsulation
StringBufferSB = new stringbuffer ();
SB. Append (userName);
SB. Append (password);
SB. Append (key);
StringRequest = sb. toString ();
// Network Requests
Asynctask (request);
return key;
}
private void asynctask (Stringparams) {
// Network request Core Code
// TODO
}
}
Proaccessvalidator:
Public class Proaccessvalidator {
/**
* Verify user permissions before querying
*/
Public booleanvalidateuser (string userName, stringpassword) {
if ("david001". Equals (userName) &&
"123456". Equals (password)) {
return true;
}
return false;
}
}
Prosearchrecorder:
Public class Prosearchrecorder {
Private StringuserName;
/**
* returns the number of user searches and the delivery log to the server
*/
Public int Save (int records) {
// Save this search record and Delivery Server
Stringrecordremote = userName + " number of times:" +records;
return Save (recordremote,Records);
}
private int Save (String record,intrecords) {
Records + +;
// passing records to the server
// TODO
return Records;
}
Public voidsetusername (String name) {
userName = name;
}
}
Prosearcherproxy:
Public class Prosearcherproxyimplements Prosearcher {
Private ProrealsearcherSearcher = null;
Private Proaccessvalidatoraccessvalidate = null;
Private ProsearchrecorderSearchrecorder = null;
Public Prosearcherproxy () {
Searcher = new prorealsearcher ();
}
@Override
Public String Dosearch (StringuserName, string password, stringkey) {
if (ValidateUser (userName,password)) {
StringResult = searcher. Dosearch (userName, password, key);
Loguserrecord (userName);
return result;
}
return null;
}
/**
* Verifying user Permissions
*/
Private booleanvalidateuser (string userName, stringpassword) {
accessvalidate = new proaccessvalidator ();
return accessvalidate. ValidateUser (userName, password);
}
/**
* query log operation record
*/
private voidLoguserrecord (String userName) {
Searchrecorder = new prosearchrecorder ();
searchrecorder. Setusername (userName);
}
}
Above is the core code of the proxy mode, the specific front-end code is not listed here, and later I will pass the project code up. As follows:
Technology Exchange Group:179914858
Proxy mode is used in Android