12.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");
}
}
12.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() {
System.out.println("In proxy: killBoss.");
this.gamePlayer.killBoss();
}
//代练登录
public void login(String user, String password) {
System.out.println("In proxy: login.");
this.gamePlayer.login(user, password);
}
//代练升级
public void upgrade() {
System.out.println("In proxy: upgrade.");
this.gamePlayer.upgrade();
}
//代理的代理暂时还没有,就是自己
public IGamePlayer getProxy(){
return this;
}
}
public class Client {
public static void main(String[] args) {
//定义个游戏的角色
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");
}
}
12.3 个性代理
public interface IGamePlayer {
//登录游戏
public void login(String user,String password);
//杀怪,这是网络游戏的主要特色
public void killBoss();
//升级
public void upgrade();
}
public interface IProxy {
//计算费用
public void count();
}
public class GamePlayer implements IGamePlayer {
private String name = "";
//通过构造函数传递名称
public GamePlayer(String _name){
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,IProxy {
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();
this.count();
}
//计算费用
public void count(){
System.out.println("升级总费用是:150元");
}
}
public class Client {
public static void main(String[] args) {
//定义一个痴迷的玩家
IGamePlayer player = new GamePlayer("张三");
//然后再定义一个代练者
IGamePlayer proxy = new GamePlayerProxy(player);
//开始打游戏,记下时间戳
System.out.println("开始时间是:2009-8-25 10:45");
proxy.login("zhangSan", "password");
//开始杀怪
proxy.killBoss();
//升级
proxy.upgrade();
//记录结束游戏时间
System.out.println("结束时间是:2009-8-26 03:40");
}
}
12.4 虚拟代理
public interface UserModelApi {
public String getUserId();
public void setUserId(String userId);
public String getName();
public void setName(String name);
public String getDepId();
public void setDepId(String depId);
public String getSex();
public void setSex(String sex);
}
public class UserModel implements UserModelApi{
private String userId;
private String name;
private String depId;
private String sex;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepId() {
return depId;
}
public void setDepId(String depId) {
this.depId = depId;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "UserModel{" +
"userId='" + userId + '\'' +
", name='" + name + '\'' +
", depId='" + depId + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
public class Proxy implements UserModelApi{
private UserModel realSubject = null;
public Proxy(UserModel realSubject) {
this.realSubject = realSubject;
}
private boolean loaded = false;
public String getUserId() {
return realSubject.getUserId();
}
public void setUserId(String userId) {
realSubject.setUserId(userId);
}
public String getName() {
return realSubject.getName();
}
public void setName(String name) {
realSubject.setName(name);
}
public String getDepId() {
if(!this.loaded) {
reload();
this.loaded = true;
}
return realSubject.getDepId();
}
public void setDepId(String depId) {
realSubject.setDepId(depId);
}
public String getSex() {
if(!this.loaded) {
reload();
this.loaded = true;
}
return realSubject.getSex();
}
public void setSex(String sex) {
realSubject.setSex(sex);
}
private void reload() {
System.out.println("Querying the DB for fetching the complete data. userId: " + realSubject.getUserId());
Connection conn = null;
try {
conn = this.getConnection();
String sql = "select * from tbl_user where userId=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, realSubject.getUserId());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
realSubject.setDepId(rs.getString("depId"));
realSubject.setSex(rs.getString("sex"));
}
rs.close();
pstmt.close();
} catch (Exception err) {
err.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
public String toString() {
return "Proxy{" +
"userId=" + getUserId() +
", name=" + getName() +
", depId=" + getDepId() +
", sex=" + getSex() +
'}';
}
private Connection getConnection() throws Exception {
Class.forName("oracle.jdbc.OracleDriver");
return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "yanmo", "yanmo");
}
}
public class UserManager {
public Collection getUserByDepId(String depId) throws Exception {
Collection col = new ArrayList();
Connection conn = null;
try {
conn = this.getConnection();
String sql = "select u.userId, u.name from tbl_user u, tbl_dep d where u.depId = d.depId and d.depId like ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, depId+"%");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Proxy proxy = new Proxy(new UserModel());
proxy.setUserId(rs.getString("userId"));
proxy.setName(rs.getString("name"));
col.add(proxy);
}
rs.close();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
} finally{
conn.close();
}
return col;
}
private Connection getConnection() throws Exception {
Class.forName("oracle.jdbc.OracleDriver");
return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "yanmo", "yanmo");
}
}
public class ProxyClient {
public static void main(String[] args) throws Exception {
UserManager userManager = new UserManager();
Collection col = userManager.getUserByDepId("0101");
for (UserModelApi umApi : col) {
System.out.println("userId: " + umApi.getUserId() + ", userName: " + umApi.getName());
}
for (UserModelApi umApi : col) {
System.out.println("userId: " + umApi.getUserId() + ", userName: " + umApi.getName() + ", depId: " + umApi.getDepId());
}
}
}
12.5 动态代理
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(String _name){
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 GamePlayIH implements InvocationHandler {
//被代理者
Class cls =null;
//被代理的实例
Object obj = null;
//我要代理谁
public GamePlayIH(Object _obj){
this.obj = _obj;
}
//调用被代理的方法
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = method.invoke(this.obj, args);
//如果是登录方法,则发送信息
if(method.getName().equalsIgnoreCase("login")){
System.out.println("有人在用我的账号登陆!");
}
return result;
}
}
public class Client {
public static void main(String[] args) throws Throwable {
//定义一个痴迷的玩家
IGamePlayer player = new GamePlayer("张三");
//然后再定义一个代练者
GamePlayIH proxy = new GamePlayIH(player);
//开始打游戏,记下时间戳
System.out.println("开始时间是:2009-8-25 10:45");
String str[] = {"zhangSan","password"};
Class type[] = {String.class,String.class};
proxy.invoke(null, player.getClass().getMethod("login", type),str);
//开始杀怪
proxy.invoke(null, player.getClass().getMethod("killBoss", null),null);
//升级
proxy.invoke(null, player.getClass().getMethod("upgrade", null),null);
//记录结束游戏时间
System.out.println("结束时间是:2009-8-26 03:40");
}
}