The Singleton pattern can be useful in several cases, and Kotlin makes it easy to declare singletons:
The initialization of an object declaration is thread-safe and done on first access.
object DataProviderManager {
fun registerDataProvider() {}
}
This is called an object declaration, and it always has a name following the object keyword. Just like a variable declaration, an object declaration is not an expression, and it cannot be used on the right-hand side of an assignment statement.
To refer to the object, use its name directly:
DataProviderManager.registerDataProvider(...)
Just like data classes, you can mark an object declaration with the data modifier. This instructs the compiler to generate a number of functions for your object:
toString() returns the name of the data object
equals()/hashCode() pair
No copy() & componentN()function. Because a data object declaration is intended to be used as singleton objects, no copy() function is generated.
Object expressions create objects of anonymous classes, that is, classes that aren't explicitly declared with the class declaration. Such classes are useful for one-time use. You can define them from scratch, inherit from existing classes, or implement interfaces. Instances of anonymous classes are also called anonymous objects because they are defined by an expression, not a name.
Object expressions start with the object keyword.
if you just need an object that doesn't have any nontrivial supertypes, write its members in curly braces after object:
To create an object of an anonymous class that inherits from some type (or types), specify this type after object and a colon (:). Then implement or override the members of this class as if you were inheriting from it:
The code in object expressions can access variables from the enclosing scope:
An object declaration inside a class can be marked with the companion keyword:
class MyClass {
companion object Factory {
fun create(): MyClass = MyClass()
}
}
Members of the companion object can be called simply by using the class name as the qualifier:
val instance = MyClass.create()
Create() method is not static if you check in java. To make it static we have to explicitly write @JvmStatic
There is one important semantic difference between object expressions and object declarations:
Object expressions are executed (and initialized) immediately, where they are used.
Object declarations are initialized lazily, when accessed for the first time.
A companion object is initialized when the corresponding class is loaded (resolved) that matches the semantics of a Java static initializer.
Object Declaration is a single pattern
It is declared like a class but without class name. the name is an object name and by using that name directly we can access the property of object
only one object is created with same name
there is no constructor in object as there is a single object
like class we can inherit the class to object
Object Expression is an annonyms object
follow the same philosophy as object
You can create an object inside the class
Classname.Object name.method() - you can access like this
If you create multipe instance of class then also the inner object's object are creating once
Class can use the properties of companion object as its a companion of the class so with companion:
Classname.method() (no object name required)
if you check this method() in java code then it is not static method
Class has only one companion object
Object which is a singleton in a behaviour is created for social media like button.
Object expression, known as a annonyms class is used like this
we can inherit another class or interface into object anonymous class
Factory pattern.
use of private constructor & companion object