设计模式-状态模式练习
题目
某纸牌游戏软件中,人物角色具有人门级(Primary)、熟练级(Secondary),高手绍(Professional)和骨灰级(Final)四种等级,角色的等级与其积分相对应,游戏胜利将增加和分,失败则扣除积分。人门级具有最基本的游戏功能 play(),熟练级增加丁游戏胜利积分加倍功能doubleScore),高手级在熟练级基础上再增加换牌功能 changeCards(),骨灰级在高手级基础上再增加偷看他人的牌功能 peekCards()。现使用状态模式来设计该系统,绘制类图并编程实现。
类图
规则
新手Primary: point < 30, 熟练Secondary : 30=< point < 50, 高手professionary: 50=< point < 60, 骨灰final: point > =60
其中积分翻倍,就只是加20分而已,换牌、看牌就只输出提示信息
代码
public class Pro {public static void main(String[] args) {Player player = new Player("rick");player.play_win();player.play_win();player.play_win();player.play_win();player.doubleScore();player.changeCards();player.play_win();player.peekCards();player.play_lose();player.play_lose();player.play_lose();}
}class Player {private String name;private State state;public String getName() {return name;}public void setName(String name) {this.name = name;}public State getState() {return state;}public void setState(State state) {this.state = state;}public Player(String name) {this.name = name;this.state = new PrimaryState(this);}public void play_win() {state.play_win();}public void play_lose() {state.play_lose();}public void doubleScore() {state.doubleScore();}public void changeCards() {state.changeCards();}public void peekCards() {state.peekCards();}
}abstract class State {protected String rankName;protected int point;protected Player player;public void play_lose() {point -= 10;System.out.println(rankName + "玩家" + player.getName() + "输了一局,积分-10,当前积分:" + point);checkState(point);}public void play_win() {point += 10;System.out.println(rankName + "玩家" + player.getName() + "赢了一局,积分+10,当前积分:" + point);checkState(point);}public void doubleScore() {System.out.println(rankName + "未解锁积分加倍");}public void changeCards() {System.out.println(rankName + "未解锁换牌");}public void peekCards() {System.out.println(rankName + "未解锁偷看牌");}public String getRankName() {return rankName;}public void setRankName(String rankName) {this.rankName = rankName;}public abstract void checkState(int point);
}class PrimaryState extends State {public PrimaryState(Player player) {// 初始状态this.rankName = "入门级";this.player = player;point = 0;}public PrimaryState(State state) {this.rankName = "入门级";this.player = state.player;point = state.point;}@Overridepublic void checkState(int point) {if (point >= 30) {player.setState(new SecondaryState(this));}}}class SecondaryState extends State {public SecondaryState(State primaryState) {this.player = primaryState.player;this.point = primaryState.point;this.rankName = "熟练级";}public void doubleScore() {System.out.println(rankName + "玩家" + player.getName() + "使用积分翻倍+20");point += 20;checkState(point);}@Overridepublic void checkState(int point) {if (point < 30)player.setState(new PrimaryState(this)); // state下落else if (point >= 50)player.setState(new ProfessionalState(this));}
}class ProfessionalState extends State {public ProfessionalState(State secondaryState) {this.rankName = "高手级";this.player = secondaryState.player;this.point = secondaryState.point;}@Overridepublic void doubleScore() {System.out.println(rankName + "玩家" + player.getName() + "使用积分翻倍+20");point += 20;checkState(point);}@Overridepublic void changeCards() {System.out.println(rankName + "玩家" + player.getName() + "换牌");}@Overridepublic void checkState(int point) {if (point >= 60) {player.setState(new FinalState(this));} else if (point < 50) {player.setState(new SecondaryState(this));}}
}class FinalState extends State {public FinalState(State professionalState) {this.rankName = "骨灰级";this.player = professionalState.player;this.point = professionalState.point;}@Overridepublic void doubleScore() {System.out.println(rankName + "玩家" + player.getName() + "使用积分翻倍+20");point += 20;checkState(point);}@Overridepublic void changeCards() {System.out.println(rankName + "玩家" + player.getName() + "换牌");}@Overridepublic void peekCards() {System.out.println(rankName + "玩家" + player.getName() + "偷看牌");}@Overridepublic void checkState(int point) {if (point < 60)player.setState(new ProfessionalState(this));}
}
运行结果
设计模式-状态模式练习相关推荐
- Python设计模式-状态模式
Python设计模式-状态模式 代码基于3.5.2,代码如下; #coding:utf-8 #状态模式class state():def writeProgram(self,work):raise N ...
- Java 设计模式——状态模式
概述 很多人在说状态模式的时候总拿策略模式来进行对比,可能他们的类图会有一点类似,可我却不认为他们有多么相像.你可以阅读<Java设计模式--策略模式>这篇博客,并与本文对比,以找到蛛丝马 ...
- 设计模式状态模式uml_UML的完整形式是什么?
设计模式状态模式uml UML:统一建模语言 (UML: Unified Modeling Language) UML is an abbreviation of Unified Modeling L ...
- C++设计模式——状态模式
C++设计模式--状态模式 在实际开发中,我们经常会遇到这种情况:一个对象有多种状态,在每一个状态下,都会有不同的行为.那么在代码中我们经常是这样实现的. 1 2 3 4 5 6 7 8 9 10 ...
- Java 有限状态机 (设计模式——状态模式)
Java 有限状态机 (设计模式--状态模式) 编写代码的时候,有时会遇见较为复杂的swith...case...和if...else...语句.这一刻有时会想到状态机,用有限状态机替换swith.. ...
- C++设计模式——状态模式(state pattern)
一.原理讲解 别名状态对象(object for state). 1.1意图 允许一个对象在其内部状态改变时改变它的行为.对象看起来似乎修改了它的类. 1.2应用场景 一个对象的行为取决于它的状态,并 ...
- 设计模式——状态模式详解
0. 前言 写在最前面,本人的设计模式类博文,建议先看博文前半部分的理论介绍,再看后半部分的实例分析,最后再返回来复习一遍理论介绍,这时候你就会发现我在重点处标红的用心,对于帮助你理解设计模式有奇效哦 ...
- 大话设计模式—状态模式
在状态模式(State Pattern)中,类的行为是基于它的状态改变的.这种类型的设计模式属于行为型模式.我们创建表示各种状态的对象和一个行为随着状态对象改变而改变的 context 对象. 大话设 ...
- C++设计模式-状态模式
目录 基本概念 代码与实例 基本概念 状态模式(State):当一个对象的内在状态改变时允许改变其行为,这个对象看起来就像是改变了其类: 状态模式的好处: 1. 将与特定状态相关的行为局部化,并且将不 ...
- PHP设计模式——状态模式
声明:本系列博客参考资料<大话设计模式>,作者程杰. 状态模式当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类.状态模式主要解决的是当控制一个对象状态的条件表达式过于 ...
最新文章
- springclould feign客户端添加全局参数
- A、B、C、D、E类IP地址都是怎么划分的?
- HDU6438 Buy and Resell 解题报告(一个有趣的贪心问题的严格证明)
- 将字符串分割为指定长度的子字符串
- t’触发器真值表和状态方程_清写出触发器按逻辑特性的分类;写出T触发器的状态方程。...
- linux sshd启动失败 sshd re-exec requires execution with an absolute path
- 组件化开发_思维导图
- arcgis图层叠加不匹配
- Data-mediator入门系列2
- 从零基础入门Tensorflow2.0 ----六、31 kaggle 10 monkeys模型微调
- 白帽子也能迎娶白富美:2017八大高薪信息安全认证
- 通过ssh登陆linux后使用vim时按了ctrl+s
- sRGB Color Space
- # GIT团队实战博客
- Redis 基础 - 优惠券秒杀《分布式锁(初级)》
- IT行业大致工作方向
- JAVA前端与后端交互面试题
- 超详细的VisualDL 的项目嵌入可视化教学
- NET Framework合集(官方)
- Android 实现三级联动 国家统计局全国省市县行政区域最新划分数据库