Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Modified singelton pattern

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singelton pattern can not only be used to create a single instance but also create 'n' instances. Can some one think of an example ?
In such a case how will the getInstance method look ?



Which instance out of the 'n' insatnce will the method return ? What logic will be used?
 
Bajji Pat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do we want to acheive here??, is it that you only want n instances of the object to exist??

In that case what is to be done after n instances are created - throw an exception/ the behavior changes to a singleton then on??
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example

 
Reid M. Pinchback
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are looking for an example where singleton is generalized to n instances, where the 'n' are different in some way other than simply memory allocation, I believe the pattern is called a multiton. The flyweight pattern is really a multiton.
 
Reghu Ram Thanumalayan
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This approach only restricts the number of clients which have access to the singleton.

If you need to restrict the number of instances of an object, Flyweight is the pattern to be used (which kind of maintains an object pool from which clients request for objects and return the objects back after use).

I am not sure if we have something called a multiton pattern
[ February 02, 2006: Message edited by: Reghu Ram T ]
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Flyweight pattern is used when there are some properties to be shared among several objects and these properties are factored into a separate object.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Multiton pattern
en.wikipedia.org/wiki/Multiton_pattern
 
Reid M. Pinchback
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In simple examples flyweight and multiton looks virtually identical; the only diff between the Wiki example and the GOF flyweight example is that the flyweights also contain their key. I suppose in a "real world" usage the distinction would be more obvious - flyweights would tend to stay as relatively simple objects, but multitons, like singletons, could have any level of complexity (e.g. using JNDI to manage access to connection factories appears to satisfy multiton).
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've not put a lot of thought into it, but I guess I have always assumed that the main use for a multi-value Singleton is as a "Pool". It's common, for example, to create a "pool" of a small number of threads, database connections, sockets, or whatever. Client code using this pool should not need to know nor care how many pooled objects are available, just that it can ask for one.

At the simplest level such a thing might be useful for some sort of load sharing. Here's an example that gives back a random selection from a pregenerated pool:



If it's important that each instance is only used by one client at a time, this becomes more like a familiar claim/release Pool. Here's one example.

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frank and Rhied.
 
Scott Ambler
author
Ranch Hand
Posts: 608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A business example would be a DayOfTheWeek class which has seven instances (Monday, Tuesday, ...).

- Scott
 
Greg Parker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reid M. Pinchback wrote:In simple examples flyweight and multiton looks virtually identical; the only diff between the Wiki example and the GOF flyweight example is that the flyweights also contain their key. I suppose in a "real world" usage the distinction would be more obvious - flyweights would tend to stay as relatively simple objects, but multitons, like singletons, could have any level of complexity (e.g. using JNDI to manage access to connection factories appears to satisfy multiton).


Both multiton and flyweight uses pooling. There is a significant difference between the two. Multiton does pooling where object creation is resource intensive. Whereas flyweight uses it to manage redundancy of the objects in the system.
 
Thorin Potjes
Greenhorn
Posts: 14
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say a better solution is to create a Factory class that manages the pool of objects you want to restrict, because otherwise your Singleton/Multiton becomes too complex, and violates SRP. A Factory can return an interface instead of a concrete class, thus reducing coupling.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic