Singelton设计模式 [英] Singelton design pattern

查看:80
本文介绍了Singelton设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建singelton设计模式时,如何限制创建多个对象。

我有以下代码。

while creating the singelton design pattern, how to restrict to create more than one object.
i have below code.

#include<iostream.h>
class Singelton
{
    // Private Constructor
    Singelton()
    {
    }

    static Singelton *s_instance_p;

public:
~Singelton()
{


}
    static Singelton* get_instance();
    void method();
    static void DestroyInstance();
};
Singelton* Singelton::get_instance()
{

   return s_instance_p;

}
void Singelton::method()
{
    cout<<"Method function called"<<endl;
}
void Singelton::DestroyInstance()
{
    delete s_instance_p;
    s_instance_p = 0;
}
Singelton* Singelton::s_instance_p = NULL;

void main()
{
Singelton *ssl_ctu_ptr = Singelton::get_instance();
ssl_ctu_ptr->method();
Singelton *ssl_ctu_ptr1 = Singelton::get_instance();
ssl_ctu_ptr1->method();
//delete (ssl_ctu_ptr);//never call delete on the singleton pointer in main()
Singelton::DestroyInstance();
}





这里我创建了两个实例ssl_ctu_ptr,ssl_ctu_ptr1。

两者都工作正常。我无法理解如何限制超过1个实例。

请提供您的解决方案

先谢谢。



here i have created two instances ssl_ctu_ptr , ssl_ctu_ptr1.
Both are working fine. i am unable to understand how to restrict more than 1 instances.
please provide your solution
Thanks in Advance.

推荐答案

我们还可以检查静态实例是否为null,然后创建单例实例。
Instead of having a flag we could also check whether the static instance is null and then create the singleton instance.


首先,你有一个拼写错误......它是 Singleton。



解决方案2是正确的,您应该检查静态实例指针而不是标志。



这些选择都不是线程安全的。如果您有多个线程,则需要使用临界区保护第一个实例创建。



由于所需的语言是C ++,您可以定义静态get_instance()方法中的实例:



Firstly, you have a spelling error... it is "Singleton".

Solution 2 is correct that you should check the static instance pointer and not a flag.

Neither of these choices is threadsafe. If you have more than one thread, you need to protect the first instance creation with a critical section.

Since the stated language desired is C++, you can define the static instance in the get_instance() method:

Singelton* Singelton::get_instance()
{
    static Singelton* pThis;
    if(!pThis)
    {
        pThis = new Singelton();
    }
    return s_instance_p;
}





如果应用程序是多线程的,则if语句需要进行关键部分备份。 />


你不能在java或其他几种语言中使用这种技术。



If the app is multithreaded, the ''if'' statement needs to have critical section backup.

You can''t use this technique in java or several other languages.


你好,

您需要限制get_instance()方法。请找到以下修改过的代码。

Hi,
You need to restrict in get_instance() method. Please find the below modified code.
#include <iostream>
#include <conio.h>
 using namespace std;
 class Singelton
{
    // Private Constructor
    Singelton()
    {
    }
	static bool instanceFlag; //added
    static Singelton *s_instance_p;
 
public:
	~Singelton()
	{
 

	}
    static Singelton* get_instance();
    void method();
    static void DestroyInstance();
};

 bool Singelton::instanceFlag = false;
Singelton* Singelton::s_instance_p = NULL;

Singelton* Singelton::get_instance()
{
 
	if(! instanceFlag)
    {
        s_instance_p = new Singelton();
        instanceFlag = true;
        return s_instance_p;
    }
    else
    {
        return s_instance_p;
    }

}
void Singelton::method()
{
    cout<<"Method function called"<<endl;
}
void Singelton::DestroyInstance()
{
    delete s_instance_p;
    s_instance_p = 0;
}

int main()
{
	Singelton *ssl_ctu_ptr = Singelton::get_instance();
	ssl_ctu_ptr->method();
	
	Singelton *ssl_ctu_ptr1 = Singelton::get_instance();
	ssl_ctu_ptr1->method();
	
	//delete (ssl_ctu_ptr);//never call delete on the singleton pointer in main()
	Singelton::DestroyInstance();

	getch();
	return 0;
}
</conio.h></iostream>





在get_instance()方法中,我们需要检查对象是否已经创建。如果它被创建,那么我们需要返回对象而不是创建新的。



希望这让你理解!



最好的问候

Muthuraja



In get_instance() method, we need to check whether the object is already created or not. if it is created then we need to return the object instead of creating new.

Hope this makes you understand!

Best Regards
Muthuraja


这篇关于Singelton设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆