aspose file tools*
The moose likes Java in General and the fly likes Controlling the no of instancecs of a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Controlling the no of instancecs of a class" Watch "Controlling the no of instancecs of a class" New topic
Author

Controlling the no of instancecs of a class

Yogender Butola
Greenhorn

Joined: Jul 19, 2004
Posts: 12
I want that only 6 instances of my class will be created.
How can I achieve that?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

How about using a Singleton factory class with a counter?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
In addition to the factory above, you can make your class a private implementation of a public interface. That way, the only place the class can be constructed is within the factory which itself would return the class object as the interface type.
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Originally posted by Rovas Kram:
In addition to the factory above, you can make your class a private implementation of a public interface. That way, the only place the class can be constructed is within the factory which itself would return the class object as the interface type.


...the alternative being to make the constructer private


I've heard it takes forever to grow a woman from the ground
kalyan Gundavaram
Greenhorn

Joined: Nov 16, 2004
Posts: 2
Hi,
this is kalyan , working on java technology and interested about java . I came accross this post , i didn't understand ur( Rovas Kram ) explanation.

can u clarify it with some coding .



Thanks & Regards,

Kalyan.G
Nikhil Vasaikar
Ranch Hand

Joined: Aug 18, 2004
Posts: 56
Hi Kalyan,

Hope the following code helps.


Regards,
Nikhil
[ November 17, 2004: Message edited by: Nikhil Vasaikar ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Say you want only 6 instances. What happens on the 7th request? You might throw an exception, return one of the 6 at random, return one of the 6 that is currently not in use. On that last choice, what if they are all in use? You might block until one becomes available or throw an exception. Lots of choices. Can you describe your requirements in these terms?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
kalyan Gundavaram
Greenhorn

Joined: Nov 16, 2004
Posts: 2
Hi Nikhil Vasaikar,

Thanks for ur code . i know this approach , but i was strucked how we can handle this sitaution by defining class within interface. i want some code regarding this implementation of class within interface.


Hi Stan James ,

u made me think something more , thanks for ur questionaire. do u have any solutions.i will be thankful, if u share ur answers with me .


Thanks in advance .


bye
Kalyan.G

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Grin, yeah, I had some things in mind. Object pooling is a pretty well-developed area. The BlockingQueue implementations in JDK 5 are great for this. Here's one possible configuration:

Put 6 instances in the queue at system startup. You can do some tricky stuff to make creation private and control the number, or just trust users to never make their own instances.

Clients do this

Read up on the difference between take and poll, add, put and offer. You can choose to wait, throw exception, etc.

If you're not on JDK 5 yet, the Apache Commons ThreadPool has a nice (simple!) blocking queue you can lift.

Have fun!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Controlling the no of instancecs of a class
 
Similar Threads
Date Problem
static variable
About imutable class
sorry what i posted i wrong in jboss
please find this error in jboss please