代理模式定义:为其他对象提供一种代理以控制对这个对象的访问。
优点:
1、职责清晰。
2、高扩展性。
3、智能化。
下面分析三种形式的代理。
第一种、简单的代理模式
类图如下:
实现代码如下:
玩家抽象类:
package com.designpatterns.proxy; public interface IGamePlayer { public void login(String name,String password); public void killBoss(); public void upgrade(); }玩家实现类:
package com.designpatterns.proxy; public class GamePlayer implements IGamePlayer { private String name=""; public GamePlayer(String name) { this.name=name; } @Override public void login(String name, String password) { System.out.println("用户名为:"+name+"的用户,"+this.name+"登录了!"); } @Override public void killBoss() { System.out.println(this.name+"正在打怪!"); } @Override public void upgrade() { System.out.println(this.name+"升一级!"); } }
package com.designpatterns.proxy; public class GamePlayerProxy implements IGamePlayer { private IGamePlayer gamePlayer=null; public GamePlayerProxy(IGamePlayer gamePlayer) { this.gamePlayer=gamePlayer; } @Override public void login(String name, String password) { this.gamePlayer.login(name, password); } @Override public void killBoss() { this.gamePlayer.killBoss(); } @Override public void upgrade() { this.gamePlayer.upgrade(); } }
package com.designpatterns.proxy; public class Client { public static void main(String[] args) { IGamePlayer zs = new GamePlayer("张三"); IGamePlayer proxy = new GamePlayerProxy(zs); proxy.login("zhangsan", "123456"); proxy.killBoss(); proxy.upgrade(); } }第二种: 强制代理
这种情况下,必须通过制定的代理,才能访问到处理事件。
类图如下:
实现代码如下:
玩家抽象类;
package com.designpatterns.proxyforced; public interface IGamePlayer { public void login(String name,String password); public void killBoss(); public void upgrade(); public IGamePlayer getProxy(); }
package com.designpatterns.proxyforced; public class GamePlayer implements IGamePlayer { private String name=""; private IGamePlayer proxy; public GamePlayer(String name) { this.name=name; } @Override public void login(String name, String password) { if (this.isProxy()) { System.out.println("用户名为:"+name+"的用户,"+this.name+"登录了!"); }else { System.out.println("请使用制定的代理!"); } } @Override public void killBoss() { if (this.isProxy()) { System.out.println(this.name+"正在打怪!"); }else { System.out.println("请使用制定的代理!"); } } @Override public void upgrade() { if (this.isProxy()) { System.out.println(this.name+"升一级!"); }else { System.out.println("请使用制定的代理!"); } } @Override public IGamePlayer getProxy() { if(this.proxy==null){ this.proxy=new GamePlayerProxy(this); } return this.proxy; } private boolean isProxy(){ if(this.proxy==null){ return false; }else { return true; } } }
package com.designpatterns.proxyforced; public class GamePlayerProxy implements IGamePlayer { private IGamePlayer gamePlayer=null; public GamePlayerProxy(IGamePlayer gamePlayer) { this.gamePlayer=gamePlayer; } @Override public void login(String name, String password) { this.gamePlayer.login(name, password); } @Override public void killBoss() { this.gamePlayer.killBoss(); } @Override public void upgrade() { this.gamePlayer.upgrade(); } @Override public IGamePlayer getProxy() { return this; } }
package com.designpatterns.proxyforced; public class Client { public static void main(String[] args) { IGamePlayer zs = new GamePlayer("张三"); System.out.println("-----------------------------\n错误的用法"); IGamePlayer proxy = new GamePlayerProxy(zs); proxy.login("zhangsan", "123456"); proxy.killBoss(); proxy.upgrade(); System.out.println("-----------------------------\n正确的用法"); proxy = zs.getProxy(); proxy.login("zhangsan", "123456"); proxy.killBoss(); proxy.upgrade(); } }
动态代理是在实现阶段不用关心代理谁,而在运行阶段才指定代理哪一个对象。(AOP就是典型的例子)
类图如下:
代码实现如下:
抽象的代理对象:
package com.designpatterns.dynamicproxy; public interface Subject { public void sayHello(); public void sayByeBye(); }
package com.designpatterns.dynamicproxy; public class RealSubject implements Subject { @Override public void sayHello() { System.out.println("hello!"); } @Override public void sayByeBye() { System.out.println("ByeBye!"); } }
package com.designpatterns.dynamicproxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class MyInvocationHandler implements InvocationHandler { private Object target = null; public MyInvocationHandler(Object object) { this.target = object; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { (new Before()).sayBefore();//AOP Object o = method.invoke(this.target, args); (new After()).sayAfter();//AOP return o; } }
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在这里打断一下,为了体现AOP的编程思想,我们可以在这里实现一个简单的切面编程,我定义了两个方法。然后在动态代理的Handler类中调用。
After类:
package com.designpatterns.dynamicproxy; public class After { public void sayAfter() { System.out.println("I am after!"); } }
package com.designpatterns.dynamicproxy; public class Before { public void sayBefore() { System.out.println("I am before!"); } }----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
动态代理类:
package com.designpatterns.dynamicproxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; public class SubjectDynamicProxy<T> { @SuppressWarnings("unchecked") public static <T> T newProxyInstance(Object O) { ClassLoader loader = O.getClass().getClassLoader(); Class<?>[] interfaces = O.getClass().getInterfaces(); InvocationHandler h = new MyInvocationHandler(O); return (T) Proxy.newProxyInstance(loader, interfaces, h); } }
package com.designpatterns.dynamicproxy; public class Client { public static void main(String[] args) { RealSubject realSubject= new RealSubject(); Subject subjectProxy= SubjectDynamicProxy.newProxyInstance(realSubject); subjectProxy.sayHello(); subjectProxy.sayByeBye(); } }
以上就是三种形式的代理模式。
参考资料:
设计模式之禅
备注:
转载请注明出处
http://blog.csdn.net/wsyw126/article/details/51313972
by WSYW126