Junior — Middle
What is the main purpose of the Singleton pattern in software development?
sobes.tech AI
Answer from AI
The pattern Singleton guarantees that a class has only one instance and provides a global point of access to it. This is useful for managing resources or state centrally, such as a configuration manager, logger, or database connection.
Example in Swift:
class Logger {
static let shared = Logger()
private init() { }
func log(_ message: String) {
print(message)
}
}
// Usage:
Logger.shared.log("Message")