File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Modified singelton pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Modified singelton pattern" Watch "Modified singelton pattern" New topic
Author

Modified singelton pattern

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

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?


Groovy
Bajji Pat
Ranch Hand

Joined: Apr 05, 2005
Posts: 50
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

Joined: Jun 26, 2002
Posts: 852
Here is an example



http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
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.


Reid - SCJP2 (April 2002)
Reghu Ram Thanumalayan
Ranch Hand

Joined: Oct 21, 2003
Posts: 193


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 ]

Cheers,<br />Reghu Ram T<br /> <br />SCJP 1.4 - 98 %, SCBCD 1.3 - 94 %, SCMAD 1.0 - 92 %
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

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

Joined: Feb 27, 2002
Posts: 8898

Multiton pattern
en.wikipedia.org/wiki/Multiton_pattern
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
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

Joined: Jan 07, 1999
Posts: 6920
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.



Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

Thanks Frank and Rhied.
Scott Ambler
author
Ranch Hand

Joined: Dec 12, 2003
Posts: 608
A business example would be a DayOfTheWeek class which has seven instances (Monday, Tuesday, ...).

- Scott


<a href="http://www-306.ibm.com/software/rational/bios/ambler.html" target="_blank" rel="nofollow">Scott W. Ambler</a><br />Practice Leader Agile Development, IBM Rational<br /> <br />Now available: <a href="http://www.ambysoft.com/books/refactoringDatabases.html" target="_blank" rel="nofollow">Refactoring Databases: Evolutionary Database Design</a>
Greg Parker
Greenhorn

Joined: Aug 13, 2010
Posts: 10
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

Joined: Aug 27, 2011
Posts: 14

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.


'Design Patterns for Java' trainer
 
 
subject: Modified singelton pattern
 
Similar Threads
Constructors in a class
Object Creation in Static Method
Certain Points to ponder
Generic Singleton Class
object creation- new v/s getInstance()