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.
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
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
Joined: Nov 16, 2004
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 .
Joined: Jan 29, 2003
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.
subject: Controlling the no of instancecs of a class