One: A hungry man type single case:
The static zone initializes the instance and then returns through GetInstance. This method does not have multi-threading problem, is a space to change the time, regardless of the program is not used, will construct a unique instance.
#pragma once
#include <Windows.h>
#include "Lock.h"
Class Singleton
{
Private
Singleton (); Constructors can only be used internally within this class to create unique instances
Singleton (const singleton&); Prevent being copied
singleton& operator= (const singleton&); Prevent assignment
~singleton ();
Static singleton* m_pinstance;
Public
Static singleton* getinstance ()
{
return m_pinstance;
}
};
#include "stdafx.h"
#include "Singleton.h"
Singleton::singleton ()
{
}
Singleton::~singleton ()
{
}
Singleton::singleton (const singleton&)
{}
singleton& singleton::operator= (const singleton&)
{
return *this;
}
singleton* singleton::m_pinstance = new Singleton ();
DesignPattern.cpp: Defines the entry point of the console application.
//
#include "stdafx.h"
#include "Singleton.h"
#include <iostream>
using namespace Std;
int main ()
{
Singleton *aaa = Singleton::getinstance ();
Singleton *bbb = Singleton::getinstance ();
cout << (int*) AAA << Endl;
cout << (int*) BBB << Endl;
}
Two: Lazy type single case:
A unique instance is created when needed, and there may be a multi-threaded problem. Here's how to do it with a double check:
2.1 Ways to use static members of a class
#include <Windows.h>
#include "Lock.h"
Class Singleton
{
Private
Singleton (); Constructors can only be used internally within this class to create unique instances
Singleton (const singleton&); Prevent being copied
singleton& operator= (const singleton&); Prevent assignment
~singleton ();
Static singleton* m_pinstance;
Public
Static singleton* getinstance ()
{
if (m_pinstance = = NULL)
{
Lock (); Implemented in other ways, this is just a little bit of meaning.
if (m_pinstance = = NULL)
{
M_pinstance = new Singleton ();
}
Unlock ();
}
return m_pinstance;
}
};
#include "stdafx.h"
#include "Singleton.h"
Singleton::singleton ()
{
}
Singleton::~singleton ()
{
}
Singleton::singleton (const singleton&)
{}
singleton& singleton::operator= (const singleton&)
{
return *this;
}
singleton* singleton::m_pinstance =null;
DesignPattern.cpp: Defines the entry point of the console application.
//
#include "stdafx.h"
#include "Singleton.h"
#include <iostream>
using namespace Std;
int main ()
{
Singleton *aaa = Singleton::getinstance ();
Singleton *bbb = Singleton::getinstance ();
cout << (int*) AAA << Endl;
cout << (int*) BBB << Endl;
}
2.1 How to use local static variables
#pragma once
#include <Windows.h>
#include "Lock.h"
Class Singleton
{
Private
Singleton (); Constructors can only be used internally within this class to create unique instances
Singleton (const singleton&); Prevent being copied
singleton& operator= (const singleton&); Prevent assignment
~singleton ();
Public
Static singleton* getinstance ()
{
Static Singleton Singleton;
After c++0x, local static variables can be guaranteed to be thread-safe; But before c++0x, they still have to be locked.
Return &singleton;
}
};
#include "stdafx.h"
#include "Singleton.h"
Singleton::singleton ()
{
}
Singleton::~singleton ()
{
}
Singleton::singleton (const singleton&)
{}
singleton& singleton::operator= (const singleton&)
{
return *this;
}
DesignPattern.cpp: Defines the entry point of the console application.
//
#include "stdafx.h"
#include "Singleton.h"
#include <iostream>
using namespace Std;
int main ()
{
Singleton *aaa = Singleton::getinstance ();
Singleton *bbb = Singleton::getinstance ();
cout << (int*) AAA << Endl;
cout << (int*) BBB << Endl;
}
Design Patterns--Singleton mode (c + +)