import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test //Based on: http://stackoverflow.com/a/13030163/361832 interface Plant class OrangePlant : Plant class ApplePlant : Plant abstract class PlantFactory { abstract fun makePlant(): Plant companion object { inline fun createFactory(): PlantFactory = when (T::class) { OrangePlant::class -> OrangeFactory() ApplePlant::class -> AppleFactory() else -> throw IllegalArgumentException() } } } class AppleFactory : PlantFactory() { override fun makePlant(): Plant = ApplePlant() } class OrangeFactory : PlantFactory() { override fun makePlant(): Plant = OrangePlant() } class AbstractFactoryTest { @Test fun `Abstract Factory`() { val plantFactory = PlantFactory.createFactory() val plant = plantFactory.makePlant() println("Created plant: $plant") assertThat(plant).isInstanceOf(OrangePlant::class.java) } }