Skip to content

Instantly share code, notes, and snippets.

@alibahaaa
Created January 18, 2023 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alibahaaa/020c42cb3de821fdb7250b930c88f6c0 to your computer and use it in GitHub Desktop.
Save alibahaaa/020c42cb3de821fdb7250b930c88f6c0 to your computer and use it in GitHub Desktop.
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