zl程序教程

您现在的位置是:首页 >  云平台

当前栏目

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

代理模式设计模式代码 深入 理解 各种 通俗
2023-09-14 09:04:36 时间
1》普通代理模式(有益于扩展开发),对代理者进行封装真实角色 public interface IGamePlayer {//登录游戏public void login(String user,String password);//杀怪,这是网络游戏的主要特色public void killBoss();//升级public void upgrade(); public

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");
}
}



设计模式轻松学【十一】装饰模式 如果要扩展一些功能,我们可以采用装饰模式来实现。装饰者模式以对客户透明的方式动态地给一个对象附加上更多的责任。换言之,客户端并不会觉得对象在装饰前和装饰后有什么不同。装饰者模式可以在不使用创造更多子类的情况下,将对象的功能加以扩展。
设计模式学习——代理模式(1) 一、什么是代理模式 为其他对象提供一种代理以控制对这个对象的访问。其类图如下: Subject:抽象主题类,它既可以是抽象类也可以是接口类,是一个最普通的业务类型定义,无特殊要求。 RealSubject:具体主题类,被代理角色,是具体业务逻辑实现执行者。 Proxy:代理类,负责对具体主题的应用,