I want to implement "singleton" pattern in Kotlin. I wrote something like this.
class MySingleton {
companion object {
val instance = MySingleton
}
}
But now users can create instances.
val mySingleton = MySingleton()
I want allow only this way.
val instance = MySingleton.instance
How can I ban using a constructor of my class?
if you use an object instead of a class you may have better luck
ie.
object MySingleton {
val thingA = 0
}
Related
I'm new to programming in Kotlin and I've already managed to run into the classic circular dependency issue - I know Kotlin can cope with those but I'd like to know how would I go about changing my design to avoid it. What structures or Kotlin functionality should I use in the following?
import MyClass
interface MyInterface {
fun useMyClass(myInstance: MyClass)
}
import MyInterface
class MyClass(myList: List<MyInterface>) {
val storedList: List<MyInterface> = myList
var myValue: Int = 10
}
I would like MyClass to store multiple objects which implement MyInterface, but I would also like each of those objects to reference the class they have been passed to, i.e. each call of useMyClass would have the signature of useMyClass(this).
For example, I could create a class
class ImplementingMyInterfaceClass(): MyInterface {
override fun useMyClass(myInstance: MyClass) {
myInstance.myValue += 10
}
}
and call it somewhere within MyClass:
ImplementingMyInterfaceClass().useMyClass(this)
Technically I could create another construct in the middle which would be used by MyInterface and inherited/implemented by MyClass, but this just doesn't feel correct. Any suggestions?
Note: In my specific issue, it might be helpful to consider each implementation of MyInterface as a sort of a "modifier" (since it will modify the instance of the class) - MyClass instances should be aware of its modifiers and each modifier should be able to modify that instance.
It's going to largely depend on what the interface has to do, but you could limit its function argument to some interface that MyClass implements:
interface MyInterface {
fun increaseSomeValue(someValueHolder: MySubInterface)
interface MySubInterface {
var myValue: Int
}
}
class MyClass(myList: List<MyInterface>): MyInterface.MySubInterface {
val storedList: List<myInterface> = myList
override var myValue: Int = 10
}
Or your interface can take a property argument:
interface MyInterface {
fun increaseSomeValue(someValue: KMutableProperty<Int>)
}
class MyInterfaceImpl: MyInterface {
override fun increaseSomeValue(someValue: KMutableProperty<Int>) {
someValue.setter.call(someValue.getter.call() + 10)
}
}
// from MyClass:
storedList.first().printSomeValue(::myValue)
In other cases where we don't need to both get and set, it could be cleaner to take a more versatile function argument (lambdas could be passed):
interface MyInterface {
fun printSomeValue(valueProvider: () -> Int)
}
class MyInterfaceImpl: MyInterface {
override fun printSomeValue(valueProvider: () -> Int) {
println(valueProvider())
}
}
// from MyClass:
storedList.first().printSomeValue(::myValue)
// or
storedList.first().printSomeValue { 1..10.random() }
Suppose I have an abstract superclass A. That class has a property abstract val predicate: (ModelClass) -> Boolean.
Let B be a subclass.
I want to be able to do both of the following:
Use the predicate from an instance aInstance.predicate
Also use that predicate elsewhere, without having to create an instance to get that predicate B.predicate
How can I do this.
I don't think this is possible.
There is no such thing as an abstract static method in Kotlin or Java.
Perhaps this will give more insight.
Does your class need to be abstract? Maybe the code below can be useful:
open class MyClass {
companion object myCompanion {
val myStatic = "MyClass"
}
open val myStatic = myCompanion.myStatic
}
class MySubClass : MyClass() {
companion object myCompanionSubClass {
val myStatic = "MySubClass"
}
override var myStatic = myCompanionSubClass.myStatic
}
fun main() {
println(MyClass.myStatic)
val t = MyClass()
println(t.myStatic)
println(MySubClass.myStatic)
val subClass = MySubClass()
println(subClass.myStatic)
}
In this code you can define a static property and use it from the class or any instance. It is also possible to override the property in a subclass and use it in the same way.
I'm defining a Kotlin class with a number of primitive properties:
class Contract (contractEntity : ContractEntity) EntityDao<ContractEntity> {
var id : Long // <- This is a primitive datatype, needs to be initialized
var concept : String //<- This also needs to be initialized or declared abstract
init{
mapFromEntity(contractEntity)
}
override fun mapFromEntity(entity : ContractEntity){
id = entity.id
concept = entity.concept
}
}
Now, I want those properties to be initialized with the function mapFromEntity() but I'm stuck with the init block because those are not initialized. What could be a good way to achieve what I'm trying?
So far, the best solution suggest to remove the function that maps the class and use the constructor parameter.
class Contract (contractEntity : ContractEntity) EntityDao<ContractEntity> {
var id = contractEntity.id
var concept = contractEntity.concept
}
In the following code I would like to set a reference to the class instance so that static functions can return a reference to it:
open class TestRunner {
init {
instance = this
}
companion object {
private lateinit var instance: TestRunner
fun addTestSetups(vararg testSetups: () -> TestSetup): TestRunner {
for (setup in testSetups) {
testsSetups.add(setup)
}
return instance
}
}
}
But setting instance = this is not allowed. How can I return an instance of the class from a function while keeping the class as a singleton?
If I get you right, you want something like this:
abstract class TestRunner {
companion object : TestRunner()
}
This seems to work. Instead of keeping a variable that holds a reference to the class, simply referencing the name of the class is sufficient. However, to return an instance of the class from functions, the return type must be Companion:
open class TestRunner {
companion object {
fun addTestSetups(vararg testSetups: () -> TestSetup): Companion {
for (setup in testSetups) {
testsSetups.add(setup)
}
return TestRunner
}
}
}
This is not a true singleton because you can still create a new instance if you did this:
val testRunner = TestRunner()
However, if you never create an instance but only refer to the functions statically, it does behave like a singleton and the state of any private variables inside the companion object will still be maintained.
Update:
I came across this code on the Android developer site that shows an example of a class that is setup as a singleton:
class StockLiveData(symbol: String) : LiveData<BigDecimal>() {
private val stockManager: StockManager = StockManager(symbol)
private val listener = { price: BigDecimal ->
value = price
}
override fun onActive() {
stockManager.requestPriceUpdates(listener)
}
override fun onInactive() {
stockManager.removeUpdates(listener)
}
companion object {
private lateinit var sInstance: StockLiveData
#MainThread
fun get(symbol: String): StockLiveData {
sInstance = if (::sInstance.isInitialized) sInstance else StockLiveData(symbol)
return sInstance
}
}
}
But it should be pointed out that this example requires functions that need to return an instance to first check if the instance variable is set and if not, create a new instance. I'm not sure what the point of that is since to call the function you already have an instance. So why bother create a new instance? Doesn't seem to make any sense.
object in Kotlin is the singleton, not the class its defined within. A companion object has the extra convenience of allowing you to call it by the name of that outer class. But it otherwise shares no hierarchy with it.
To make your class subclassable, you can't define the functions in the companion object. But you can make the class abstract so it can't be instantiated unless subclassed. Then make your companion object extend the abstract class so it will have all those functions available.
abstract class TestRunner{
open fun addTestSetups(vararg testSetups: () -> TestSetup): TestRunner{
//...
return this
}
companion object: TestRunner()
}
Usage:
TestRunner.addTestSetups(someTestSetup)
Note that your singleton is not an instance of TestRunner. It is a singleton instance of a subclass of TestRunner. But since you define no extra functions and override nothing, it behaves exactly like a TestRunner.
If you want a subclass:
abstract class ExtendedTestRunner: TestRunner() {
fun someOtherFunction() {}
companion object: ExtendedTestRunner()
}
The companions are not being subclassed, but their abstract parents can be.
I want to be able to save a class instance to a public static variable but I can't figure out how to do this in Kotlin.
class Foo {
public static Foo instance;
public Foo() {
instance = this;
}
}
Update: since this answer is getting a decent amount of upvotes, I really wanted to say that you shouldn't do the below, but instead just use object Foo { ... }, like Roman rightly points out in the comment.
Previous answer:
The closest thing to Java's static fields is a companion object. You can find the documentation reference for them here: https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects
Your code in Kotlin would look something like this:
class Foo {
companion object {
lateinit var instance: Foo
}
init {
instance = this
}
}
If you want your fields/methods to be exposed as static to Java callers, you can apply the #JvmStatic annotation:
class Foo {
companion object {
#JvmStatic lateinit var instance: Foo
}
init {
instance = this
}
}
It looks that you want to define a singleton object. It is supported in Kotlin as a first-class concept:
object Foo {
...
}
All the boilerplate code with static field and constructor is taken care by the Kotlin automatically. You don't have to write any of that.
From the Kotlin code you can refer to the instance of this object simply as Foo. From the Java code you can referer to the instance of this object as Foo.INSTANCE, because the Kotlin compiler automatically creates the corresponding static field named INSTANCE.
first you create a simple class then after create a block followed by companion object keyword
for example:
class Test{
companion object{
fun getValue(): String{
return "Test String"
}
}
}
you can call this class function using class name dot function name
for example:
// here you will get the function value
Test.getValue()
You can create a companion object for the class, and if you want the field to be static you can use the annotation #JvmStatic. Companion object have access to private members of the class it is companion for.
See below an example:
class User {
private lateinit var name: String
override fun toString() = name
companion object {
#JvmStatic
val instance by lazy {
User().apply { name = "jtonic" }
}
}
}
class CompanionTest {
#Test
fun `test companion object`() {
User.instance.toString() shouldBe "jtonic"
}
}