How to Stop the creation of objects(For ex:For Player class of Cricket Team,stop instance creation after 11 players.)

Questions by andrews

Showing Answers 1 - 12 of 12 Answers

Atul Shelke

  • Nov 29th, 2006
 

Can be achieved based on the singleton pattern .

return an instance when asked for from a cache that contains the max number of instances (11 here for this example) and restict the user from getting a instance when the count exceeds the max count (11 in this case).

  Was this answer useful?  Yes

hi_goura

  • Dec 4th, 2006
 

Use Singleton design pattern to prevent more than 11 instances. Use HashMap as the cache to store the instances.You have to always check the current size of the cache to ensure you are creating a limited number of instances.When cache size is already 11, and someone is trying to get an instance, return null or throw some exception.ThanksGourahari

  Was this answer useful?  Yes

u could use patten Multiton (like Singleton, but 4 more than 1 obj).but Y? what if later u want 2 model two teams? Or 3 teams? U have 2 modifiy da Multiton each time!!this logic sould be part of Team, so its addPlayer method throws a TooManyPlayerzException if u add more than 11 players!

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions