//
SINGLETON.cpp : Defines the entry point for the console application.
// 《设计模式》上的例子。
// 只能创建唯一实例。
#include " stdafx.h "
class Singleton {
public:
static Singleton* Instance();
~Singleton(){ delete _instance;}
protected:
Singleton(){};
private:
static Singleton* _instance;
} ;
Singleton * Singleton::_instance = 0 ;
Singleton * Singleton::Instance()
{
if(_instance == 0)
{
_instance = new Singleton();
}
return _instance;
}
int main( int argc, char * argv[])
{
Singleton::Instance();
return 0;
}
// 《设计模式》上的例子。
// 只能创建唯一实例。
#include " stdafx.h "
class Singleton {
public:
static Singleton* Instance();
~Singleton(){ delete _instance;}
protected:
Singleton(){};
private:
static Singleton* _instance;
} ;
Singleton * Singleton::_instance = 0 ;
Singleton * Singleton::Instance()
{
if(_instance == 0)
{
_instance = new Singleton();
}
return _instance;
}
int main( int argc, char * argv[])
{
Singleton::Instance();
return 0;
}
//
SINGLETON.cpp : Defines the entry point for the console application.
// 《C++编程思想》第二卷上的例子。
// 只能创建唯一实例。
#include " stdafx.h "
#include < iostream >
class Singleton {
int i;
Singleton(int x):i(x){}
void operator=(Singleton& );
Singleton(const Singleton& );
public:
static Singleton& instance()
{
static Singleton s(47);
return s;
}
int getVaule(){return i;}
void setVaule(int x){ i = x;}
} ;
int main( int argc, char * argv[])
{
Singleton& s = Singleton::instance();
std::cout<<s.getVaule()<<std::endl;
Singleton& s1 = Singleton::instance();
s1.setVaule(990);
std::cout<<s.getVaule()<<std::endl;
return 0;
}
// 《C++编程思想》第二卷上的例子。
// 只能创建唯一实例。
#include " stdafx.h "
#include < iostream >
class Singleton {
int i;
Singleton(int x):i(x){}
void operator=(Singleton& );
Singleton(const Singleton& );
public:
static Singleton& instance()
{
static Singleton s(47);
return s;
}
int getVaule(){return i;}
void setVaule(int x){ i = x;}
} ;
int main( int argc, char * argv[])
{
Singleton& s = Singleton::instance();
std::cout<<s.getVaule()<<std::endl;
Singleton& s1 = Singleton::instance();
s1.setVaule(990);
std::cout<<s.getVaule()<<std::endl;
return 0;
}
//
当调用 Singleton2::ref()时,将导致唯一的singleton2对象被创建。
// 在这个对象被创建的过程中,singleton1::ref()被调用,这将导致唯一的
// singleton1对象被创建。
class Singleton1 {
Singleton1(){}
public:
static Singleton1& ret()
{
static Singleton1 single;
return single;
}
} ;
class Singleton2 {
Singleton1& s1;
Singleton2(Singleton1& s):s1(s) {}
public:
static Singleton2& ref(){
static Singleton2 single(Singleton1::ret());
return single;
}
Singleton1& f(){ return s1; }
} ;
int main( int argc, char * argv[])
{
Singleton1& s1 = Singleton2::ref().f();
return 0;
}
// 在这个对象被创建的过程中,singleton1::ref()被调用,这将导致唯一的
// singleton1对象被创建。
class Singleton1 {
Singleton1(){}
public:
static Singleton1& ret()
{
static Singleton1 single;
return single;
}
} ;
class Singleton2 {
Singleton1& s1;
Singleton2(Singleton1& s):s1(s) {}
public:
static Singleton2& ref(){
static Singleton2 single(Singleton1::ret());
return single;
}
Singleton1& f(){ return s1; }
} ;
int main( int argc, char * argv[])
{
Singleton1& s1 = Singleton2::ref().f();
return 0;
}
//
《C++ 编程思想》p387
#include " stdafx.h "
#include < iostream >
using std::cout;
using std::endl;
template < class T >
class Singleton {
Singleton(const Singleton& );
Singleton& operator=(const Singleton& );
protected:
Singleton(){}
virtual ~Singleton(){}
public:
static T& instance()
{
static T theInstance;
return theInstance;
}
} ;
class Myclass : public Singleton < Myclass >
{
int x;
protected:
friend class Singleton<Myclass>;
Myclass(){ x = 0; }
public:
void setVaule(int n) { x = n;}
int getVaule()const {return x;}
} ;
int main( int argc, char * argv[])
{
Myclass& m = Myclass::instance();
cout<<m.getVaule()<<endl;
m.setVaule(7582);
cout<<m.getVaule()<<endl;
return 0;
}
#include " stdafx.h "
#include < iostream >
using std::cout;
using std::endl;
template < class T >
class Singleton {
Singleton(const Singleton& );
Singleton& operator=(const Singleton& );
protected:
Singleton(){}
virtual ~Singleton(){}
public:
static T& instance()
{
static T theInstance;
return theInstance;
}
} ;
class Myclass : public Singleton < Myclass >
{
int x;
protected:
friend class Singleton<Myclass>;
Myclass(){ x = 0; }
public:
void setVaule(int n) { x = n;}
int getVaule()const {return x;}
} ;
int main( int argc, char * argv[])
{
Myclass& m = Myclass::instance();
cout<<m.getVaule()<<endl;
m.setVaule(7582);
cout<<m.getVaule()<<endl;
return 0;
}