15.3 proxy mode application instance
Next we will use an application instance to further learn and understand the proxy mode.
1. instance description
A software company undertakes the development task of a paid Business Information Inquiry System of an Information consulting company. The basic requirements of this system are as follows: (1) Before querying business information, the user must pass identity authentication. Only valid users can use this query system; (2) When querying business information, the system must record the query log to charge the query fee based on the number of queries. The developer of the software company has completed the development task of the business information query module and hopes to add the authentication and logging functions to the original system in a loosely coupled manner, the client code can treat the original business information query module and the business information query module after the new function is added, and some new functions may be added to the information query module in the future. Design and implement the paid Business Information Query System in proxy mode. |
2. instance analysis and class diagram
Through analysis, an indirect access method can be used to design the Business Information Query System. A proxy object is added between the client object and the information query object, let the proxy object implement identity authentication and logging functions, without directly modifying the original business information query object, as shown in Figure 15-3:
Figure 15-3 Business Information Query System Design Plan
In Figure 15-3, a client object indirectly accesses a real object with the business information query function through a proxy object, in addition to calling the business information query function of a real object in the proxy object, authentication and logging are also added. Design the Business Information Query System in proxy mode, as shown in Figure 15-4.
Figure 15-4 Structure of the Business Information Query System
In Figure 15-4, the business type accessvalidator is used to verify the user identity. The business type logger is used to record the user query logs. The searcher acts as the abstract topic role, the realsearcher acts as the real topic role, and the proxysearcher acts as.
3. instance code
(1) accessvalidator: authentication class, business class. It provides the method validate () for identity authentication.
// Accessvalidator. csusing system; namespace proxysample {class accessvalidator {// simulate logon verification public bool validate (string userid) {console. writeline ("verify user'" + userid + "'in the database is a legal user? "); If (userid. Equals (" Yang Guo ") {console. writeline (" '{0}', logon successful! ", Userid); Return true;} else {console. writeline (" '{0}' login failed! ", Userid); Return false ;}}}}
(2) Logger: log record class and business class. It provides the log () method to save logs.
// Logger. csusing system; namespace proxysample {class logger {// simulate the public void log (string userid) {console. writeline ("update database, user '{0}' queries plus 1! ", Userid );}}}
(3) searcher: an abstract Query Class that acts as an abstract topic. It declares the dosearch () method.
//Searcher.csnamespace ProxySample{ interface Searcher { string DoSearch(string userId, string keyword); }}
(4) realsearcher: a specific query class that acts as a real topic. It implements the query function and provides the method dosearch () to query information.
// Realsearcher. csusing system; namespace proxysample {class realsearcher: searcher {// simulate the query of Business Information Public String dosearch (string userid, string keyword) {console. writeline ("User '{0}' uses the keyword '{1}' to query business information! ", Userid, keyword); Return" Return specific content ";}}}
(5) proxysearcher: A proxy Query Class that acts as a proxy topic. It is a query proxy that maintains reference to realsearcher objects, accessvalidator objects, and logger objects.
// Proxysearcher. csnamespace proxysample {class proxysearcher: searcher {private realsearcher searcher = new realsearcher (); // maintain a reference to a real topic private accessvalidator validator; private logger; public String dosearch (string userid, string keyword) {// If the authentication is successful, run the query if (this. validate (userid) {string result = searcher. dosearch (userid, keyword); // call the query method of the real theme object this. log (userid); // record query log return result; // return query result} else {return NULL ;}// create an Access Validation object and call its validate () public bool validate (string userid) {validator = new accessvalidator (); Return validator. validate (userid);} // create a logging object and call its log () method to implement the public void log (string userid) {logger = new logger (); logger. log (userid );}}}
(6) the configuration file app. config stores the proxy topic class name in the configuration file.
<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="proxy" value="ProxySample.ProxySearcher"/> </appSettings></configuration>
(7) Program: client test class
// Program. csusing system; using system. configuration; using system. reflection; namespace proxysample {class program {static void main (string [] ARGs) {// read the configuration file string proxy = configurationmanager. appsettings ["proxy"]; // reflection generates objects. For abstract programming, the client does not need to distinguish the real topic class from the proxy class searcher; searcher = (Searcher) assembly. load ("proxysample "). createinstance (proxy); string result = searcher. dosearch ("Yang Guo", "Yu nvjing"); console. read ();}}}
4. Results and Analysis
Compile and run the program. The output result is as follows:
In the database, check whether the user 'yang Ta' is a legal user? 'Yang Ta' login successful! The user 'yang Ta' uses the keyword 'yu nvxin jing' to query business information! The database is updated. The number of queries by the user 'yang Ta' is increased by 1! |
This instance isProtection proxyAndIntelligent reference proxyTo control the permissions and reference count of the real topic category in the proxysearcher proxy class. If you need to add a new access control mechanism and new functions when accessing the real topic, you only need to add a new proxy class, modify the configuration file, and use the new proxy class in the client code. The source code does not need to be modified and complies with the open and closed principles.
[Author: Liu Wei (Sunny) http://blog.csdn.net/lovelion]