代理模式也叫委托模式,给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用。是一项基本的设计技巧。分为普通代理和强制代理。 另一个角度又分为静态代理和动态代理。
[img]http://dl.iteye.com/upload/attachment/552285/f1e3634c-0064-3d36-a8f5-08a1cfc911fa.png[/img]
[b]Subject接口[/b]
[b]RealSubject类[/b]
[b]ProxySubject类[/b]
[b]TestProxy 测试类:[/b]
[b]测试结果:[/b]
[quote]------------Without Proxy------------
实际操作,参数:liuwentao
------------With Proxy------------
代理操作,参数:liuwentao
实际操作,参数:liuwentao[/quote]
[b]游戏代练帮忙打怪[/b]
[img]http://dl.iteye.com/upload/attachment/552319/9ea288f9-7ece-3b25-84d1-07cb1958c64d.png[/img]
[b]1:普通代理:由代理生成真实角色,真是角色不能直接new出来[/b]
[img]http://dl.iteye.com/upload/attachment/552350/eb0b591e-6f3c-3c7f-bc72-69d2475060f6.png[/img]
约定真实角色不能 直接 new ,必须通过代理类才能操作。
真实角色,构造函数中 有 代理类参数,做判断
代理类构造函数中 new 真实角色
测试类:
[b]2:强制代理:由真是角色指定代理,不是指定的代理不能调方法[/b]
[img]http://dl.iteye.com/upload/attachment/552344/59d85147-6639-3dba-8f3b-67712d1c5609.png[/img]
测试:
[img]http://dl.iteye.com/upload/attachment/552285/f1e3634c-0064-3d36-a8f5-08a1cfc911fa.png[/img]
[b]Subject接口[/b]
public interface Subject {
void operation(String arg);
}
[b]RealSubject类[/b]
public class RealSubject implements Subject {
@Override
public void operation(String arg) {
System.out.println("实际操作, 参数:" + arg);
}
}
[b]ProxySubject类[/b]
public class ProxySubject implements Subject {
private Subject beProxy;
public ProxySubject(Subject beProxy) {
this.beProxy = beProxy;
}
@Override
public void operation(String arg) {
System.out.println("代理操作, 参数:" + arg);
beProxy.operation(arg);
}
}
[b]TestProxy 测试类:[/b]
public class TestProxy {
public static void main(String[] args) {
RealSubject realSubject = new RealSubject();
ProxySubject proxySubject = new ProxySubject(realSubject);
System.out.println("------------Without Proxy ------------");
doSomething(realSubject);
System.out.println("------------With Proxy ------------");
doSomething(proxySubject);
}
public static void doSomething(Subject subject){
subject.operation("flynewton");
}
}
[b]测试结果:[/b]
[quote]------------Without Proxy------------
实际操作,参数:liuwentao
------------With Proxy------------
代理操作,参数:liuwentao
实际操作,参数:liuwentao[/quote]
[b]游戏代练帮忙打怪[/b]
[img]http://dl.iteye.com/upload/attachment/552319/9ea288f9-7ece-3b25-84d1-07cb1958c64d.png[/img]
[b]1:普通代理:由代理生成真实角色,真是角色不能直接new出来[/b]
[img]http://dl.iteye.com/upload/attachment/552350/eb0b591e-6f3c-3c7f-bc72-69d2475060f6.png[/img]
约定真实角色不能 直接 new ,必须通过代理类才能操作。
真实角色,构造函数中 有 代理类参数,做判断
public GamePlayer(IGamePlayer _gamePlayer,String _name) throws Exception{
if(_gamePlayer == null ){
throw new Exception("不能创建真是角色!");
}else{
this.name = _name;
}
}
代理类构造函数中 new 真实角色
public GamePlayerProxy(String name){
try {
gamePlayer = new GamePlayer(this,name);
} catch (Exception e) {
}
}
测试类:
IGamePlayer proxy = new GamePlayerProxy("张三");
proxy.login("zhangsan","password");
[b]2:强制代理:由真是角色指定代理,不是指定的代理不能调方法[/b]
[img]http://dl.iteye.com/upload/attachment/552344/59d85147-6639-3dba-8f3b-67712d1c5609.png[/img]
测试:
//定义个游戏的角色
IGamePlayer player = new GamePlayer("张三");
//获得指定的代理
IGamePlayer proxy = player.getProxy();
//开始打游戏,记下时间戳
System.out.println("开始时间是:2009-8-25 10:45");
proxy.login("zhangSan", "password");
//开始杀怪
proxy.killBoss();
//升级
proxy.upgrade();
//记录结束游戏时间
System.out.println("结束时间是:2009-8-26 03:40");