State (Design Pattern)

This behavioral pattern is used to let objects behave differently based on their current state.

class GirlfriendState;

class Girlfriend {
public:
    Girlfriend();
    ~Girlfriend();
    void givePresent();
    void forgetBirthday();
private:
    friend class GirlfriendState;
    GirlfriendState* m_State;
};

class GirlfriendState {
public:
    virtual void givePresent(GirlfriendState* state) {}
    virtual void forgetBirthday(GirlfriendState* state) {}
};
class HappyState : public GirlfriendState {
public:
    void forgetBirthday(GirlfriendState *state);
};
class AngryState : public GirlfriendState {
public:
    void givePresent(GirlfriendState *state);
};

Girlfriend::Girlfriend() { m_State = new HappyState(); }
Girlfriend::~Girlfriend() { delete m_State; }
void Girlfriend::givePresent() {
    m_State->givePresent(this->m_State);
}
void Girlfriend::forgetBirthday() {
    m_State->forgetBirthday(this->m_State);
}

void HappyState::forgetBirthday(GirlfriendState* state) {
    delete state;
    state = new AngryState();
}
void AngryState::givePresent(GirlfriendState* state) {
    delete state;
    state = new HappyState();
}

int main() {
    Girlfriend gf;
    gf.forgetBirthday();
    gf.givePresent();
    // ...
    return 0;
}