设计模式练习(18)——状态模式
2017-01-20 16:09
1056 查看
状态模式
一、题目:某纸牌游戏中,人物角色具有入门级(Primary),熟练级(Secondary),高手级(Professional)和骨灰级(Final)四种等级,角色的等级与积分相对应,游戏胜利将增加积分,失败则扣除积分,入门级具有最基本的游戏功能play(),熟练级增加了游戏胜利积分加倍功能doubleScore(),高手级在熟练级基础上再增加换牌功能changeCards(),骨灰级在高手级基础上再增加偷看他人的牌的功能peekCards(),使用状态模式来设计系统
(1) 绘制状态模式结构视图
(2) 给出实例类图并实现代码
二、所用模式结构视图:
三、实例类图:
四、实例实现代码:
(因为区分,所以在类的前面加了Gj19)
环境类-玩家实体类
package gjStatePattern; /** * 玩家实体类 * @author gong * */ public class Gj19Player { private Gj19AbstractLevel level; private String name; private boolean isSuccess; public Gj19Player(String name) { this.level=new Gj19PrimaryLevel(this); this.name = name; } public boolean isSuccess() { return isSuccess; } public void setSuccess(boolean isSuccess) { this.isSuccess = isSuccess; } public Gj19AbstractLevel getLevel() { return level; } public void setLevel(Gj19AbstractLevel level) { this.level = level; } public String getName() { return name; } public void play(int score){ level.play(score); } public void doubleScore(int score){ level.doubleScore(score); } public void changeCards(int score){ level.changeCards(score); } public void peekCards(int score){ level.peekCards(score); } }
抽象状态类
package gjStatePattern; /** * 抽象状态类 * @author gong * */ public abstract class Gj19AbstractLevel { protected Gj19Player player; protected String stateName; protected int point; public abstract void checkState(int score); public int getPoint() { return point; } public void setPoint(int point) { this.point = point; } public String getStateName() { return stateName; } public void setStateName(String stateName) { this.stateName = stateName; } public void play(int score){ System.out.println(player.getName()+"拥有"+this.point+"积分,开始玩纸牌游戏"); if(player.isSuccess()==true){ this.point+=score; } else if(point>score&&player.isSuccess()==false){ System.out.println("分数已经最低,不可以再扣分了"); } else if(player.isSuccess()==false){ this.point-=score; } System.out.println(player.isSuccess()==true?"你的牌打得忒好了,赢得一场比赛":"同志任需努力,输了一场比赛"); checkState(score); System.out.println("积分剩余:"+this.point+",当前级别:"+player.getLevel().getStateName()); } public void doubleScore(int score){ System.out.println(player.getName()+"拥有"+this.point+"积分,游戏胜利可以积分加倍"); if(player.isSuccess()==true){ this.point+=2*score; } else if(point>score&&player.isSuccess()==false){ System.out.println("分数已经最低,不可以再扣分了"); } else if(player.isSuccess()==false){ this.point-=score; } System.out.println(player.isSuccess()==true?"你的牌打得忒好了,赢得一场比赛":"同志任需努力,输了一场比赛"); checkState(score); System.out.println("积分剩余:"+this.point+",当前级别:"+player.getLevel().getStateName()); } public void changeCards(int score){ System.out.println(player.getName()+"拥有"+this.point+"积分,可以和其他玩家换牌"); if(player.isSuccess()==true){ this.point+=score; } else if(point>score&&player.isSuccess()==false){ System.out.println("分数已经最低,不可以再扣分了"); } else if(player.isSuccess()==false){ this.point-=score; } System.out.println(player.isSuccess()==true?"你的牌打得忒好了,赢得一场比赛":"同志任需努力,输了一场比赛"); checkState(score); System.out.println("积分剩余:"+this.point+",当前级别:"+player.getLevel().getStateName()); } public void peekCards(int score){ System.out.println(player.getName()+"拥有"+this.point+"积分,可以偷看他人的牌"); if(player.isSuccess()==true){ this.point+=score; } else if(point>score&&player.isSuccess()==false){ System.out.println("分数已经最低,不可以再扣分了"); } else if(player.isSuccess()==false){ this.point-=score; } System.out.println(player.isSuccess()==true?"你的牌打得忒好了,赢得一场比赛":"同志任需努力,输了一场比赛"); checkState(score); System.out.println("积分剩余:"+this.point+",当前级别:"+player.getLevel().getStateName()); } }
具体状态类
package gjStatePattern; /** * 具体状态类 * @author gong * */ public class Gj19PrimaryLevel extends Gj19AbstractLevel { public Gj19PrimaryLevel(Gj19AbstractLevel level) { this.player=level.player; this.point=level.getPoint(); this.stateName="入门级玩家"; } public Gj19PrimaryLevel(Gj19Player player) { this.player=player; this.stateName="入门级玩家"; this.point=0; } public void doubleScore(int score){ System.out.println("对不起"+player.getName()+",您没有获胜双倍积分的权利"); } @Override public void checkState(int score) { if(this.point>1000){ player.setLevel(new Gj19FinalLevel(this)); } else if(this.point>500){ player.setLevel(new Gj19ProfessionalLevel(this)); } else if(this.point>200){ player.setLevel(new Gj19SecondaryLevel(this)); } } }
具体状态类
package gjStatePattern; /** * 具体状态类 * @author gong * */ public class Gj19SecondaryLevel extends Gj19AbstractLevel { public Gj19SecondaryLevel(Gj19AbstractLevel level) { this.player=level.player; this.point=level.getPoint(); this.stateName="熟练级玩家"; } public void changeCards(int score){ System.out.println("对不起"+player.getName()+",您没有换牌的权利"); } public void peekCards(int score){ System.out.println("对不起"+player.getName()+",您没有偷看别人牌的权利"); } @Override public void checkState(int score) { if(this.point>1000){ player.setLevel(new Gj19FinalLevel(this)); } else if(this.point>500){ player.setLevel(new Gj19ProfessionalLevel(this)); } else if(this.point<200){ player.setLevel(new Gj19PrimaryLevel(this)); } } }
具体状态类
package gjStatePattern; /** * 具体状态类 * @author gong * */ public class Gj19ProfessionalLevel extends Gj19AbstractLevel { public Gj19ProfessionalLevel(Gj19AbstractLevel level) { this.player=level.player; this.point=level.getPoint(); this.stateName="高手级玩家"; } public void peekCards(int score){ System.out.println("对不起"+player.getName()+",您没有偷看别人牌的权利"); } @Override public void checkState(int score) { if(this.point>1000){ player.setLevel(new Gj19FinalLevel(this)); } else if(this.point<200){ player.setLevel(new Gj19PrimaryLevel(this)); } else if(this.point<500){ player.setLevel(new Gj19SecondaryLevel(this)); } } }
具体状态类
package gjStatePattern; /** * 具体状态类 * @author gong * */ public class Gj19FinalLevel extends Gj19AbstractLevel { public Gj19FinalLevel(Gj19AbstractLevel level) { this.player=level.player; this.point=level.getPoint(); this.stateName="骨灰级玩家"; } @Override public void checkState(int score) { if(this.point<1000){ player.setLevel(new Gj19ProfessionalLevel(this)); } else if(this.point<500){ player.setLevel(new Gj19SecondaryLevel(this)); } else if(this.point<200){ player.setLevel(new Gj19PrimaryLevel(this)); } } }
状态模式客户端
package gjStatePattern; /** * 状态模式客户端 * @author gong * */ public class Gj19Client { public static void main(String[] args) { Gj19Player player1=new Gj19Player("周小妹"); player1.setSuccess(true); player1.play(200); System.out.println("-----------------------------------------------"); player1.setSuccess(true); player1.doubleScore(200); System.out.println("==============================================="); player1.setSuccess(true); player1.play(200); System.out.println("==============================================="); player1.setSuccess(true); player1.doubleScore(200); System.out.println("-----------------------------------------------"); player1.setSuccess(true); player1.peekCards(200); System.out.println("==============================================="); player1.setSuccess(true); player1.doubleScore(200); System.out.println("==============================================="); } }
五、运行结果:
相关文章推荐
- 如何在Tomcat中部署应用的多个版本
- 解决play out of memory的问题
- PropertyChangeListener简单理解
- 什么是设计模式
- 设计模式之创建型模式 - 特别的变量问题
- 我是运营,我没有假期
- 二、设计模式——状态模式
- 七、设计模式——装饰模式
- 设计模式总结
- 设计模式之创建型模式
- 浅谈设计模式的学习
- 每个 Linux 游戏玩家都绝不想要的恼人体验
- 设计模式---状态模式在web前端中的应用
- 在 Fedora 上使用 Steam play 和 Proton 来玩 Windows 游戏
- Steam 让我们在 Linux 上玩 Windows 的游戏更加容易
- 如何使用 Steam Play 在 Linux 上玩仅限 Windows 的游戏
- 新一代iPad适配应用之游戏篇
- VB实现的《QQ美女找茬游戏》作弊器实例
- Ruby设计模式编程之适配器模式实战攻略