https://kotlinlang.org logo
Title
j

Joel Armstrong

04/11/2018, 5:39 PM
Essentially I just want a public class but with protected constructor (I understand this isn't possible in Kotlin) so that the only way to construct it is through the factory
c

Czar

04/11/2018, 5:49 PM
why would you think that it is impossible?
open class PublicClassProtectedConstructor protected constructor(/*...*/)
in your :java: example you missed that
Factory
extends
PublicClassProtectedConstructor
to be able to see the constructor 🙂
f

fred.deschenes

04/11/2018, 5:56 PM
j

Joel Armstrong

04/11/2018, 6:03 PM
The issue is that I have multiple different constructors within the factory with vastly different parameters. I'm building midi byte sequences; sometimes there are no parameters; sometimes I need a device ID; sometimes it takes a volume and patch number...
f

fred.deschenes

04/11/2018, 6:09 PM
you can still have multiple constructors
did your factory methods just get the arguments and just send them to a protected constructor?
j

Joel Armstrong

04/11/2018, 6:13 PM
The factory methods take the arguments and place them into a sequence of bytes. Often logic is involved
Then that is sent to the constructor
f

fred.deschenes

04/11/2018, 6:18 PM
class PublicClassProtectedConstructor private constructor(private val bytes: ByteArray) {
    companion object Factory {
        fun factory1(deviceId: String): PublicClassProtectedConstructor {
            val bytes = ByteArray(0) // Setup however
            return PublicClassProtectedConstructor(bytes)
        }

        fun factory2(volume: Int, patch: Int): PublicClassProtectedConstructor {
            val bytes = ByteArray(0) // Setup however
            return PublicClassProtectedConstructor(bytes)
        }
    }
}