状态模式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef _CDPLAYER_H_
#define _CDPLAYER_H_

#ifdef __cplusplus
extern "C" {
#endif

/* 状态结构内部持有两个函数指针 */
typedef struct _State {
    const struct _State *(*stop)(const struct _State *pThis);   /* 停止 */
    const struct _State *(*playOrPause)(const struct _State \
        *This); /* 播放/暂停 */
} State;

void initialize();
void onStop();
void onPlayOrPause();

// for test
extern int cmdIdx;
extern const char *buf[10];

#ifdef __cplusplus
}
#endif

#endif
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdbool.h>
#include "cdplayer.h"

static const State *pCurrentState;

static const State *ignore(const State *pThis);
static const State *startPlay(const State *pThis);
static const State *stopPlay(const State *pThis);
static const State *pausePlay(const State *pThis);
static const State *resumePlay(const State *pThis);

const State IDLE = {
    ignore,
    startPlay
};

const State PLAY = {
    stopPlay,
    pausePlay
};

const State PAUSE = {
    stopPlay,
    resumePlay
};

int cmdIdx = 0;
const char *buf[10];

void initialize() {
    cmdIdx = 0;
    pCurrentState = &IDLE;
}

void onStop() {
    pCurrentState = pCurrentState->stop(pCurrentState);
}

void onPlayOrPause() {
    pCurrentState = pCurrentState->playOrPause(pCurrentState);
}

static const State *ignore(const State *pThis) {
    return pCurrentState;
}

static const State *stopPlay(const State *pThis) {
    buf[cmdIdx++] = "stop";
    return &IDLE;
}

static const State *pausePlay(const State *pThis) {
    buf[cmdIdx++] = "pause";
    return &PAUSE;
}

static const State *resumePlay(const State *pThis) {
    buf[cmdIdx++] = "resume";
    return &PLAY;
}

static const State *startPlay(const State *pThis) {
    buf[cmdIdx++] = "start";
    return &PLAY;
}