是的,我知道,单身人士模式的问题已经多次讨论过了。我的略有不同,请阅读。
我正在研究状态机。任何实体都可以持有处理相关实体状态的状态机:
template <class T>
class State {
// Default interface for a state
}
class Entity;
class SomeState : public State<Entity> {
// Some implementation dedicated to Entity's type.
// Presumably a Singleton but maybe I'm wrong
}
template <class T>
class StateMachine {
public:
StateMachine(T& entity, SOME_TYPE initial_state);
private:
T& m_entity;
}
class Entity {
public:
// An entity that holds a state machine
Entity(): m_stateMachine(*this, SOME_WAY_TO_GET_"SOME_TYPE") {}
private:
// Its state machine
StateMachine<Entity> m_stateMachine;
}
所以我的问题是: 什么类型应该是“SOME_TYPE”?
那么,任何想法?我该如何设计呢?