Demo of class reflection mechanism + responsibility chain design mode

Source: Internet
Author: User

Demo of class reflection mechanism + responsibility chain design mode

Directly paste the Code:

Reflection Toolkit
Package com.zzk.cn;


Import java. lang. reflect. Method;
Import java. util. List;


/**
* Object processing class
*
* @ Author zhengke. zzk
*/
Public class ObjectInstance {

/**
* Obtain the object based on the class name
*
* @ Param className
* @ Return
*/
Public Object getObjectByClassName (String className ){
Try {
Class cls = Class. forName (className );
Return cls. newInstance ();
} Catch (Exception e ){
Throw new RuntimeException ("ObjectInstance Get Class error:" + e );
}
}

/**
* Execution Method
*
* @ Param className
* @ Param methodName
* @ Return
*/
Public Object getMethodInvoke (String className, String methodName, Object object ){
If (object = null ){
Throw new RuntimeException ("ObjectInstance Get Method Invoke object is null ");
}
Try {
Method method = Class. forName (className). getMethod (methodName, new Class [] {});
Return method. invoke (object );
} Catch (Exception e ){
Throw new RuntimeException ("ObjectInstance Get Method Invoke error:" + e );
}
}

/**
* Execution Method
*
* @ Param className
* @ Param methodName
* @ Return
*/
Public ListGetListByClassName (String className, String methodName, Object object ){
If (object = null ){
Throw new RuntimeException ("ObjectInstance Get Method Invoke object is null ");
}
Try {
Method method = Class. forName (className). getMethod (methodName, new Class [] {});
Return (List) Method. invoke (object );
} Catch (Exception e ){
Throw new RuntimeException ("ObjectInstance Get Method Invoke error:" + e );
}
}

}




Interface
Package com.zzk.cn;
Public interface Filter {
Public void doFilter (Request request, Response response, FilterChain chain );

}



Request:
Package com.zzk.cn;
Public class Request {
String requestStr;

Public String getRequestStr (){
Return requestStr;
}

Public void setRequestStr (String requestStr ){
This. requestStr = requestStr;
}

}


Response:
Package com.zzk.cn;
Public class Response {
String responseStr;

Public String getResponseStr (){
Return responseStr;
}

Public void setResponseStr (String responseStr ){
This. responseStr = responseStr;
}

}



Responsibility chain
Package com.zzk.cn;
Import java. util. ArrayList;
Import java. util. List;

Public class FilterChain implements Filter {
List Filters = new ArrayList ();
Int index = 0;

Public FilterChain addFilter (Filter f ){
This. filters. add (f );
Return this;
}

@ Override
Public void doFilter (Request request, Response response, FilterChain chain ){
If (index = filters. size ())
Return;
Filter f = filters. get (index );
Index ++;
F. doFilter (request, response, chain );
}
}



Policy 1:
Package com.zzk.cn;
Public class FaceFilter implements Filter {

@ Override
Public void doFilter (Request request, Response response, FilterChain chain ){
Request. requestStr = request. getRequestStr (). replace (":)",
"^ V ^ ------- FaceFilter ");
Chain. doFilter (request, response, chain );
Response. responseStr + = "------- FaceFilter ";

}

}



Policy 2:
Package com.zzk.cn;
/**
* Filter script elements in HTML
* @ Author lcq
*
*/
Public class HTMLFilter implements Filter {

@ Override
Public void doFilter (Request request, Response response, FilterChain chain ){
Request. requestStr = request. getRequestStr (). replace ("<","[")
. Replace (">", "] -------- HTMLFilter ");
Chain. doFilter (request, response, chain );
Response. responseStr + = "-------- HTMLFilter ";

}

}



Policy 3:
Package com.zzk.cn;
Public class SesitiveFilter implements Filter {

@ Override
Public void doFilter (Request request, Response response, FilterChain chain ){
Request. requestStr = request. getRequestStr (). replace ("sensitive ","")
. Replace ("cat", "haha ------ SesitiveFilter ");
Chain. doFilter (request, response, chain );
Response. responseStr + = "------ SesitiveFilter ";

}

}


Test method:
Package com.zzk.cn;

Public class Main2 {



Public static void main (String [] args ){
String message = "sensitive words, Chongqing, <script> Dodge :)";
Request request = new Request ();
Request. setRequestStr (message );
Response response = new Response ();
Response. setResponseStr ("response ");

ObjectInstance objectInstance = new ObjectInstance ();
Object object = objectInstance. getObjectByClassName ("com.zzk.cn. HTMLFilter ");

FilterChain fc = new FilterChain ();
Fc. addFilter (Filter) object );
Fc. addFilter (new SesitiveFilter ());

FilterChain fc2 = new FilterChain ();
Fc2.addFilter (new FaceFilter ());

Fc. addFilter (fc2 );
Fc. doFilter (request, response, fc );
System. out. println ("request =" + request. getRequestStr ());
System. out. println ("response =" + response. getResponseStr ());
}
}

Related Article

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.