• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Controlling the no of instancecs of a class

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

Hope the following code helps.


Regards,
Nikhil
[ November 17, 2004: Message edited by: Nikhil Vasaikar ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic