首页 > 代码库 > 类反射机制+责任链的设计模式demo
类反射机制+责任链的设计模式demo
直接贴代码:
反射工具包
package com.zzk.cn;
import java.lang.reflect.Method;
import java.util.List;
/**
* Object 处理类
*
* @author zhengke.zzk
*/
public class ObjectInstance {
/**
* 根据class name 获得 object
*
* @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);
}
}
/**
* 执行方法
*
* @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);
}
}
/**
* 执行方法
*
* @param className
* @param methodName
* @return
*/
public List<Object> getListByClassName(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<Object>) method.invoke(object);
} catch (Exception e) {
throw new RuntimeException("ObjectInstance Get Method Invoke error:" + e);
}
}
}
接口
package com.zzk.cn;
public interface Filter {
public void doFilter(Request request, Response response, FilterChain chain);
}
请求:
package com.zzk.cn;
public class Request {
String requestStr;
public String getRequestStr() {
return requestStr;
}
public void setRequestStr(String requestStr) {
this.requestStr = requestStr;
}
}
响应:
package com.zzk.cn;
public class Response {
String responseStr;
public String getResponseStr() {
return responseStr;
}
public void setResponseStr(String responseStr) {
this.responseStr = responseStr;
}
}
责任链
package com.zzk.cn;
import java.util.ArrayList;
import java.util.List;
public class FilterChain implements Filter {
List<Filter> filters = new ArrayList<Filter>();
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);
}
}
策略一:
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";
}
}
策略二:
package com.zzk.cn;
/**
* 过滤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";
}
}
策略三:
package com.zzk.cn;
public class SesitiveFilter implements Filter {
@Override
public void doFilter(Request request, Response response, FilterChain chain) {
request.requestStr = request.getRequestStr().replace("敏感", " ")
.replace("猫猫", "haha------SesitiveFilter");
chain.doFilter(request, response, chain);
response.responseStr += "------SesitiveFilter";
}
}
测试方法:
package com.zzk.cn;
public class Main2 {
public static void main(String[] args) {
String message = "敏感词汇,重庆,<script> 躲猫猫 :)";
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());
}
}
类反射机制+责任链的设计模式demo
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。