Skip to content

Instantly share code, notes, and snippets.

@alibahaaa
Created January 18, 2023 14:32
Embed
What would you like to do?
interface AlertDialog {
fun show()
}
class MaterialAlertDialog : AlertDialog {
override fun show() {
println("Showing Material Design Alert Dialog")
}
}
class CupertinoAlertDialog : AlertDialog {
override fun show() {
println("Showing Cupertino Alert Dialog")
}
}
interface DialogFactory {
fun createAlertDialog(): AlertDialog
}
class MaterialDialogFactory : DialogFactory {
override fun createAlertDialog(): AlertDialog {
return MaterialAlertDialog()
}
}
class CupertinoDialogFactory : DialogFactory {
override fun createAlertDialog(): AlertDialog {
return CupertinoAlertDialog()
}
}
class DialogFactoryProvider {
companion object {
fun getDialogFactory(factoryType: String): DialogFactory? {
return when (factoryType) {
"material" -> MaterialDialogFactory()
"cupertino" -> CupertinoDialogFactory()
else -> null
}
}
}
}
val materialDialogFactory = DialogFactoryProvider.getDialogFactory("material")
val materialAlertDialog = materialDialogFactory?.createAlertDialog()
materialAlertDialog?.show() // Output: "Showing Material Design Alert Dialog"
val cupertinoDialogFactory = DialogFactoryProvider.getDialogFactory("cupertino")
val cupertinoAlertDialog = cupertinoDialogFactory?.createAlertDialog()
cupertinoAlertDialog?.show() // Output: "Showing Cupertino Alert Dialog"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment