Junior
What is the Singleton pattern?
sobes.tech AI
Answer from AI
A singleton is a creational design pattern that guarantees that only one instance of a class exists at any given time and provides a global point of access to it. It is used for centralized management of resources or configurations.
Example implementation in Swift:
class Singleton {
static let shared = Singleton() // Static property for the single instance
private init() { // Private initializer prevents creating new instances from outside
// Singleton setup
}
func someMethod() {
// Singleton logic
}
}
// Using singleton
let instance = Singleton.shared
instance.someMethod()