"If the facts don't fit the theory, get new facts" --Albert Einstein
"If the facts don't fit the theory, get new facts" --Albert Einstein
Jesper de Jong wrote:You need to make the getInstance() method synchronized, otherwise two threads calling the method simultaneously might cause two instances to be created.
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Mike Simmons wrote:Yes, since JDK 1.5 - it works as long as you declare the variable as volatile.
Though 1.5 also introduced enums, which give an even easier way to create a singleton:
So double-checked locking doesn't really have much use, still.
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Pat Farrell wrote:Please consider not using a Singleton pattern. They are grossly overused.
All a Singleton is, really, is a global wad of data. It breaks cohesion, increases coupling, and makes unit testing nearly impossible.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet wrote:I don't see how a singleton breaks cohesion.
"If the facts don't fit the theory, get new facts" --Albert Einstein
Hunter McMillen wrote:But from the point of view of the Printer example in the original post, it makes sense to only have one printer regardless of how many other classes exist within your application. The amount of coupling that occurs is bad from a design perspective, but it models the real world in this case.
Wikipedia about Singleton wrote:In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wim Vanni wrote:- Singleton's have the reputation to not being thread-safe although recent Java releases would already counter that
Ehm no. The Gof are Erich Gamma, Richard Helm, Ralph Johnson & John M. Vlissides and is a reference to Design Patterns: Elements of Reusable Object-Oriented Software.Wim Vanni wrote:PS The GoF - Gang of Four - is a reference to the Head First Design Patterns book from O'Reilly. Well worth the read!
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet wrote:
Wim Vanni wrote:- Singleton's have the reputation to not being thread-safe although recent Java releases would already counter that
Could you explain that a bit further?
Wouter Oet wrote:
Ehm no. The Gof are Erich Gamma, Richard Helm, Ralph Johnson & John M. Vlissides and is a reference to Design Patterns: Elements of Reusable Object-Oriented Software.Wim Vanni wrote:PS The GoF - Gang of Four - is a reference to the Head First Design Patterns book from O'Reilly. Well worth the read!
Mike Simmons wrote:Singletons force all threads to use the same instance, so if the instance has mutable fields, it's much more likely that there will be threading problems if proper thread-safe techniques are used.
Mike Simmons wrote:Singletons force all threads to use the same instance, so if the instance has mutable fields, it's much more likely that there will be threading problems unless proper thread-safe techniques are used.
Mike Simmons wrote:Singletons force all threads to use the same instance, so if the instance has mutable fields, it's much more likely that there will be threading problems unless proper thread-safe techniques are used.