• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

want to make only three objects of class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like a great job for the Factory pattern.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ... ...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic