• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does singleton allows the flexibility to create more objects if the situation changes.  RSS feed

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read at several places that "The Singleton's purpose is to control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes".
But nobody mentioned anything about how does this pattern provides the flexibility to create more objects if situation demands more than one object.
Can anybody help me?

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A "Singleton" that creates more than one instance is not a Singleton.

Classes like Singletons usually have code to control the creation of instances and code to access those instances. Its failry trivial to change this pattern to create an array of instances.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because both classes have one way of getting instances, getInstance(), all you need to do to change the number of different objects that can be returned is that single method (and the fields required for that method, but these should be private). In case of Multiton, all you need to do to change it into a singleton is change COUNT to 1.
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul and Rob for such a detailed explanation.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!