Kotlin object singleton: In this tutorial, you will learn about object declarations (singletons) and object expressions with the assistance of examples.
In this article, you will learn-
Object Declarations
Singleton is an objected-oriented example where a class can have just one occasion (object).
For instance, you are working on an application having an SQL database backend. You need to create a connection pool to access the database while reusing a similar connection for all clients. For this, you can create the connection through singleton class so every client get a similar connection.
Kotlin provides a simple method to create singletons using the object declaration highlight. For that, object watchword is used.
object SingletonExample { ... .. ... // body of class ... .. ... }
The above code combines a class declaration and a declaration of a single occasion SingletonExample of the class.
An object declaration can contain properties, strategies, etc. Be that as it may, they are not allowed to have constructors (which makes sense). Why?
Like objects of an ordinary class, you can call strategies and access properties by using the . notation.
Example: Object Declaration
object Test { private var a: Int = 0 var b: Int = 1 fun makeMe12(): Int { a = 12 return a } } fun main(args: Array<String>) { val result: Int result = Test.makeMe12() println("b = ${Test.b}") println("result = $result") }
At the point when you run the program, the output will be:
b = 1 result = 12
Object declaration can acquire from classes and interfaces along these lines like normal classes.
Singletons and Dependency Injection
Object declarations can be helpful at times. Notwithstanding, they are not ideal in enormous software frameworks that cooperate with numerous different pieces of the framework.
Recommended Reading: Dependency Injection & Singleton Design pattern
Kotlin object Expressions
The object of an anonymous inward class is carried out by an object expression, which is used in the parameters of the strategy
The object watchword can likewise be used to create objects of an anonymous class known as anonymous objects. They are used on the off chance that you need to create an object of a slight adjustment of some class or interface without declaring a subclass for it. For instance ,
window.addMouseListener(object : MouseAdapter() { override fun mouseClicked(e: MouseEvent) { // ... } override fun mouseEntered(e: MouseEvent) { // ... } })
(The example is taken from official Kotlin docs page.)
Here, an anonymous object is declared extending MouseAdapter class.The program overrides two MouseAdapter strategies: mouseClicked() and mouseEntered().
On the off chance that fundamental, you can assign a name to the anonymous object and store it in a variable. For instance,
val obj = object : MouseAdapter() { override fun mouseClicked(e: MouseEvent) { // ... } override fun mouseEntered(e: MouseEvent) { // ... } }
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.