• 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

Singleton Pattern

 
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interview question I encountered recently. After explaining what a singleton pattern was; the interviewer wanted to know under what condition does one create more than one object with singleton pattern; he went on to state that singleton pattern could be combined with another pattern to create more than one object. Does anyone know the pattern that could be combined with singleton? and under what condition is it used to create more than one pattern? Personal question - if singleton is used in creating more than one object does this not defeat the original purpose which was to ensure creation of just a single object?
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds to me like the interviewer was showing off.
I know depending on the class loader a singleton might not be a singleton. For example if you put a jar in web-inf/lib (i.e. in your war) then every app server I have seen would make that a singleton only for your war context. i.e. if you had 5 wars on the server then you would have 5 singletons. If you put the jar in the server context then you would have one.
I have no idea what pattern he is referring to though.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He was probably talking about the Factory Method pattern (the singleton object would have createThisAndThat() methods for creating a number of objects).
 
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
Also, please bear in mind two things:
  • Singleton is a greatly misunderstood and overused pattern. Search this forum for "singleton" to read loads more.
  • The Singleton pattern as described by the Gang of Four actually specifically allows the instantiation of 1 or any specified constant number of object instances. It's rare to see Singletons which create more than one object in this way, but it is part of the pattern.


  • [ December 30, 2003: Message edited by: Frank Carver ]
     
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi, as Frank explained, Singleton is often misunderstood. He quites rightly makes a reference to GoF which states that the Singleton pattern could conceivably have more than instance. The important point to note is that the number of instances is controlled by the class. For example the singleton class could create up to a maximum number of instances and pool them internally.
     
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The interviewer MIGHT have been referring to "registry of singletons", which is explained in GoF pp.130-132; but this technique is a part of GoF's Singleton pattern, not a a separate pattern.
    It is used when you have [mutliple] subclasses of the singleton class.

    if singleton is used in creating more than one object does this not defeat the original purpose which was to ensure creation of just a single object?


    As Frank stated, the singleton is not necesserily permits only 1 instance, but a set number of intances of a class. In case of registry of singletons, it permits only 1 instance of each subclass. So you have multiple instance of class A, each of them an instance of different subclass of A (MyA extends A, OurA extends A, TheirA extends A).
    But again, this is a part of GoF's singleton pattern, not a separate one.
    [ January 16, 2004: Message edited by: Yuriy Grechukhin ]
    [ January 16, 2004: Message edited by: Yuriy Grechukhin ]
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic