aspose file tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Approach that can be used if only 2 instance of a particular class is needed. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Reply Bookmark "Approach that can be used if only 2 instance of a particular class is needed." Watch "Approach that can be used if only 2 instance of a particular class is needed." New topic
Author

Approach that can be used if only 2 instance of a particular class is needed.

Jyoti Aggarwal
Ranch Hand

Joined: May 26, 2009
Posts: 33
In singleton pattern we have to use just one instance of a particular class. What is the approach that can be used if at any particulay moment just 2 or 3 instance of a particular class need to exist.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2343

You adapt the Singleton class to manage and restrict the number of instances.
The implementation that is used by the Gang of Four checks if one instance exists. If yes, it returns that instance, if no, it creates a new one and returns that.

In your Singleton implementation, you will need to do the bookkeeping to count how many instances are existing, and create a new one if needed/allowed.
But then, which one of the 2 instances are you going to return? The first one? The second one? You will have to define the logic for that.

OCUP UML fundamental
ITIL foundation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Approach that can be used if only 2 instance of a particular class is needed.
 
Similar Threads
instanceof Marcus Mock 3 No. 6
instanceof
A Question from Marcus exam 3 about instanceof
Class.forName() question
question on instanceof operator..