Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Controlling the no of instancecs of a class

 
Yogender Butola
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want that only 6 instances of my class will be created.
How can I achieve that?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using a Singleton factory class with a counter?
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kalyan Gundavaram
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kalyan,

Hope the following code helps.


Regards,
Nikhil
[ November 17, 2004: Message edited by: Nikhil Vasaikar ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
kalyan Gundavaram
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic