Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Modified singelton pattern

 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an example

 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Frank and Rhied.
 
author
Posts: 608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A business example would be a DayOfTheWeek class which has seven instances (Monday, Tuesday, ...).

- Scott
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic