《设计模式之禅》--代理扩展:强制代理

接上篇《设计模式之禅》--工厂方法扩展:实现单例

需求:就算你找到真实角色,也强制你使用指定代理

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 player = new GamePlayer("张三");
        //开始打游戏,记下时间戳
        System.out.println("开始时间是:2009-8-25 10:45");
        player.login("zhangSan", "password");
        //开始杀怪
        player.killBoss();
        //升级
        player.upgrade();
        //记录结束游戏时间
        System.out.println("结束时间是:2009-8-26 03:40");
    }
}

 结果

开始时间是:2009-8-25 10:45
请使用指定的代理访问
请使用指定的代理访问
请使用指定的代理访问
结束时间是

第二次尝试

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

结果

开始时间是:2009-8-25 10:45
请使用指定的代理访问
请使用指定的代理访问
请使用指定的代理访问
结束时间是:2009-8-26 03:40

第三次尝试

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

结果

开始时间是:2009-8-25 10:45
登录名为zhangSan 的用户张三登录成功!
张三在打怪!
张三 又升了一级!
结束时间是:2009-8-26 03:40

 

posted @ 2018-02-09 16:39  嘉禾世兴  阅读(190)  评论(0编辑  收藏  举报