What does it mean?
Singleton is a software design pattern that you can get an instance of it at one time. Singletons are useful when a system needs a resource to control the action of the entire software system. It provides easy access from anywhere on the system.
You can say why we are not using static objects like in Java or global variables? Because Kotlin itself does not contain “static” keyword. Of course, you can use other identifiers that have similar functionality with static but this is a topic for another article. Also :
- Singletons can implement interfaces
- Singletons can be passed as parameters
On the other hand, benefit of singletons against global variables :
- They do not take up space in the global namespace.
Basic usage is like below:
After that, you can call with
Thank you for reading the article. Bye and stay safe!