2 篇文章 0 订阅

# 2. 分支逻辑法

public enum State {

SMALL(0),
SUPER(1),
FIRE(2),
CAPE(3);

private int value;

private State(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}




public class MarioStateMachine {
// 积分
private int score;
// 状态
private State currentState;

public MarioStateMachine() {
this.score = 0;
this.currentState = State.SMALL;
}

// 事件处理：在不同状态下事件所触发的状态转移、动作执行
public void obtainMushRoom() {
if (currentState.equals(State.SMALL)) {
this.currentState = State.SUPER;
this.score += 100;
}
}
public void meetMonster() {
if (currentState.equals(State.SUPER)) {
this.currentState = State.SMALL;
this.score -= 100;
return;
}
if (currentState.equals(State.CAPE)) {
this.currentState = State.SMALL;
this.score -= 200;
return;
}
if (currentState.equals(State.FIRE)) {
this.currentState = State.SMALL;
this.score -= 300;
return;
}
}
}


# 3. 查表法

public enum Event {

GOT_MUSHROOM(0),
GOT_CAPE(1),
GOT_FIRE(2),
MET_MONSTER(3);

private int value;

private Event(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}



public class MarioStateMachine {
private int score;
private State currentState;

private static final State[][] TRANSITION_TABLE = {
{SUPER, CAPE, FIRE, SMALL},
{SUPER, CAPE, FIRE, SMALL},
{CAPE, CAPE, CAPE, SMALL},
{FIRE, FIRE, FIRE, SMALL}
};

private static final int[][] ACTION_TABLE = {
{+100, +200, +300, +0},
{+0, +200, +300, -100},
{+0, +0, +0, -200},
{+0, +0, +0, -300}
};

public MarioStateMachine() {
this.score = 0;
this.currentState = State.SMALL;
}

// 事件处理：根据当前状态和事件，查询TRANSITION_TABLE和ACTION_TABLE表，获得转移后的新状态和需要执行的动作
public void executeEvent(Event event) {
int stateValue = currentState.getValue();
int eventValue = event.getValue();
this.currentState = TRANSITION_TABLE[stateValue][eventValue];
this.score = ACTION_TABLE[stateValue][eventValue];
}
}


# 4. 状态模式

IMario是状态的接口，定义了所有事件的响应方法。

public interface IMario {

State getName();

//以下是定义的事件
void obtainMushRoom();
void obtainCape();
void obtainFireFlower();
void meetMonster();
}



SmallMario、SuperMario等是状态的具体实现类，分别对应状态机中的4个状态。具体实现了在当前状态下，每个事件的状态转移和动作执行，这些原本在MarioStateMachine中，现在分散到具体的状态类中了。

public class SmallMario implements IMario {

// 状态类也依赖于状态机，用于修改其状态。
private MarioStateMachine stateMachine;

public SmallMario(MarioStateMachine stateMachine) {
this.stateMachine = stateMachine;
}

// 实现事件的具体逻辑：状态转移、动作执行
@Override
public void obtainMushRoom() {
stateMachine.setCurrentState(new SuperMario(stateMachine));
stateMachine.setScore(stateMachine.getScore() + 100);
}
@Override
public void obtainCape() {
stateMachine.setCurrentState(new CapeMario(stateMachine));
stateMachine.setScore(stateMachine.getScore() + 200);
}
@Override
public void obtainFireFlower() {
stateMachine.setCurrentState(new FireMario(stateMachine));
stateMachine.setScore(stateMachine.getScore() + 300);
}
@Override
public void meetMonster() {
// do nothing...
}

@Override
public State getName() {
return State.SMALL;
}

}


public class MarioStateMachine {
private int score;

// 使用状态类而不是状态枚举表示当前状态
private IMario currentState;

public MarioStateMachine() {
this.score = 0;
this.currentState = new SmallMario(this);
}

// 调用具体状态类的事件方法
public void obtainMushRoom() {
this.currentState.obtainMushRoom();
}
public void obtainCape() {
this.currentState.obtainCape();
}
public void obtainFireFlower() {
this.currentState.obtainFireFlower();
}
public void meetMonster() {
this.currentState.meetMonster();
}

// getter、setter
public int getScore() {
return this.score;
}
public State getCurrentState() {
return this.currentState.getName();
}
public void setScore(int score) {
this.score = score;
}
public void setCurrentState(IMario currentState) {
this.currentState = currentState;
}

}


public interface IMario {
State getName();

void obtainMushRoom(MarioStateMachine stateMachine);

void obtainCape(MarioStateMachine stateMachine);

void obtainFireFlower(MarioStateMachine stateMachine);

void meetMonster(MarioStateMachine stateMachine);
}

public class SmallMario implements IMario {
private static final SmallMario instance = new SmallMario();

private SmallMario() {
}

public static SmallMario getInstance() {
return instance;
}

@Override
public State getName() {
return State.SMALL;
}

@Override
public void obtainMushRoom(MarioStateMachine stateMachine) {
stateMachine.setCurrentState(SuperMario.getInstance());
stateMachine.setScore(stateMachine.getScore() + 100);
}

@Override
public void obtainCape(MarioStateMachine stateMachine) {
stateMachine.setCurrentState(CapeMario.getInstance());
stateMachine.setScore(stateMachine.getScore() + 200);
}

@Override
public void obtainFireFlower(MarioStateMachine stateMachine) {
stateMachine.setCurrentState(FireMario.getInstance());
stateMachine.setScore(stateMachine.getScore() + 300);
}

@Override
public void meetMonster(MarioStateMachine stateMachine) {
// do nothing...
}
}

public class MarioStateMachine {
private int score;
private IMario currentState;

public MarioStateMachine() {
this.score = 0;
this.currentState = SmallMario.getInstance();
}

public void obtainMushRoom() {
this.currentState.obtainMushRoom(this);
}

public void obtainCape() {
this.currentState.obtainCape(this);
}

public void obtainFireFlower() {
this.currentState.obtainFireFlower(this);
}

public void meetMonster() {
this.currentState.meetMonster(this);
}

public int getScore() {
return this.score;
}

public State getCurrentState() {
return this.currentState.getName();
}

public State getCurrentState() {
return this.currentState.getName();
}

public void setScore(int score) {
this.score = score;
}

public void setCurrentState(IMario currentState) {
this.currentState = currentState;
}
}



《设计模式之美》——极客时间

• 3
点赞
• 2
评论
• 6
收藏
• 一键三连
• 扫一扫，分享海报

04-29 2929

12-25 3753
11-02 984
05-13 999
08-23 2万+
09-18 4万+
06-21 92
06-12 1345
06-13 623
08-28 792
08-14 259
11-12 3366