代理模式
代理模式无非就是说某个人把自己要做的事情行为给到一个专门代理的人做,比如游戏代玩。
普通代理模式
- 首先我们假设某个游戏升级有几个步骤。登录,杀怪,升级。先定义一个游戏的接口。
/**
* Copyright (c) $(YEAR) jueying.
* All rights reserved.
* Create on 2021/10/06
* <p>
* Contributors:
* jueying - initial implementation
**/
package test.java;
/**
* 游戏升级行为接口
* @author jueying
* @date 2021/10/06
**/
public interface IGamePlayer {
/**
* 游戏登录
* @param userName
* @param passWord
*/
void login(String userName,String passWord);
/**
* 杀booss
*/
void killBoss();
/**
* 升级
*/
void upgrade();
}
- 游戏玩家角色要做的动作
/**
* Copyright (c) $(YEAR) jueying.
* All rights reserved.
* Create on 2021/10/06
* <p>
* Contributors:
* jueying - initial implementation
**/
package test.java.impl;
import test.java.IGamePlayer;
/**
* 游戏玩家
* @author jueying
* @date 2021/10/06
**/
public class GamePlayer implements IGamePlayer {
private String name;
public GamePlayer(String name){
this.name=name;
}
@Override
public void login(String userName, String passWord) {
System.out.println("用户"+name+"登录游戏.......游戏账号"+userName);
}
@Override
public void killBoss() {
System.out.println("!!!!!!恭喜你杀死了boss");
}
@Override
public void upgrade() {
System.out.println("游戏升级,请再接再厉......");
}
}
- 游戏代理玩家实现
/**
* Copyright (c) $(YEAR) jueying.
* All rights reserved.
* Create on 2021/10/06
* <p>
* Contributors:
* jueying - initial implementation
**/
package test.java.impl;
import test.java.IGamePlayer;
/**
* 游戏代练玩家
* @author jueying
* @date 2021/10/06
**/
public class GamePlayerProxy implements IGamePlayer {
private IGamePlayer gamePlayer;
public GamePlayerProxy(IGamePlayer gamePlayer) {
this.gamePlayer = gamePlayer;
}
@Override
public void login(String userName, String passWord) {
gamePlayer.login(userName, passWord);
}
@Override
public void killBoss() {
gamePlayer.killBoss();
}
@Override
public void upgrade() {
gamePlayer.upgrade();
}
}
- 游戏客户端
/**
* Copyright (c) $(YEAR) jueying.
* All rights reserved.
* Create on 2021/10/06
* <p>
* Contributors:
* jueying - initial implementation
**/
package test.java.impl;
/**
* 游戏客户端
* @author jueying
* @date 2021/10/06
**/
public class GameClient {
public static void main(String args[]){
IGamePlayer gamePlayer=new GamePlayer("张三");
gamePlayer=new GamePlayerProxy(gamePlayer);
boolean isLoginSuccess=gamePlayer.login("zhangsan","password");
if(isLoginSuccess){
gamePlayer.killBoss();
gamePlayer.upgrade();
}
}
}
5.执行结果
用户张三登录游戏.......游戏账号zhangsan
!!!!!!恭喜你杀死了boss
游戏升级,请再接再厉......
强制代理模式
和普通代理模式不同之处在于代理者是指定的,需使用特定代理者否则会请求拒绝。这里举例还是用游戏代玩,不用设计模式之禅中的明星和经纪人的例子,个人觉得不是很恰当,代理者在游戏中能做的事差不多近似等于被代理者,明星和经纪人关系和这个差别有点大。这里可以这样加以说明,虽然游戏找人代玩但是不想谁都可以代玩,技术好升级是回事,万一这人素质不好,游戏中到处乱喷且不是把被代理者名声搞坏了。想要指定特定代理者来玩,那就在开始游戏时如果发现不是指定人那就无法进行游戏。游戏代理者不再是通过代理者来决定,而是要通过被代理者的方法来获取,否则执行就会出错。
- 游戏接口
/**
* Copyright (c) $(YEAR) jueying.
* All rights reserved.
* Create on 2021/10/06
* <p>
* Contributors:
* jueying - initial implementation
**/
package test.java;
/**
* 游戏升级行为接口
*
* @author jueying
* @date 2021/10/06
**/
public interface IGamePlayer {
/**
* 游戏登录
*
* @param userName
* @param passWord
*/
boolean login(String userName, String passWord);
/**
* 杀booss
*/
void killBoss();
/**
* 升级
*/
void upgrade();
/**
* 游戏玩家,默认是自己可以指定代理
*
* @return
*/
IGamePlayer getGamePlayer();
}
2.游戏玩家
/**
* Copyright (c) $(YEAR) jueying.
* All rights reserved.
* Create on 2021/10/06
* <p>
* Contributors:
* jueying - initial implementation
**/
package test.java.impl;
import test.java.IGamePlayer;
/**
* 游戏玩家
*
* @author jueying
* @date 2021/10/06
**/
public class GamePlayer implements IGamePlayer {
private String name;
private GamePlayerProxy gamePlayerProxy;
public GamePlayer(String name) {
this.name = name;
}
@Override
public boolean login(String userName, String passWord) {
if (gamePlayerProxy == null) {
System.out.println("!!!!!!!!!!!你不是被代理者指定的游戏代理者无权登陆进行游戏......");
return false;
} else {
System.out.println("用户" + name + "登录游戏.......游戏账号" + userName);
return true;
}
}
@Override
public void killBoss() {
System.out.println("!!!!!!恭喜你杀死了boss");
}
@Override
public void upgrade() {
System.out.println("游戏升级,请再接再厉......");
}
/**
* 有的游戏玩家指定了游戏代玩者
*
* @return
*/
@Override
public IGamePlayer getGamePlayer() {
this.gamePlayerProxy = new GamePlayerProxy(this);
return gamePlayerProxy;
}
}
3.游戏代理者
/**
* Copyright (c) $(YEAR) jueying.
* All rights reserved.
* Create on 2021/10/06
* <p>
* Contributors:
* jueying - initial implementation
**/
package test.java.impl;
import test.java.IGamePlayer;
/**
* 游戏代练玩家
*
* @author jueying
* @date 2021/10/06
**/
public class GamePlayerProxy implements IGamePlayer {
private IGamePlayer gamePlayer;
public GamePlayerProxy(IGamePlayer gamePlayer) {
this.gamePlayer = gamePlayer;
}
@Override
public boolean login(String userName, String passWord) {
return gamePlayer.login(userName, passWord);
}
@Override
public void killBoss() {
gamePlayer.killBoss();
}
@Override
public void upgrade() {
gamePlayer.upgrade();
}
@Override
public IGamePlayer getGamePlayer() {
return gamePlayer;
}
}
4.客户端1
/**
* Copyright (c) $(YEAR) jueying.
* All rights reserved.
* Create on 2021/10/06
* <p>
* Contributors:
* jueying - initial implementation
**/
package test.java.impl;
import test.java.IGamePlayer;
/**
* 游戏客户端1
*
* @author jueying
* @date 2021/10/06
**/
public class GameClient1 {
public static void main(String args[]) {
IGamePlayer gamePlayer = new GamePlayer("张三");
gamePlayer = gamePlayer.getGamePlayer();
boolean isLoginSuccess = gamePlayer.login("zhangsan", "password");
if (isLoginSuccess) {
gamePlayer.killBoss();
gamePlayer.upgrade();
}
}
}
5.客户端1输出结果
用户张三登录游戏.......游戏账号zhangsan
!!!!!!恭喜你杀死了boss
游戏升级,请再接再厉......
6.如果此时客户端仍然和普通代理模式的实现一致,会看到如下输出,因为指定了代理者就必须要从被代理者中获取特定代理类,否则就会抛出错,这就是强制代理模式和普通代理模式的区别。
!!!!!!!!!!!你不是被代理者指定的游戏代理者无权登陆进行游戏......