单例模式

Java

在Java語言中，單例模式(餓漢模式)應用的例子如下述代碼所示：
  public class Singleton {
    private static final Singleton INSTANCE = new Singleton();
  
    // Private constructor suppresses
    // default public constructor
    private Singleton() {};
 
    public static Singleton getInstance() {
        return INSTANCE;
    }
  }
在Java程式語言中，單例模式(懶漢模式)應用的例子如下述代碼所示 (此種方法只能用在JDK5及以後版本(注意 INSTANCE 被聲明為 volatile)，之前的版本使用「雙重檢查鎖」會發生非預期行為)：
  public class Singleton {
    private static volatile Singleton INSTANCE = null;
  
    // Private constructor suppresses 
    // default public constructor
    private Singleton() {};
  
    //Thread safe and performance  promote 
    public static  Singleton getInstance() {
        if(INSTANCE == null){
             synchronized(Singleton.class){
                 // When more than two threads run into the first null check same time, 
                 // to avoid instanced more than one time, it needs to be checked again.
                 if(INSTANCE == null){ 
                     INSTANCE = new Singleton();
                  }
              } 
        }
        return INSTANCE;
    }
  }

C++

在C++程式語言中，單例模式應用的例子如下述代碼所示（這裡僅僅提供一個示例；由於加入了鎖，這代碼是執行緒安全的）：
// ...class lock{
    public:
        lock();
        lock(lock const & l);
        ~lock();
        lock & operator =(lock const & l);
        void request();
        void release();
    // ...};lock::lock(){
    // ...}// ...lock::~lock(){
    // ...}// ...void lock::request(){
    // ...}void lock::release(){
    // ...}// ...// assumes _DATA_TYPE_ has a default constructortemplate<typename _DATA_TYPE_>class singleton{
    public:
        static _DATA_TYPE_ * request();
        static void release();

    private:
        singleton();
        singleton(singleton<_DATA_TYPE_> const & s);
        ~singleton();
        singleton<_DATA_TYPE_> & operator =(singleton<_DATA_TYPE_> const & s);
        static _DATA_TYPE_ * pointer;
        static lock mutex;
    // ...};template<typename _DATA_TYPE_>_DATA_TYPE_ * singleton<_DATA_TYPE_>::pointer = nullptr;template<typename _DATA_TYPE_>lock singleton<_DATA_TYPE_>::mutex;template<typename _DATA_TYPE_>_DATA_TYPE_ * singleton<_DATA_TYPE_>::request(){
    if(singleton<_DATA_TYPE_>::pointer == nullptr)
    {
        singleton<_DATA_TYPE_>::mutex.request();

        if(singleton<_DATA_TYPE_>::pointer == nullptr)
        {
            singleton<_DATA_TYPE_>::pointer = new _DATA_TYPE_;
        }

        singleton<_DATA_TYPE_>::mutex.release();
    }

    return singleton<_DATA_TYPE_>::pointer;}template<typename _DATA_TYPE_>void singleton<_DATA_TYPE_>::release(){
    if(singleton<_DATA_TYPE_>::pointer != nullptr)
    {
        singleton<_DATA_TYPE_>::mutex.request();

        if(singleton<_DATA_TYPE_>::pointer != nullptr)
        {
            delete singleton<_DATA_TYPE_>::pointer;
 
            singleton<_DATA_TYPE_>::pointer = nullptr;
        }

        singleton<_DATA_TYPE_>::mutex.release();
    }}template<typename _DATA_TYPE_>singleton<_DATA_TYPE_>::singleton(){
    // ...}// ...int main(){
    int * s;

    s = singleton<int>::request();

    // ...

    singleton<int>::release();

    return 0;}

GO

GO語言中的單例模式:
var _instance *servicevar once sync.Oncetype service struct {
Name string}func Service() *service {
once.Do(func() {
           _instance = &service{}
        })
return _instance}func (this *service) Startup() {}func (this *service) Stop() {}func (this *service) Restart() {}

Objective-C

OC語言中的單例模式:
@interface Control : NSObject+(id)getShare;@endstatic Control *mControl;@implementation Control+(id)getShare{
    if (mControl == nil) {
        mControl = [[Control alloc] init];
    }
    return mControl;}-(id)init {//你需要初始化的属性。。。}@end

PHP

PHP語言中的單例模式:
<?phpclass singleton{
    private function __construct()
    {
    }
    
    static public function instance()
    {
        static $obj = null;
        if($obj === null)
        {
            $obj = new self();
        }
        return $obj;
    }}?>