• 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

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

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic