Test05.cpp:Defines the entry point for the console application.
//
#include "stdafx.h"
Design mode 5th Chapter One-piece mode
Class Singleton
{
Private
Static singleton* uniqueinstance;
Private
Singleton () {}
Public
Static synchronized singleton* getinstance ()//synchronized represents thread synchronization in Java, multithreading protection
{
if (uniqueinstance = = NULL)
{
Uniqueinstance = new Singleton ();
}
return uniqueinstance;
}
};
Class Singleton1
{
Private
static singleton1* uniqueinstance = new Singleton1 ();
Private
Singleton1 () {}
Public
Static singleton1* getinstance ()
{
return uniqueinstance;
}
};
Class Singleton2
{
Private
volatile static singleton2* uniqueinstance;
Private
Singleton2 () {}
Public
Volatile static singleton2* getinstance ()
{
if (uniqueinstance = = NULL)
{
Synchronized (Singleton2.class) lock in//java
{
if (uniqueinstance = = NULL)
{
Uniqueinstance = new Singleton2 ();
}
}
}
return uniqueinstance;
}
};
int _tmain (int argc, _tchar* argv[])
{
return 0;
}
Introduction to design patterns, single-piece mode, C + + code implementation