一,前言:
状态模式的注意事项和细节:
- 代码有很强的可读性,状态模式可将每个状态的行为封装到对应的一个类中。
- 遵循 ”开闭原则“,容易增删状态。
- 当一个事件或对象有很多的状态并需要互相转换,不同状态要求有不同行为的时候,考虑用状态模式。
二,状态模式:
2.1,UML图:
2.2、代码实例:
Step 1) 创建 State 抽象类:
public abstract class State {
public abstract void deductMoney();
public abstract boolean raffle();
public abstract void dispensePrize();
}
Step 2) 创建 NoRaffleState 实现类:
public class NoRaffleState extends State{
RaffleActiveity activeity;
public NoRaffleState(RaffleActiveity activeity) {
this.activeity = activeity;
}
@Override
public void deductMoney() {
System.out.println("扣除 50 积分成功,您可以抽奖了!");
activeity.setState(activeity.getCanRaffleState());
}
@Override
public boolean raffle() {
System.out.println("扣了积分才可以抽奖");
return false;
}
@Override
public void dispensePrize() {
System.out.println("不能发放奖品!");
}
}
Step 3) 创建 CanRaffleState 实现类:
public class CanRaffleState extends State {
Activeity activeity;
public CanRaffleState(Activeity activeity) {
this.activeity = activeity;
}
@Override
public void deductMoney() {
System.out.println("已经扣除过了积分!");
}
@Override
public boolean raffle() {
System.out.println("正在抽奖,请稍等!");
Random r = new Random();
int num = r.nextInt(10);
if(num == 0){
activeity.setState(activeity.getDispenseState());
return true;
}else {
System.out.println("很遗憾,没有抽到奖品!");
activeity.setState(activeity.getNoRaffleState());
return false;
}
}
@Override
public void dispensePrize() {
System.out.println("没中奖,不能发放奖品!");
}
}
Step 4) 创建 DispenseState 实现类:
public class DispenseState extends State{
Activeity activeity;
public DispenseState(Activeity activeity) {
this.activeity = activeity;
}
@Override
public void deductMoney() {
System.out.println("不能扣除积分 !");
}
@Override
public boolean raffle() {
System.out.println("不能抽奖!");
return false;
}
@Override
public void dispensePrize() {
if(activeity.getCount() > 0){
System.out.println("恭喜你中奖了 !");
activeity.setState(activeity.getNoRaffleState());
}else {
System.out.println("中奖了,很遗憾,奖品发完了 !");
activeity.setState(activeity.getDispensOutState());
System.out.println("抽奖结束 !");
System.exit(0);
}
}
}
Step 5) 创建 DispenOutState 实现类:
public class DispenOutState extends State{
Activeity activeity;
public DispenOutState(Activeity activeity) {
this.activeity = activeity;
}
@Override
public void deductMoney() {
System.out.println("奖品发送完了,请下次再参加 !");
}
@Override
public boolean raffle() {
System.out.println("奖品发送完了,请下次再参加 !");
return false;
}
@Override
public void dispensePrize() {
System.out.println("奖品发送完了,请下次再参加 !");
}
}
Step 6) 创建 Activeity 类:
public class Activeity {
State state = null;
int count = 0;
State noRaffleState = new NoRaffleState(this);
State canRaffleState = new CanRaffleState(this);
State dispenseState = new DispenseState(this);
State dispensOutState = new DispenOutState(this);
public Activeity(int count) {
this.state = getNoRaffleState();
this.count = count;
}
public void deductMoney() {
state.deductMoney();
}
public void raffle() {
if(state.raffle()){
state.dispensePrize();
}
}
public State getState(){
return state;
}
public void setState(State state){
this.state = state;
}
public int getCount(){
int curCount = count;
count--;
return curCount;
}
public void setCount(int count){
this.count = count;
}
public State getNoRaffleState(){
return noRaffleState;
}
public void setNoRaffleState(State noRaffleState) {
this.noRaffleState = noRaffleState;
}
public State getCanRaffleState() {
return canRaffleState;
}
public void setCanRaffleState(State canRaffleState) {
this.canRaffleState = canRaffleState;
}
public State getDispenseState() {
return dispenseState;
}
public void setDispenseState(State dispenseState) {
this.dispenseState = dispenseState;
}
public State getDispensOutState() {
return dispensOutState;
}
public void setDispensOutState(State dispensOuttate) {
this.dispensOutState = dispensOuttate;
}
}
Step 7) 创建 main 方法:
public class ClientTest {
public static void main(String[] args) {
Activeity activeity = new Activeity(1);
for(int i=0; i<30; i++){
System.out.println("----------第" + (i+1) + "次抽奖----------");
activeity.deductMoney();
activeity.raffle();
}
}
}
•由 ChiKong_Tam 写于 2020 年 10 月 23 日