• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

want to make only three objects of class

 
atool pune
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you to want make only three objects of class,how will you do that,
the fourth time if it tries to create new object it should return last created instance....
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a great job for the Factory pattern.
 
Praison Selvaraj
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Singleton Pattern can be used here to control the number of instances that are created.

As indicated by the name 'Singleton', it does not necessarily mean that a Single instance is for maintaining a single instance. It just means that this pattern can be used to control the number of instances.

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praison Selvaraj wrote:The Singleton Pattern can be used here to control the number of instances that are created.

As indicated by the name 'Singleton', it does not necessarily mean that a Single instance is for maintaining a single instance. It just means that this pattern can be used to control the number of instances.



You can't just decide that a pattern means what you want it to mean. The singleton pattern is exactly what it says it is. It means you can only have one instance of a given class at any time. Not 2, not 3, but 1.

Bear is right, Factory pattern would be the way to go here, and then look at object pooling techniques.
 
Praison Selvaraj
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear & Gregg, I was of the same opinion as your's until I read Sun Education Services' Java EE Patterns SL-500-EE5 recommended for SCEA preparation.

Page3-17 of this book talks about this GoF Pattern (Singleton) and mentions that

"Singleton can guard several instances at one time, not just one"
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Debate aside -- maybe it could be reworded to "[The techniques used to implement the] Singleton [pattern] can guard several instances at one time, not just one".

Having said that, here's my two cents. Patterns are ... well... patterns. IMHO, as long as people understand what you mean, its fine with me. And when someone says, they used a singleton to restrict something to three items, I can guess what they are talking about.

Henry
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to the original question, the general idea is to make your constructor
private. Then use a static method to manage the number of objects created,
returning them when asked. This is typically a getInstance() method.

Jim ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic