Singleton pattern is to have only one instance for a class. One of the most important reason to use singleton pattern is to save memory. For example, in a system there should be only one window manager. Singletons is good at providing a centralized management system for internal or external resources.
Description
The class can create no more than one instace. All the programs want to access the instance will use the same reference.
Intent
- No more than one instance should be created globally,
- Provide a global access point.
UML
Implementation
Java
The implementations above in Java should be robust for any situtation. However, synnchronize a function is expensive.
Go
example from stackoverflow
The above code is an implementation of Singleton Pattern in Go. However, since Go is not a class-based OO language, most of the classic OO design patterns are awkward to translate into Go. Therefore, Singleton pattern is less useful for Go compared with Java.