Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton produce how many object?  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

singleton(private constructor,static stuff) means create only one object for a class?


like example

class NotSingleton{

static NotSingleton obj=null;
private NotSingleton(){
}
static NotSingleton getInstance(){
obj=new NotSingleton();
return obj;
}

}

class Access{

void getInstance(){
for(int i=0;i=100;i++){

NotSingleton objref[i]=NotSingleton.getInstannce();//how many object create

}
}
}

If it produce more than one object .what is a singleton... please explain?
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't a Servlet
Moving to the Java in General (beginner) forum.

Dave
 
Marshal
Posts: 59422
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There has been discussion of Singleton in the last week or two on the intermediate forum, try: this thread, this one, or this third one.
See whether any of them helps you. Any more questions, I am sure people will be happy to help.

CR
 
Campbell Ritchie
Marshal
Posts: 59422
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and there are definitely versions of the Singleton pattern which create two objects.

CR
 
lowercase baba
Bartender
Posts: 12626
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please don't make the same post in multiple forums. it wastes people time who answer one, only to find it's been answered in the other.

i'm having the other thread deleted.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the idea behind the Singlenton pattern is to have one sole instance of a kind object.

Typically it means one sole instance of a class. However, that is not an absolute requirement. The Gang of Four (GoF), authors of Design Patterns, Elements of Reusable Object-Oriented Software talk about the posibility of having a registry of Singlentons within the Singleton class itself.

Based on this principle I would say that the Singlenton is a kind of class that guarantees a limited and predefined existance of instances of a class. Almost ever this limited number is one, but that is not a requirement.

In a Singleton that uses a registry, you call the getInstance method and the Singleton goes over the singlenton registry and checks if the instance you require already exists, if so, returns that instance, if not, a new instance is created.

In this case, the Singlenton is based on the principle of keeping one sole instance for every possible state of the Singleton class.

Still, that should be considered a Singleton, at least according to GoF.
 
What are your superhero powers? Go ahead and try them on this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!