Can I restrict the users of my class(say class A) to create an instance of my class not more than once(only one), and if anybody tries to create more than one instance, all the object refernces should refer to the same (one) instance

Showing Answers 1 - 15 of 15 Answers

Anitha

  • Dec 14th, 2005
 

Use a singleton class

  Was this answer useful?  Yes

Gaurav Sahni

  • Dec 15th, 2005
 

We can use the singleton claass so that we can only make one object of that class

  Was this answer useful?  Yes

Here is an example:

class Singleton
{

static Singleton* _Singleton;
Singleton()
{
}
public:

static Singleton* GetSingleton()
{
 if(!_Singleton)
  return new Singleton;

 return _singleton;
}
}

Singleton* Singleton::_Singleton = NULL;

int main()
{
 Singleton* p = Singleton::Getsingleton();
 Singleton* p = Singleton::Getsingleton();
 Singleton* p = Singleton::Getsingleton();

 return 0;
}

  Was this answer useful?  Yes

SK

  • Jan 12th, 2006
 

Singleton would assure only one instance. To answer original question about restriction of no more than some number of instance you have to have static variable wich you increment when instance is created. So when this variable reach maximum just return NO MORE INSTANCES FOR YOU ;o)

Saju

  • Mar 17th, 2006
 

Use A singleton class like this (in Java)

public class Singleton

{

private static Singleton sInstance= new Singleton();

// NOTE : PRIVATE CONSTRUCTOR !!!

private Singleton()

{

}

public static Singleton getInstance()

{

  return sInstance;

}

  Was this answer useful?  Yes

sarvesh bhatnagar

  • Apr 12th, 2006
 

Preventing direct instantiation
We all know how objects are instantiated right? Maybe not everyone? Let's go through a quick refresher.

Objects are instantiated by using the new keyword. The new keyword allows you to create a new instance of an object, and to specify parameters to the class's constructor. You can specify no parameters, in which case the blank constructor (also known as the default constructor) is invoked. Constructors can have access modifiers, like public and private, which allow you to control which classes have access to a constructor. So to prevent direct instantiation, we create a private default constructor, so that other classes can't create a new instance.

We'll start with the class definition, for a SingletonObject class. Next, we provide a default constructor that is marked as private. No actual code needs to be written, but you're free to add some initialization code if you'd like.

public class SingletonObject
{
 private SingletonObject()
 {
  // no code req'd
 }
}
So far so good. But unless we add some further code, there'll be absolutely no way to use the class. We want to prevent direct instantiation, but we still need to allow a way to get a reference to an instance of the singleton object.

Getting an instance of the singleton
We need to provide an accessor method, that returns an instance of the SingletonObject class but doesn't allow more than one copy to be accessed. We can manually instantiate an object, but we need to keep a reference to the singleton so that subsequent calls to the accessor method can return the singleton (rather than creating a new one). To do this, provide a public static method called getSingletonObject(), and store a copy of the singleton in a private member variable.

public class SingletonObject
{
    private SingletonObject()
    {
        // no code req'd
    }

    public static SingletonObject getSingletonObject()
    {
      if (ref == null)
          // it's ok, we can call this constructor
          ref = new SingletonObject();
      return ref;
    }

    private static SingletonObject ref;
}
So far, so good. When first called, the getSingletonObject() method creates a singleton instance, assigns it to a member variable, and returns the singleton. Subsequent calls will return the same singleton, and all is well with the world. You could extend the functionality of the singleton object by adding new methods, to perform the types of tasks your singleton needs. So the singleton is done, right? Well almost.....

dhanalakshmi

  • May 24th, 2015
 

by making the constructor of the class has private

  Was this answer useful?  Yes

Swapnil

  • Sep 21st, 2017
 

Yes, using singleton pattern

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions