请编写程序完成APP抽奖活动 具体要求如下:
① 假如每参加一次这个活动要扣除用户50积分,中奖概率是10%
② 奖品数量固定,抽完就不能抽奖
③ 活动有四个状态: 可以抽奖、不能抽奖、发放奖品和奖品领完
④ 活动的四个状态转换关系图
状态模式
状态模式(State Pattern):它主要用来解决对象在多种状态转换时,需要对外输出不同的行为的问题。状态和行为是一一对应的,状态之间可以相互转换。当一个对象的内在状态改变时,允许改变其行为,这个对象看起来像是改变了其类。
状态模式原理类图
对原理类图的说明
- Context 类为环境角色, 用于维护State实例,这个实例定义当前状态
- State 是抽象状态角色,定义一个接口封装与Context 的一个特点接口相关行为
- ConcreteState 具体的状态角色,每个子类实现一个与Context 的一个状态相关行为
代码实现:
public interface State {
public void deducePoint(int point);
public boolean drawPrice();
public void receivePrice(int priceCount);
}
public class Activity {
private int point = 0;
private int priceCount = 0;
private State state = null;
private boolean isOver = false;
private boolean isWin = false;
private boolean isNext = true;
private State prepareState = new PrepareState(this);
private State receivePriceState = new ReceivePriceState(this);
private State drawPriceState = new DrawPriceState(this);
public Activity(int point, int priceCount) {
state = getPrepareState();
this.point = point;
this.priceCount = priceCount;
}
public void deducePoint() {
state.deducePoint(point);
}
public void drawPrice() {
if(isNext) {
isWin = state.drawPrice();
}
}
public void receivePrice() {
if(isWin) {
state.receivePrice(priceCount);
}
}
public State getPrepareState() {
return prepareState;
}
public State getReceivePriceState() {
return receivePriceState;
}
public State getDrawPriceState() {
return drawPriceState;
}
public void setState(State state) {
this.state = state;
isOver = false;
isWin = false;
isNext = true;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
public int getPriceCount() {
return priceCount;
}
public void setPriceCount(int priceCount) {
this.priceCount = priceCount;
}
public boolean isOver() {
return isOver;
}
public void setOver(boolean isOver) {
this.isOver = isOver;
}
public boolean isNext() {
return isNext;
}
public void setNext(boolean isNext) {
this.isNext = isNext;
}
}
public class PrepareState implements State{
private Activity activity;
public PrepareState(Activity activity) {
this.activity = activity;
}
@Override
public void deducePoint(int point) {
if(point >= 50 && activity.getPriceCount() > 0) {
point -= 50;
activity.setPoint(point);
activity.setState(activity.getDrawPriceState());
System.out.println("已经扣除50积分!");
}else {
activity.setNext(false);
activity.setOver(true);
if(point < 50) {
System.out.println("积分不足,不能参加抽奖!");
}else {
System.out.println("真遗憾,已经没有奖品了!");
}
}
}
@Override
public boolean drawPrice() {
System.out.println("请先扣除积分!");
return false;
}
@Override
public void receivePrice(int priceCount) {
System.out.println("请先扣除积分!");
}
}
public class DrawPriceState implements State{
private Activity activity;
public DrawPriceState(Activity activity) {
this.activity = activity;
}
@Override
public void deducePoint(int point) {
System.out.println("已经扣除了积分,不用再次扣除!");
}
@Override
public boolean drawPrice() {
System.out.println("正在抽奖,请耐心等待!");
Random random = new Random();
int i = random.nextInt(10);
if(i == 0) {
activity.setState(activity.getReceivePriceState());
System.out.println("恭喜您中奖了!");
return true;
}else {
activity.setState(activity.getPrepareState());
System.out.println("很遗憾,没中奖,欢迎下次再来!");
return false;
}
}
@Override
public void receivePrice(int priceCount) {
System.out.println("请先参与抽奖! ");
}
}
public class ReceivePriceState implements State{
private Activity activity;
public ReceivePriceState(Activity activity) {
this.activity = activity;
}
@Override
public void deducePoint(int point) {
System.out.println("您已经被扣除了积分,不用再次扣除!");
}
@Override
public boolean drawPrice() {
System.out.println("您已经参与抽奖了!");
return false;
}
@Override
public void receivePrice(int priceCount) {
if(priceCount > 0) {
priceCount --;
activity.setPriceCount(priceCount);
activity.setState(activity.getPrepareState());
System.out.println("奖品已经发放成功,欢迎下次再来!");
}else {
activity.setOver(true);
System.out.println("真遗憾,没有奖品了!");
}
}
}
public class Test {
public static void main(String[] args) {
Activity activity = new Activity(500, 1);
int i = 1;
while(!activity.isOver()) {
System.out.println("==============第"+ i +"次抽奖==================");
activity.deducePoint();
activity.drawPrice();
activity.receivePrice();
i++;
}
}
}
评价:
优势:
① 代码有很强的可读性。状态模式将每个状态的行为封装到对应的一个类中
② 方便维护。将容易产生问题的if-else语句删除了,如果把每个状态的行为都放到一个类中,每次调用方法时都要判断当前是什么状态,不但会产出很多if-else语句,而且容易出错
③ 符合“开闭原则”,容易增删状态
不足:
会产生很多类。每个状态都要一个对应的类,当状态过多时会产生很多类,加大维护难度
应用场景:
当一个事件或者对象有很多种状态,状态之间会相互转换,对不同的状态要求有不同的行为的时候,可以考虑使用状态模式