• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to restrict the object creation to only 5?  RSS feed

 
navya nagaraj
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets have a class
public class example{

// members
//methods

}

How to restrict the object creation to only 5?
For example, we can restrict the object creation to only one by making the class as Singleton.
Is there a way to restrict the creation of an object to only 5 ?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. A Singleton is really just a Factory Method pattern applied to an class, holding one instance of the target class in a memory cache, where the cache size is one. There is no reason why you couldn't follow the same procedure, but have a cache size of 5.

The trick would be in distributing the instances fairly, if that is something you care about.
 
Jam Rei
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
… which appears on the title page for this forum. It does the OP no good at all to be given a complete answer, and we usually delete such posts. I shall leave it, however, because of the serious design errors concealed in it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Navya,

Why?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!