您的位置:首页 > Web前端

剑指offer2 - 实现singleton模式

2015-05-06 10:33 381 查看
1. 静态构造函数

静态构造函数用于初始化任何静态数据,或用于执行仅需执行一次的特定操作。在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数。

C#

class SimpleClass
{
// Static constructor
static SimpleClass()
{
//...
}
}


静态构造函数具有以下特点:

静态构造函数既没有访问修饰符,也没有参数。

在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数来初始化

无法直接调用静态构造函数。

在程序中,用户无法控制何时执行静态构造函数。

静态构造函数的典型用途是:当类使用日志文件时,将使用这种构造函数向日志文件中写入项。

静态构造函数在为非托管代码创建包装类时也很有用,此时该构造函数可以调用 LoadLibrary 方法。


示例

在此示例中,类 Bus 有一个静态构造函数和一个静态成员 Drive()。当调用 Drive() 时,将调用静态构造函数来初始化类。

C#

public class Bus
{
// Static constructor:
static Bus()
{
System.Console.WriteLine("The static constructor invoked.");
}

public static void Drive()
{
System.Console.WriteLine("The Drive method invoked.");
}
}

class TestBus
{
static void Main()
{
Bus.Drive();
}
}


  p.29

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class  A
{
public A(string text)
{
Console.WriteLine(text);
}

};
public sealed class B
{
static A a1 = new A("a1");  //1 先初始化
A a2 = new A("a2");   //3

static B()
{
a1 = new A("a3");  //2  再执行函数体
}
public B()
{
a2 = new A("a4");   //4
}
};

class Program
{
static void Main(string[] args)
{
B b = new B();
}

}
}
 

输出结果: a1,a3,a2,a4

2. C# 实现singleton模式

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLib
{
public sealed class Singleton1 // 只适用于单线程
{
private Singleton1()
{ }
private static Singleton1 instance = null; //定义一个静态实例
public static Singleton1 Instance
{
get
{
if (instance == null)
instance = new Singleton1(); //需要的时候创建
return instance;
}
}
}

public sealed class Singleton2 //可用于多线程但效率不高
{
private Singleton2()
{ }
private static Singleton2 instance = null;
private static readonly object obj = new object(); //同步锁
public static Singleton2 Instance
{
get
{
lock (obj)
{
if (instance == null)
instance = new Singleton2();
}
return instance;
}
}
}

public sealed class Singleton3 //同步锁前后两次判断
{
private Singleton3()
{}
private static Singleton3 instance = null;
private static readonly object obj = new object();
public static Singleton3 Instance
{
get
{
if (instance == null) //只有第一次试图创建时加锁
{
lock (obj)
{
if (instance == null)
instance = new Singleton3();
}
}
return instance;
}
}
}

public sealed class Singleton4 //利用静态构造函数
{
private Singleton4()
{}
private static Singleton4 instance = new Singleton4(); //第一次用Singleton4时就被创建
private static Singleton4 Instance
{
get
{
return instance;
}
}
public static void Print()
{ Console.WriteLine("Singleton4 Print");}
}

public sealed class Singleton5 //按需创建实例
{
public Singleton5()
{ }
private static Singleton5 Instance
{
get
{
return Nested.instance; //第一次调用时,自动调用nested的静态构造函数创
} //建实例
}
public static void Print()
{
Console.WriteLine("Singleton5 Print");
}
}
public class Nested
{
static Nested()
{ }
internal static readonly Singleton5 instance = new Singleton5();
}
class Program
{
static void Main(string[] args)
{

Singleton4.Print();
Singleton5.Print();
}
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLib
{
public sealed class Singleton1    // 只适用于单线程
{
private Singleton1()
{
}

private static Singleton1 instance = null;
public static  Singleton1 Instance
{
get
{
if (instance == null)
instance = new Singleton1();
return instance;

}
}
public static void Print()
{
Console.WriteLine("Singleton1 Print");
}

}

public sealed class Singleton2     //可用于多线程但效率不高
{
private Singleton2()
{
}

private static Singleton2 instance = null;
private static readonly object obj = new object();
public static Singleton2 Instance
{
get
{
lock (obj)
{
if (instance == null)
instance = new Singleton2();
}
return instance;

}
}
public static void Print()
{
Console.WriteLine("Singleton2 Print");
}
}

public sealed class Singleton3    //同步锁前后两次判断
{
private Singleton3()
{
}

private static Singleton3 instance = null;

private static readonly object obj = new object();
public static Singleton3 Instance
{
get
{
if (instance == null)
{
lock (obj)
{
if (instance == null)
instance = new Singleton3();
}
}
return instance;
}
}

public static void Print()
{
Console.WriteLine("Singleton3 Print");
}
}

public sealed class Singleton4    //利用静态构造函数
{
private Singleton4()
{
}
private static Singleton4 instance = new Singleton4();

private static Singleton4 Instance
{
get
{
return instance;
}
}
public static void Print()
{
Console.WriteLine("Singleton4 Print");
}
}

public sealed class Singleton5    //按需创建实例
{
public  Singleton5()
{
}
private static Singleton5 Instance
{
get
{
return Nested.instance;
}
}
public static void Print()
{
Console.WriteLine("Singleton5 Print");
}
}

public class Nested
{
static Nested()
{
}
internal static readonly Singleton5 instance = new Singleton5();
}

class Program
{
static void Main(string[] args)
{

Singleton4.Print();
Singleton5.Print();
}
}
}


3.C++  单例模式  或者  http://blog.csdn.net/hackbuteer1/article/details/7460019

     

#include <iostream>
using namespace std;

class Singleton
{
private:
Singleton();//注意:构造方法私有
virtual ~Singleton();
static Singleton *instance;//惟一实例
int var;
public:
static Singleton *GetInstance();//工厂方法(用来获得实例)
int getVar();
void setVar(int);
};
Singleton::Singleton() //构造方法实现
{
this->var=20;
cout<<"Singleton Constructor"<<endl;
}
Singleton::~Singleton()
{
if(instance!=NULL)
{
delete instance;
}
}
Singleton *Singleton::instance=NULL;//初始化静态成员

Singleton *Singleton::GetInstance()
{
if(instance==NULL)
{
instance=new Singleton();
}
return instance;
}
int Singleton::getVar()
{
return(this->var);
}
void Singleton::setVar(int var)
{
this->var=var;
}

int main(int argc,char **argv)
{
Singleton *ton1=Singleton::GetInstance();
Singleton *ton2=Singleton::GetInstance();
cout<<"ton1 var="<<ton1->getVar()<<endl;
ton1->setVar(150);
cout<<"ton2 var="<<ton2->getVar()<<endl;
return 0;
}


1)、构造方法私有 

 那么,就意味着,只能在Singleton的成员函数中,才能调用Singleton的构造函数来创建实例。在Singleton之外,不能创建Singleton对象的实例。

  2)、代码中,定义了GetInstance方法,只能通过GetInstance方法来获取Singleton对象的实例,单例就是在GetInstance方法中控制的。

 首先,Singleton有一个 static Singleton* instance;//惟一实例 

 Singleton* Singleton::instance=NULL; 在这里初始化为NULL。

 Singleton* Singleton::GetInstance() 

 { 

   if(instance == NULL) 

    { instance = new Singleton(); } 

   return instance;

 } 

    上面的函数,就是通过instance来实现单例的。

    当第一次调用GetInstance时,instance 为NULL,所以会执行 instance = new Singleton(); 把这个新建的实例保存到静态成员instance,并返回这个指针。

    第二次到第N次调用GetInstance时,由于instance不为空,所以会直接返回instance 。也就是第一次调用GetInstance创建的那个实例。 所以这样就实现了,单实例。 意思就是说,Singleton对象的实例,只会被创建一次,就是说内存中,只存在一个Singleton的实例,就是所谓,单实例。


一、懒汉模式:即第一次调用该类实例的时候才产生一个新的该类实例,并在以后仅返回此实例。

需要用锁,来保证其线程安全性:原因:多个线程可能进入判断是否已经存在实例的if语句,从而non thread safety.
使用double-check来保证thread safety.但是如果处理大量数据时,该锁才成为严重的性能瓶颈。

1、静态成员实例的懒汉模式:

class Singleton
{
private:
static Singleton* m_instance;
Singleton(){}
public:
static Singleton* getInstance();
};

Singleton* Singleton::getInstance()
{
if(NULL == m_instance)
{
Lock();//借用其它类来实现,如boost
if(NULL == m_instance)
{
m_instance = new Singleton;
}
UnLock();
}
return m_instance;
}


2、内部静态实例的懒汉模式
class SingletonInside
{
private:
SingletonInside(){}
public:
static SingletonInside* getInstance()
{
Lock(); // not needed after C++0x
static SingletonInside instance;
UnLock(); // not needed after C++0x
return instance;
}
};


这里需要注意的是,C++0X以后,要求编译器保证内部静态变量的线程安全性,可以不加锁。但C++ 0X以前,仍需要加锁。

二、饿汉模式:即无论是否调用该类的实例,在程序开始时就会产生一个该类的实例,并在以后仅返回此实例。

由静态初始化实例保证其线程安全性,WHY?因为静态实例初始化在程序开始时进入主函数之前就由主线程以单线程方式完成了初始化,不必担心多线程问题。
故在性能需求较高时,应使用这种模式,避免频繁的锁争夺。

class SingletonStatic
{
private:
static const SingletonStatic* m_instance;
SingletonStatic(){}
public:
static const SingletonStatic* getInstance()
{
return m_instance;
}
};

//外部初始化 before invoke main
const SingletonStatic* SingletonStatic::m_instance = new SingletonStatic;





3.关键字

  1) sealed

  2) abstract

 

    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: