Factory method pattern in Kotlin (Generics)

Implementation of https://www.linkedin.com/pulse/factory-method-pattern-kotlin-v%C3%ADctor-mart%C3%ADn-molina/ using Generics

fun main(args: Array<String>) {
    val ordinaryGame = OrdinaryMazeGame()
    val magicGame = MagicMazeGame()

abstract class Room {
    fun connect(room: Room) {

class MagicRoom : Room()

class OrdinaryRoom : Room()

class MagicMazeGame : MazeGame<MagicRoom>() {
    override fun makeRoom(): MagicRoom = MagicRoom()

class OrdinaryMazeGame : MazeGame<OrdinaryRoom>() {
    override fun makeRoom(): OrdinaryRoom = OrdinaryRoom()

abstract class MazeGame<T: Room> {
    private val rooms = mutableListOf<T>()

    init {
        val room1 = makeRoom()
        val room2 = makeRoom()

    protected abstract fun makeRoom(): T

But these operations are dangerous because your class can be inherited, and a derived class is not yet initialized at this moment. Typical example:

package org.myorg

fun main(args: Array<String>) {

abstract class Base {
    val code = calculate()
    abstract fun calculate(): Int

class Derived(val x: Int) : Base() {
    override fun calculate(): Int {
        println("Derived.calculate: x is $x")

        return x

fun testIt() {
    val d = Derived(42)
    println("testIt: d.code is ${d.code}") // Expected: 42, actual: 0
    println("testIt: d.x is ${d.x}")

Use-Site Variance: adding the following function to the main code:

fun <T: Room> someFun(r1: MutableList<T>, r2: MutableList<out T>) {

you can now mix these subtypes:

fun mix() {
    val r1 = mutableListOf(MagicRoom()) as MutableList<Room>
    val r2 = mutableListOf(MagicRoom())

    someFun(r1, r2)


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s

A %d blogueros les gusta esto: