设计模式-深入理解各种代理模式(1)通俗代码版

1》普通代理模式(有益于扩展开发),对代理者进行封装真实角色

public interface IGamePlayer {


//登录游戏
public void login(String user,String password);

//杀怪,这是网络游戏的主要特色
public void killBoss();

//升级
public void upgrade();
}

---

public class GamePlayer implements IGamePlayer {
private String name = "";

//构造函数限制谁能创建对象,并同时传递姓名
public GamePlayer(IGamePlayer _gamePlayer,String _name) throws Exception{
if(_gamePlayer == null ){
throw new Exception("不能创建真是角色!");
}else{
this.name = _name;
}


}
//打怪,最期望的就是杀老怪
public void killBoss() {
System.out.println(this.name + "在打怪!");
}

//进游戏之前你肯定要登录吧,这是一个必要条件
public void login(String user, String password) {
System.out.println("登录名为"+user + " 的用户 " + this.name + "登录成功!");
}


//升级,升级有很多方法,花钱买是一种,做任务也是一种
public void upgrade() {
System.out.println(this.name + " 又升了一级!");
}


}

----

public class GamePlayerProxy implements IGamePlayer {
private IGamePlayer gamePlayer = null;

//通过构造函数传递要对谁进行代练,封装代理者
public GamePlayerProxy(String name){
try {
gamePlayer = new GamePlayer(this,name);
} catch (Exception e) {
// TODO 异常处理
}
}


//代练杀怪
public void killBoss() {
this.gamePlayer.killBoss();
}


//代练登录
public void login(String user, String password) {
this.gamePlayer.login(user, password);


}


//代练升级
public void upgrade() {
this.gamePlayer.upgrade();


}


}

-------

public class Client {
public static void main(String[] args) {
//然后再定义一个代练者
IGamePlayer proxy = new GamePlayerProxy("张三");

//开始打游戏,记下时间戳
System.out.println("开始时间是:2009-8-25 10:45");
proxy.login("zhangSan", "password");
//开始杀怪
proxy.killBoss();
//升级
proxy.upgrade();
//记录结束游戏时间
System.out.println("结束时间是:2009-8-26 03:40");

}
}


2》强制代理模式:真实角色找指定代理

public interface IGamePlayer {
//登录游戏
public void login(String user,String password);

//杀怪,这是网络游戏的主要特色
public void killBoss();

//升级
public void upgrade();

//每个人都可以找一下自己的代理
public IGamePlayer getProxy();
}

----

public class GamePlayer implements IGamePlayer {
private String name = "";
//我的代理是谁
private IGamePlayer proxy = null;

public GamePlayer(String _name){
this.name = _name;
}

//找到自己的代理
public IGamePlayer getProxy(){
this.proxy = new GamePlayerProxy(this);
return this.proxy;
}


//打怪,最期望的就是杀老怪
public void killBoss() {
if(this.isProxy()){
System.out.println(this.name + "在打怪!");
}else{
System.out.println("请使用指定的代理访问");
}

}

//进游戏之前你肯定要登录吧,这是一个必要条件
public void login(String user, String password) {
if(this.isProxy()){
System.out.println("登录名为"+user + " 的用户 " + this.name + "登录成功!");
}else{
System.out.println("请使用指定的代理访问");;
}

}


//升级,升级有很多方法,花钱买是一种,做任务也是一种
public void upgrade() {
if(this.isProxy()){
System.out.println(this.name + " 又升了一级!");
}else{
System.out.println("请使用指定的代理访问");
}
}

//校验是否是代理访问
private boolean isProxy(){
if(this.proxy == null){
return false;
}else{
return true;
}
}

}


----

public class GamePlayerProxy implements IGamePlayer {
private IGamePlayer gamePlayer = null;

//构造函数传递用户名
public GamePlayerProxy(IGamePlayer _gamePlayer){
this.gamePlayer = _gamePlayer;
}

//代练杀怪
public void killBoss() {
this.gamePlayer.killBoss();
}

//代练登录
public void login(String user, String password) {
this.gamePlayer.login(user, password);

}

//代练升级
public void upgrade() {
this.gamePlayer.upgrade();

}

//代理的代理暂时还没有,就是自己
public IGamePlayer getProxy(){
return this;
}

}

----

public class Client {
public static void main(String[] args) {
IGamePlayer proxy=null;
//定义个游戏的角色
IGamePlayer player = new GamePlayer("张三");
//真实角色
//proxy=player; 
//新建一个代理者
//proxy = new GamePlayerProxy(player);
//获得指定的代理(代理自己指定的代理)
proxy = player.getProxy();//真实角色找到指定代理
System.out.println(">>>"+proxy);
//System.out.println(proxy);
//开始打游戏,记下时间戳
System.out.println("开始时间是:2009-8-25 10:45");
proxy.login("zhangSan", "password");
//开始杀怪
proxy.killBoss();
//升级
proxy.upgrade();
//记录结束游戏时间
System.out.println("结束时间是:2009-8-26 03:40");
}
}


posted @ 2014-09-21 21:42  172257861  阅读(87)  评论(0编辑  收藏  举报