1、意图
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
2、适应性
Use the Singleton pattern when
• there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.
• when the sole instance should be extensible by subclassing, and client should be able to use an extended instance without modifying their code.
3、结构
4、示例代码
class Singleton {
public:
static Singleton* Instance();
protected:
Singleton();
private:
static Singleton* _instance;
};
The corresponding implementation isThe corresponding implementation is
Singleton* Singleton::_instance = 0;
Singleton* Singleton::Instance () {
if (_instance == 0) {
_instance = new Singleton;
}
return _instance;
}