什么是状态模式

状态模式,又称状态对象模式(Pattern of Objects for States),状态模式是对象的行为模式。状态模式允许一个对象在其内部状态改变的时候改变其行为。这个对象看上去就像是改变了它的类一样。

状态模式的结构

  • Context(环境类)

  • State(抽象状态类)

  • ConcreteState(具体状态类)

状态模式的一个实例

有一个游戏,玩家等级分为四个等级,分别是Light、Medium、Hard和Devilish。等级越高权限越多。

实例代码

Level

public abstract class Level {
    public abstract void play();
    public abstract void doubleScore();
    public abstract void changeCards();
    public abstract void peekCards();
}

Light

public class Light extends Level {

    @Override
    public void play() {
        System.out.println("Light play()");
    }

    @Override
    public void doubleScore() {
        System.out.println("Light doubleScore(). Not Support...");
    }

    @Override
    public void changeCards() {
        System.out.println("Light changeCards(). Not Support...");
    }

    @Override
    public void peekCards() {
        System.out.println("Light peekCards(). Not Support...");
    }
}

Medium

public class Medium extends Level {
    @Override
    public void play() {
        System.out.println("Medium play()");
    }

    @Override
    public void doubleScore() {
        System.out.println("Medium doubleScore().");
    }

    @Override
    public void changeCards() {
        System.out.println("Medium changeCards(). Not Support...");
    }

    @Override
    public void peekCards() {
        System.out.println("Medium peekCards(). Not Support...");
    }
}

Hard

public class Hard extends Level {
    @Override
    public void play() {
        System.out.println("Hard play()");
    }

    @Override
    public void doubleScore() {
        System.out.println("Hard doubleScore().");
    }

    @Override
    public void changeCards() {
        System.out.println("Hard changeCards().");
    }

    @Override
    public void peekCards() {
        System.out.println("Hard peekCards(). Not Support...");
    }
}

Devilish

public class Devilish extends Level {
    @Override
    public void play() {
        System.out.println("Devilish play()");
    }

    @Override
    public void doubleScore() {
        System.out.println("Devilish doubleScore().");
    }

    @Override
    public void changeCards() {
        System.out.println("Devilish changeCards().");
    }

    @Override
    public void peekCards() {
        System.out.println("Devilish peekCards().");
    }
}

Player

public class Player {
    private Level level;
    private int score;
    private String name;

    public Player(String name){
        level = new Light();
        score = 0;
        this.name = name;

        System.out.println("Create Player:" + name);
    }

    public void setLevel(Level level) {
        this.level = level;
    }

    public void addScore(int score){
        this.score += score;

        if (this.score < 100){
            setLevel(new Light());
            System.out.println("Current Level:Light. Now Score:" + this.score);
        }else if (this.score < 200){
            setLevel(new Medium());
            System.out.println("Current Level:Medium. Now Score:" + this.score);
        }else if (this.score < 300){
            setLevel(new Hard());
            System.out.println("Current Level:Hard. Now Score:" + this.score);
        }else {
            setLevel(new Devilish());
            System.out.println("Current Level:Devilish. Now Score:" + this.score);
        }
    }

    public void play(){
        level.play();
    }

    public void doubleScore() {
        level.doubleScore();
    }

    public void changeCards() {
        level.changeCards();
    }

    public void peekCards() {
        level.peekCards();
    }
}

Client

public class Client {
    public static void main(String[] args){
        Player player = new Player("Jack");

        player.addScore(50);
        player.play();
        player.doubleScore();

        player.addScore(1000);
        player.peekCards();

        player.addScore(-800);
        player.changeCards();
    }
}

实例结果

Create Player:Jack
Current Level:Light. Now Score:50
Light play()
Light doubleScore(). Not Support...
Current Level:Devilish. Now Score:1050
Devilish peekCards().
Current Level:Hard. Now Score:250
Hard changeCards().

(完)