Design Patterns--Singleton mode (c + +)

Source: Internet
Author: User

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 + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.