Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
while creating the singelton design pattern, how to restrict to create more than one object.
i have below code.
C#
#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();
}


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.
Posted

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:

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


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.
 
Share this answer
 
Instead of having a flag we could also check whether the static instance is null and then create the singleton instance.
 
Share this answer
 
Hi,
You need to restrict in get_instance() method. Please find the below modified code.
C++
#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>


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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900