1.反射
package Test2019616;
public class ReflectImpl {
public void sayHello(String name) {
System.out.print("Hello" + name);
}public ReflectImpl getInstance() {
ReflectImpl Object = null;
try {
Object = (ReflectImpl) Class.forName("Test2019616.ReflectImpl")
.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return Object;
}
public static void main(String args[]){
ReflectImpl r = new ReflectImpl();
System.out.print(r.getInstance());
System.out.print(r);
}
}
结果:创建了两个不同的对象
Test2019616.ReflectImpl@6b97fdTest2019616.ReflectImpl@1c78e57
带参数
package Test2019616;
public class ReflectImpl2 {
private String name;
public ReflectImpl2(String name){
this.name = name;
}
public void sayHello(String name) {
System.out.print("Hello" + name);
}
public ReflectImpl2 getInstance() {
ReflectImpl2 Object = null;
try {
Object = (ReflectImpl2) Class.forName("Test2019616.ReflectImpl2").getConstructor(String.class)
.newInstance("张三");
} catch (Exception e) {
e.printStackTrace();
}
return Object;
}
public static void main(String args[]){
ReflectImpl2 r = new ReflectImpl2("李四");
System.out.println(r.getInstance().name);
System.out.println(r.name);
}
}
调用方法:
package Test2019616;
import java.lang.reflect.Method;
public class ReflectImpl2 {
private String name;
public ReflectImpl2(String name){
this.name = name;
}
public ReflectImpl2(){
}
public void sayHello(String name) {
System.out.print("Hello" + name);
}
public ReflectImpl2 getInstance() {
ReflectImpl2 Object = null;
try {
Object = (ReflectImpl2) Class.forName("Test2019616.ReflectImpl2").getConstructor(String.class)
.newInstance("张三");
} catch (Exception e) {
e.printStackTrace();
}
return Object;
}
public Object getReflect(){
ReflectImpl2 Object = null;
try {
Object = (ReflectImpl2) Class.forName("Test2019616.ReflectImpl2").newInstance();
Method method = Object.getClass().getMethod("sayHello", String.class);
method.invoke(Object, "wang wu");
} catch (Exception e) {
e.printStackTrace();
}
return Object;
}
public static void main(String args[]){
ReflectImpl2 r = new ReflectImpl2();
//System.out.println(r.getInstance().name);
//System.out.println(r.name);
System.out.println(r.getReflect());
}
}
2 动态代理
- JDK动态代理
package Test2019616;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;public class JDKProxyExample implements InvocationHandler {
//真是对象
private Object target = null;//绑定
public Object bind(Object target) {
this.target = target;
return Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(), this);
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("进入代理逻辑方法");
System.out.println("在调度真实对象之前的服务");
Object obj = method.invoke(target, args);
System.out.println("在调度真实对象之后的服务");
return obj;
}
public static void main(String args[]){
JDKProxyExample jdk = new JDKProxyExample();
HelloWorld proxy = (HelloWorld) jdk.bind(new HelloWorldImpl());
proxy.sayHello();
}
}
结果:
进入代理逻辑方法
在调度真实对象之前的服务
hello moring 在调度真实对象之后的服务
设计者往往会用拦截器去代替动态代理
3拦截器---责任链模式
当一个对象在 条链上被多个拦截器拦截处理(拦截器也可以选择不拦截处理它)时,
我们把这样的设计模式称为责任链模式它用于 个对象在多个角色中传递的场景。还是
刚才的例子,申请单走到项目经理那, 经理可能把申请时间“ 周”改为" 天”,从而
响了后面的审批,后面的审批都要根据前面的结果进行。这个时候可以考虑用层层代理来
实现,就是当申请单(target )走到项目经理处,使用第 个动态代理 proxyl 。当它走到部
门经理处,部门经理会得到 个在项目经理的代理 proxyl 基础上生成的 proxy2 来处理
经理的逻辑。
HelloWorldImpl
package Test2019616;
public class HelloWorldImpl implements HelloWorld{
public void sayHello() {
System.out.print("hello moring ");
}}
Intercepter
package Test2019616;
import java.lang.reflect.Method;
public interface Intercepter {
public boolean before(Object Proxy,Object target,Method method,Object[] args);
public boolean around(Object Proxy,Object target,Method method,Object[] args);
public boolean after(Object Proxy,Object target,Method method,Object[] args);}
Intercepter1
package Test2019616;
import java.lang.reflect.Method;
public class Intercepter1 implements Intercepter{
public boolean after(Object Proxy, Object target, Method method,
Object[] args) {
System.out.println("拦截器1的after方法");
return true;
}public boolean around(Object Proxy, Object target, Method method,
Object[] args) {
//System.out.println("拦截器1的before方法");
return true;
}public boolean before(Object Proxy, Object target, Method method,
Object[] args) {
System.out.println("拦截器1的before方法");
return true;
}}
Intercepter2
package Test2019616;
import java.lang.reflect.Method;
public class Intercepter2 implements Intercepter{
public boolean after(Object Proxy, Object target, Method method,
Object[] args) {
System.out.println("拦截器2的after方法");
return true;
}public boolean around(Object Proxy, Object target, Method method,
Object[] args) {
//System.out.println("拦截器1的before方法");
return true;
}public boolean before(Object Proxy, Object target, Method method,
Object[] args) {
System.out.println("拦截器2的before方法");
return true;
}public static void main(String[] args) {
}}
InterceptorJDKProxy
package Test2019616;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;public class InterceptorJDKProxy implements InvocationHandler{
private Object target ;
private String interceptorClass = null;///拦截器全限定名
public InterceptorJDKProxy(Object target,String interceptorClass){
this.target = target;
this.interceptorClass = interceptorClass;
}
//绑定蚕托对象并返回一个 代理占位
public static Object bind (Object target,String interceptorClass){
return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass()
.getInterfaces(), new InterceptorJDKProxy(target,interceptorClass) );
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if(null == interceptorClass){
///没有设置拦截器则直接反射原有方法
return method.invoke(target,args);
}
Object result = null;
///通过反射生成拦截器
Intercepter interceptor = (Intercepter)Class.forName(interceptorClass).newInstance();
///调用前置方法
if(interceptor.before(proxy, target, method, args)){
///反射原有对象方法
result = method.invoke(target,args);
} else{
//返回 false 执行 around 方法
interceptor.around(proxy,target,method,args);
}
///调用后置方法
interceptor.after(proxy, target, method, args);
return result;
}}
TestInterceptor
package Test2019616;
public class TestInterceptor {
/* public static void main(String[] args) {
HelloWorld proxy = (HelloWorld)InterceptorJDKProxy.bind(new HelloWorldImpl()
,"Test2019616.Intercepter1");
proxy.sayHello();
}*/
public static void main(String[] args) {
HelloWorld proxy1 = (HelloWorld)InterceptorJDKProxy.bind(new HelloWorldImpl()
,"Test2019616.Intercepter1");
HelloWorld proxy2 = (HelloWorld)InterceptorJDKProxy.bind(proxy1
,"Test2019616.Intercepter2");
proxy2.sayHello();
}
}
结果
拦截器2的before方法
拦截器1的before方法
hello moring 拦截器1的after方法
拦截器2的after方法