English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Give me a example to explain this class

2007-01-19 01:51:00 · 8 answers · asked by Anonymous in Computers & Internet Programming & Design

8 answers

Well, Java is easier than C++, so i will explain it in Java :D

01    public class MyClass{
02        private static MyClass instance = null;
03
04        private MyClass(){
05        }
06
07        public static MyClass getInstance(){
08            if ( instance == null ){
09                instance = new MyClass();
10            }
11
12            return instance;
13        }
14    }

It could also be modified to return new instances each time getInstance() is called, but that depends on the behaviour you want it to have anyways.

Even though, there are a very common bug about this pattern, that if two threads call getInstance() at the same time, both of them will see the value of instance as NULL, that means, you will have two instances rather than one. To overcome this bug, you can declare getInstance() to be synchronized, i.e. each call to it must have a synchronization lock, and no two calls will happen at the same time.


01    public class MyClass{
02        private static MyClass instance = null;
03
04        private MyClass(){
05        }
06
07        public synchronized static MyClass getInstance(){
08            if ( instance == null ){
09                instance = new MyClass();
10            }
11
12            return instance;
13        }
14    }

If you are not going to use threads, then don't use the second implementation, it has an overhead of getting a synchronization lock before calling the method.

Finally, to know how to use synchronized methods in C++, see this link: http://msdn2.microsoft.com/en-us/library/34d2s8k3(VS.80).aspx

Thanks.

2007-01-19 03:36:31 · answer #1 · answered by Fox 3 · 1 0

Java Single Instance

2016-12-17 12:23:02 · answer #2 · answered by ? 4 · 0 0

In c++:

class Singleton
{
Singleton()
{
mInstance = this;
}

public static Singleton *GetSingleton()
{
return mInstance;
}

static Singleton *mInstance = null;
}

You can set this up as a template class so you can make any class a singleton that you choose.

2007-01-19 01:57:02 · answer #3 · answered by Pfo 7 · 0 0

in C++

# include
using namespace std;

class singleton
{
//Function prototypes
public:
void single(int var);
//Default constructor
//When you create a new object (or instance) the defualt //constructor (or constuctors, what have you...) will set the //variables to whatever you chose to store in them
singleton();
//Deconstructor
~singleton();
//The deconstructor will activate when the object goes out of //scope, deleting all pointers (you will need to use the delete //operator) and clearing the memory used for the variables within
//the class
//Data members
private:
int one;
int two;
string three;
};

int main
{
//Declare local variables
singleton instance1
//Note that I have created my own instance (or object of the class, with the name instance1)
cout << "This was a sample program, made by me, good luck and I hope it helped!!!!"
return 0;
}

2007-01-19 03:14:59 · answer #4 · answered by D 4 · 0 0

Thread safe java singleton

public class ExampleSingleton {
private static ExampleSingleton instance;

public static ExampleSingleton getInstance() {
if( instance == null ) {
synchronized( ExampleSingleton.class ) {
if ( instance == null ) {
instance = new ExampleSingleton();
}
}
}
return instance;
}
}

2007-01-19 17:00:26 · answer #5 · answered by MilenaMarkova 1 · 0 0

i will reword Pradeesh's answer... The techniques you're calling from the mathematics class are referred to as static techniques. Static techniques do not require an occasion of a class to exist: they are in a position to be referred to as immediately from the class. it is clever in case you have a application which you purely might desire to execute initiatives, yet putting issues up in a static way does not enable variables and such to persist. For that, you elect an merchandise. utilising Pradeesh's occasion, in case you run this: Math.random(); you're working a static approach. Random does not truly need any variables that would desire to alter over the years. There are not any attributes or states that a Math merchandise might decide to maintain song of to execute the random approach, so as that they made it static. that's referred to as without instantiating a Math merchandise. notwithstanding if, in case you elect an merchandise that keeps song of state or makes use of inner variables to alter its habit for a given approach, that merchandise that would desire to decide to be invoked with "new" may be required. i've got additionally suitable some suitable interpreting interior the sources. i will verify lower back in this one now and lower back, so if issues nonetheless are not sparkling, purely edit your question.

2016-10-07 09:47:04 · answer #6 · answered by wiemer 4 · 0 0

//in c++
class Singleton{
private:
   static Singleton * mInstance;
   SingleTon(){ // constructor logic }
public:
  Singleton * getInstance(){
     if( mInstance == NULL )
       mInstance = new Singleton();
     return mInstance;
   }
};

2007-01-19 02:48:14 · answer #7 · answered by Priyananda S 2 · 0 0

class Puma
{
public:
static Puma *getInstance();
private:
static int a;
static Puma *single;
Puma()
{cout << a;}
~Puma();
};
int Puma::a = 10;
Puma *Puma::single = NULL;
Puma *Puma::getInstance()
{
if(!single)
{single = new Puma();
return single;}
}
int main()
{
Puma *p;
p = Puma::getInstance();
return 0;`
}

2015-01-07 21:10:52 · answer #8 · answered by Tarun 1 · 0 0

fedest.com, questions and answers