index in the constructor

  • Thread starter Thread starter Dave Johnson
  • Start date Start date
D

Dave Johnson

greetings fellow developers,

how to limit the number of objects that can be build from a class???

is anyone familiar with this problem, i guess that it has somthing to do
with indexing the objects while instantiated, i am i correct ??? or
there is any other way ??

if someone came accross this problem before it would be nice to explain
how he\she solved this issue :) have fun!

Sharing makes All the Difference
 
Make the constructor of the object private so that it can't be instantiated
via new.
Then add a static CreateObject() method that counts the number of objects
created so fat and if exceeded throws an exception.
This way the users of the object must call
BrandNewObject=LimitedObject.CreateObject(); to have an instance and this
way your CreateObject method can control the number of objects at large.

Laura
 
Hello, Dave!

DJ> how to limit the number of objects that can be build from a class???

DJ> is anyone familiar with this problem, i guess that it has somthing to
DJ> do with indexing the objects while instantiated, i am i correct ??? or
DJ> there is any other way ??

DJ> if someone came accross this problem before it would be nice to explain
DJ> how he\she solved this issue :) have fun!

If I correctly understood the question:

One of the solutions here will be object factory, that will track necessary objects count.

Another one will be incrementing class static counter and if limit is exceeded throw exception in the constructor with
appropriate message.


--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Back
Top