单件Singleton

//  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;
}
//  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;
}
//  当调用 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;

}
// 《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;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
单例模式(Singleton Pattern)是一种创建型模式,它保证一个类只有一个实例,并提供一个全局访问点。单例模式通常用于控制某些资源的访问权限,或者在整个系统中只需要一个共享资源的情况下使用。 在单例模式中,类的构造函数必须私有化,这样外部就不能直接实例化该类。同时,类中需要定义一个静态方法,用于获取该类唯一的实例。在第一次调用该方法时,会创建一个实例,并将其保存下来。以后每次调用该方法时,都会返回同一个实例。 例如,下面的代码演示了如何在C++中实现单例模式: ```c++ class Singleton { public: static Singleton& getInstance() { static Singleton instance; // 延迟初始化,保证线程安全 return instance; } private: Singleton() {} // 将构造函数私有化,防止外部实例化 Singleton(const Singleton&) = delete; // 禁止拷贝构造函数 Singleton& operator=(const Singleton&) = delete; // 禁止赋值运算符 }; int main() { Singleton& s1 = Singleton::getInstance(); Singleton& s2 = Singleton::getInstance(); std::cout << std::boolalpha << (&s1 == &s2) << std::endl; // 输出:true return 0; } ``` 在这个例子中,我们定义了一个名为Singleton的类,并将其构造函数私有化,防止外部实例化。同时,我们定义了一个静态方法getInstance,用于获取该类唯一的实例。在getInstance方法中,我们使用了静态局部变量的方式来延迟初始化,保证线程安全。最后,在main函数中,我们多次调用getInstance方法,每次调用都会返回同一个实例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yshuise

权术横行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值