Blog Infos
Author
Published
Topics
,
Published
Topics
,
Creational patterns
interface Product {
fun use()
}
view raw Product.kt hosted with ❤ by GitHub
class ConcreteProductA : Product {
override fun use() {
// implementation
}
}
class ConcreteProductB : Product {
override fun use() {
// implementation
}
}
view raw Concrete.kt hosted with ❤ by GitHub
abstract class Creator {
abstract fun factoryMethod(): Product
fun someOperation() {
val product = factoryMethod()
product.use()
}
}
view raw Creator.kt hosted with ❤ by GitHub
class ConcreteCreatorA : Creator() {
override fun factoryMethod(): Product {
return ConcreteProductA()
}
}
class ConcreteCreatorB : Creator() {
override fun factoryMethod(): Product {
return ConcreteProductB()
}
}
interface RetrofitApi {
fun create(): Retrofit
}
class ProductionRetrofitApi : RetrofitApi {
override fun create(): Retrofit {
val retrofit = Retrofit.Builder()
.baseUrl("https://production-api.com")
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit
}
}
class StagingRetrofitApi : RetrofitApi {
override fun create(): Retrofit {
val retrofit = Retrofit.Builder()
.baseUrl("https://staging-api.com")
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit
}
}
class RetrofitApiFactory {
companion object {
fun createRetrofitApi(apiType: String): RetrofitApi? {
return when (apiType) {
"production" -> ProductionRetrofitApi()
"staging" -> StagingRetrofitApi()
else -> null
}
}
}
}
val productionApi = RetrofitApiFactory.createRetrofitApi("production")?.create()
val stagingApi = RetrofitApiFactory.createRetrofitApi("staging")?.create()
interface AbstractFactory {
fun createProductA(): ProductA
fun createProductB(): ProductB
}
class ConcreteProductA1 : ProductA {
override fun use() {
// implementation
}
}
class ConcreteProductA2 : ProductA {
override fun use() {
// implementation
}
}
class ConcreteProductB1 : ProductB {
override fun use() {
// implementation
}
}
class ConcreteProductB2 : ProductB {
override fun use() {
// implementation
}
}

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Kobweb:Creating websites in Kotlin leveraging Compose HTML

Kobweb is a Kotlin web framework that aims to make web development enjoyable by building on top of Compose HTML and drawing inspiration from Jetpack Compose.
Watch Video

Kobweb:Creating websites in Kotlin leveraging Compose HTML

David Herman
Ex-Googler, author of Kobweb

Kobweb:Creating websites in Kotlin leveraging Compose HTML

David Herman
Ex-Googler, author o ...

Kobweb:Creating websites in Kotlin leveraging Compose HTML

David Herman
Ex-Googler, author of Kob ...

Jobs

interface ProductA {
fun use()
}
interface ProductB {
fun use()
}
view raw Product.kt hosted with ❤ by GitHub
class ConcreteFactory1 : AbstractFactory {
override fun createProductA(): ProductA {
return ConcreteProductA1()
}
override fun createProductB(): ProductB {
return ConcreteProductB1()
}
}
class ConcreteFactory2 : AbstractFactory {
override fun createProductA(): ProductA {
return ConcreteProductA2()
}
override fun createProductB(): ProductB {
return ConcreteProductB2()
}
}
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"
view raw Dialog.kt hosted with ❤ by GitHub
class Product {
var partA: String = ""
var partB: String = ""
var partC: String = ""
}
interface Builder {
fun buildPartA(part: String)
fun buildPartB(part: String)
fun buildPartC(part: String)
fun getResult(): Product
}
class ConcreteBuilder : Builder {
private val product = Product()
override fun buildPartA(part: String) {
product.partA = part
}
override fun buildPartB(part: String) {
product.partB = part
}
override fun buildPartC(part: String) {
product.partC = part
}
override fun getResult(): Product {
return product
}
}
class Director {
private lateinit var builder: Builder
fun setBuilder(builder: Builder) {
this.builder = builder
}
fun buildMinimalViableProduct() {
builder.buildPartA("part A")
}
fun buildFullFeaturedProduct() {
builder.buildPartA("part A")
builder.buildPartB("part B")
builder.buildPartC("part C")
}
}
view raw Builder.kt hosted with ❤ by GitHub
class User {
var name: String = ""
var email: String = ""
var age: Int = 0
var address: String = ""
override fun toString(): String {
return "User(name='$name', email='$email', age=$age, address='$address')"
}
}
class UserBuilder {
private var user = User()
fun name(name: String): UserBuilder {
user.name = name
return this
}
fun email(email: String): UserBuilder {
user.email = email
return this
}
fun age(age: Int): UserBuilder {
user.age = age
return this
}
fun address(address: String): UserBuilder {
user.address = address
return this
}
fun build(): User {
return user
}
}
val user = UserBuilder()
.name("John Smith")
.email("john.smith@example.com")
.age(30)
.address("123 Main St")
.build()
println(user)
// Output: User(name='John Smith', email='john.smith@example.com', age=30, address='123 Main St')
view raw User.kt hosted with ❤ by GitHub
interface Prototype {
fun clone(): Prototype
}
view raw Prototype.kt hosted with ❤ by GitHub
class ConcretePrototype : Prototype {
var attribute: String = ""
override fun clone(): Prototype {
val clone = ConcretePrototype()
clone.attribute = this.attribute
return clone
}
}
class Client {
fun operation(prototype: Prototype) {
val prototypeClone = prototype.clone()
// use the clone
}
}
view raw Client.kt hosted with ❤ by GitHub
class Singleton private constructor() {
companion object {
private var instance: Singleton? = null
fun getInstance(): Singleton {
if (instance == null) {
instance = Singleton()
}
return instance!!
}
}
}
view raw Singleton.kt hosted with ❤ by GitHub
class DatabaseHelper private constructor(context: Context) {
companion object {
private var instance: DatabaseHelper? = null
fun getInstance(context: Context): DatabaseHelper {
if (instance == null) {
instance = DatabaseHelper(context)
}
return instance!!
}
}
private val database: SQLiteDatabase = context.openOrCreateDatabase("app_db", Context.MODE_PRIVATE, null)
fun executeQuery(query: String) {
database.execSQL(query)
}
fun getData(query: String): Cursor {
return database.rawQuery(query, null)
}
}
val databaseHelper = DatabaseHelper.getInstance(applicationContext)
val cursor = databaseHelper.getData("SELECT * FROM users")
Happy coding!
Learn more

This article was originally published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
Hi, today I come to you with a quick tip on how to update…
READ MORE
blog
Automation is a key point of Software Testing once it make possible to reproduce…
READ MORE
blog
Drag and Drop reordering in Recyclerview can be achieved with ItemTouchHelper (checkout implementation reference).…
READ MORE
Menu