Proxy Pattern:对其他对象提供一种代理以控制对这个对象的访问,它的主要作用是为其他对象提供一种代理以控制对这个对象的访问。在某些情况下,一个对象不想或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用
1)
Subject.java
package com.flyingh.isubject;
public interface Subject {
void fun1();
void fun2();
void fun3();
}
RealSubject.java
package com.flyingh.subject;
import com.flyingh.isubject.Subject;
public class RealSubject implements Subject {
@Override
public void fun1() {
// TODO Auto-generated method stub
System.out.println("fun1()");
}
@Override
public void fun2() {
// TODO Auto-generated method stub
System.out.println("fun2()");
}
@Override
public void fun3() {
// TODO Auto-generated method stub
System.out.println("fun3()");
}
}
Proxy.java
package com.flyingh.subject;
import com.flyingh.isubject.Subject;
public class Proxy implements Subject {
private Subject realSubject;
public Proxy(Subject realSubject) {
super();
this.realSubject = realSubject;
}
public void before() {
System.out.println("before");
}
public void after() {
System.out.println("after");
}
@Override
public void fun1() {
// TODO Auto-generated method stub
before();
realSubject.fun1();
}
@Override
public void fun2() {
// TODO Auto-generated method stub
before();
realSubject.fun2();
after();
}
@Override
public void fun3() {
// TODO Auto-generated method stub
realSubject.fun3();
after();
}
}
Client.java
package com.flyingh.client;
import com.flyingh.isubject.Subject;
import com.flyingh.subject.Proxy;
import com.flyingh.subject.RealSubject;
public class Client {
public static void main(String[] args) {
Subject proxy = new Proxy(new RealSubject());
proxy.fun1();
proxy.fun2();
proxy.fun3();
}
}
程序运行结果如下:
before
fun1()
before
fun2()
after
fun3()
after
2)Dynamic Proxy:
(1):
Subject.java
package com.flyingh.isubject;
public interface Subject {
void print();
}
RealSubject.java
package com.flyingh.subject;
import com.flyingh.isubject.Subject;
public class RealSubject implements Subject {
@Override
public void print() {
// TODO Auto-generated method stub
System.out.println("hello world!!!");
}
}
DynaProxy.java
package com.flyingh.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class DynaProxy implements InvocationHandler {
private Object obj;
public DynaProxy(Object obj) {
super();
this.obj = obj;
}
public Object newProxyInstance() {
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
before();
Object retObj = method.invoke(obj, args);
after();
return retObj;
}
private void after() {
// TODO Auto-generated method stub
System.out.println("after()");
}
private void before() {
// TODO Auto-generated method stub
System.out.println("before()");
}
}
Client.java
package com.flyingh.client;
import com.flyingh.isubject.Subject;
import com.flyingh.proxy.DynaProxy;
import com.flyingh.subject.RealSubject;
public class Client {
public static void main(String[] args) {
Subject proxy = (Subject) new DynaProxy(new RealSubject())
.newProxyInstance();
proxy.print();
}
}
程序运行结果如下:
before()
hello world!!!
after()
(2):
Subject.java
package com.flyingh.subject;
public class Subject {
public void print() {
System.out.println("hello world!!!");
}
}
CglibProxy.java
package com.flyingh.proxy;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class CglibProxy implements MethodInterceptor {
private Object obj;
public CglibProxy(Object obj) {
super();
this.obj = obj;
}
public Object newProxyInstance() {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(obj.getClass());
enhancer.setCallback(this);
return enhancer.create();
}
@Override
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
// TODO Auto-generated method stub
before();
Object retObj = proxy.invoke(this.obj, args);
after();
return retObj;
}
private void after() {
// TODO Auto-generated method stub
System.out.println("after()");
}
private void before() {
// TODO Auto-generated method stub
System.out.println("before()");
}
}
Client.java
package com.flyingh.client;
import com.flyingh.proxy.CglibProxy;
import com.flyingh.subject.Subject;
public class Client {
public static void main(String[] args) {
Subject proxy = (Subject) new CglibProxy(new Subject())
.newProxyInstance();
proxy.print();
}
}
程序运行结果如下:
before()
hello world!!!
after()