Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd first mention that Object Oriented design patterns and their consequences do not always influence every language decision, even in Object Oriented languages. You can certainly find classic design patterns that are easier to implement in one language (Smalltalk) as opposed to another (C++).</p> <p>That being said, I'm not sure I agree with the premise that a singleton instance should only be disposed at the end of an application. Nothing in the design pattern descriptions that I've read for <a href="http://www.dofactory.com/Patterns/PatternSingleton.aspx" rel="noreferrer">Singleton</a> (or <a href="http://rads.stackoverflow.com/amzn/click/0201633612" rel="noreferrer">Design Patterns: Elements of reusable Object-Oriented Software</a>) mention this as a property of this pattern. A singleton should ensure that only one instance of the class exists at any one moment in time; that does not imply that it must exist for as long as the application exists.</p> <p>I have a feeling that in practice, many singletons do exist for most of the life of an application. However, consider an application that uses a TCP connection to communicate with a server, but can also exist in a disconnected mode. When connected, you would want a singleton to maintain the connection information and state of the connection. Once disconnected, you may want to keep that same singleton - or you may dispose of the singleton. While some may argue that it makes more sense to keep the singleton (and I may even be among them), there is nothing in the design pattern itself that precludes you from disposing of it - if a connection is remade, the singleton can be instantiated again, as no instance of it exists at that moment in time.</p> <p>In other words, you can create scenarios where it is logical for singletons to have IDisposable.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COFact is, if you can conceive of a reason for your object to be replaced during the app's lifetime, the object can not legitimately be a singleton. As for why...Say i grabbed the Singleton and passed it to stuff that needed it (cause, ya know, dependency injection is a good thing). Then something somewhere decides to replace it. Now there are *two singletons* (the old one i have been passing around, and the new one). This very possibility violates the entire definition of Singleton -- that is, that code always sees exactly one instance.
      singulars
    2. COSo, first of all, holy old answer Batman. Second, your example doesn't seem to make sense. If you pass an instance of a Singleton to an object and then some other object decides it wants the Singleton, then the pattern allows for that - it should get a reference back to the object that is currently in memory. The point I was making was that the pattern doesn't preclude you from disposing of the object. In that case, using your example, if the object was disposed then the single instance should no longer exist, so when a different object requests the Singleton, it should get a new instance.
      singulars
    3. COIf the object is disposed, the instance *does* still exist -- anyone who'd acquired it before it was disposed would still have it. So it obviously can't be GCed, but it's now unusable. Think about the consequences of that statement in a multithreaded context. An expression like `Singleton.getInstance().doStuff()` is no longer thread safe, *even if both `getInstance` and `doStuff` are*. A thread could come along in between the two calls and dispose the instance we just acquired. And that's even assuming your mythical everybody-always-calls-`getInstance` scenario.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload