aspose file tools*
The moose likes Beginning Java and the fly likes want to make only three objects of class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "want to make only three objects of class" Watch "want to make only three objects of class" New topic
Author

want to make only three objects of class

atool pune
Greenhorn

Joined: Mar 03, 2008
Posts: 3
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

Joined: Jan 10, 2002
Posts: 61764
    
  67

Sounds like a great job for the Factory pattern.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Praison Selvaraj
Ranch Hand

Joined: Jun 05, 2010
Posts: 49
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.


SCJP, SCWCD, SCEA
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.


GenRocket - Experts at Building Test Data
Praison Selvaraj
Ranch Hand

Joined: Jun 05, 2010
Posts: 49
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
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: want to make only three objects of class