Created
January 18, 2023 14:32
-
-
Save alibahaaa/020c42cb3de821fdb7250b930c88f6c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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