8. 代理模式(Proxy)
- 抽象主题角色(abstract role): 声明真实主题和代理主题的共同接口。
- 代理主题角色(Proxy role ): 包含真实主题的引用;创建真实引用;在调用真实主题之前和之后,执行某个真实对象没有侧操作。
- 真实主题角色(true role): 被代理的对象。
代理模式例子
public interface SaleComputer {
//抽象主题角色,一个接口
public void sale(String type);
}
/**
* 真实主题角色
*/
public class ComputerMaker implements SaleComputer {
public void sale(String type) {
System.out.println("卖出了一台"+type+"电脑");
}
}
import java.lang.reflect.Proxy;
public class ComputerProxy {
public static SaleComputer getComputerMaker(){
ProxyFunction pf=new ProxyFunction();
return (SaleComputer)Proxy.newProxyInstance(ComputerMaker.class.getClassLoader(),
ComputerMaker.class.getInterfaces(), pf);
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class ProxyFunction implements InvocationHandler {
private ComputerMaker cm;
public void youHui(){
System.out.println("我给你一些优惠。。。");
}
public void giveMouse(){
System.out.println("我还要送你一个鼠标。。。 ");
}
public Object invoke(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
String type=(String)arg2[0];
if(type.equals("联想")||type.equals("三星")){
if(cm==null){
cm=new ComputerMaker();
youHui();
giveMouse();
arg1.invoke(cm, type);
}
}else{
System.out.println("我没有你要的这个牌子的电脑。。。。");
}
return null;
}
}
public class Test {
public static void main(String[] args) {
SaleComputer sc=ComputerProxy.getComputerMaker();
sc.sale("联想");
sc.sale("三星");
sc.sale("Dell");
}
}
java2.0动态代理
- 定义和被代理对象一样的接口
- 调用处理器对象(Invocation Handler)
- 生成代理对象
- 调用处理器invoke()方法,一方面调用真实对象,另一方面执行各种需要的操作
jdk动态代理例子, 来自bjsxt
public interface UserManager {
public void addUser(String username,String password);
public void deleteUser(int id);
public void modifyUser(int id,String username,String password);
public String findUserById(int id);
}
public class UserManagerImpl implements UserManager {
public void addUser(String username, String password) {
System.out.println("---------addUser----------");
}
public void deleteUser(int id) {
System.out.println("---------deleteUser----------");
}
public String findUserById(int id) {
System.out.println("---------findUserById----------");
return null;
}
public void modifyUser(int id, String username, String password) {
System.out.println("---------modifyUser----------");
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class SecurityHandler implements InvocationHandler{
private Object targetObject;
public Object newProxy(Object targetObject){
this.targetObject = targetObject;
return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),
targetObject.getClass().getInterfaces(),
this);
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
chechSecurity();
Object result = null;
try{
result = method.invoke(this.targetObject, args);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
return result;
}
private void chechSecurity(){
System.out.println("---------chechSecurity----------");
}
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Client {
public static void main(String[] args){
SecurityHandler handler = new SecurityHandle();
UserManager um = (UserManager) handler.newProxy(new UserManagerImpl());
um.addUser("ads", "132");
}
}