kotlin 工厂模式demo笔记

编程入门 行业动态 更新时间:2024-10-24 08:20:49

kotlin <a href=https://www.elefans.com/category/jswz/34/1770822.html style=工厂模式demo笔记"/>

kotlin 工厂模式demo笔记

1.单例工厂模式

interface Computer {val cpu: String
}class PC(override val cpu: String = "intel") : Computer
class Mobile(override val cpu: String = "gt") : Computer
enum class ComputerType {PC, Mobile
}object ComputerFactory {operator fun invoke(type: ComputerType): Computer {return when (type) {ComputerType.PC -> PC()ComputerType.Mobile -> Mobile()}}
}fun main() { val p = ComputerFactory(ComputerType.Mobile)println(p.cpu)}

2.伴生对象创建静态工厂方法Demo

interface Computer {val cpu: Stringcompanion object {operator fun invoke(type: ComputerType): Computer {return when (type) {ComputerType.PC -> PC()ComputerType.Mobile -> Mobile()}}}
}class PC(override val cpu: String = "intel") : Computer
class Mobile(override val cpu: String = "gt") : Computer
enum class ComputerType {PC, Mobile
}fun main() {println(Computer(ComputerType.PC).cpu)println(Computer(ComputerType.Mobile).cpu)}

在不指定伴生对象名字时,可通过Computer来调用其伴生对象中的方法

 companion object Factory{operator fun invoke(type: ComputerType): Computer {return when (type) {ComputerType.PC -> PC()ComputerType.Mobile -> Mobile()}}}

两种调用都可以:

    println(Computer(ComputerType.PC).cpu)println(Computer.Factory(ComputerType.Mobile).cpu)

3.扩展伴生对象方法

fun Computer.Factory.fromCpu(cpu: String): ComputerType? = when (cpu) {"intel" -> ComputerType.PC"gt" -> ComputerType.Mobileelse -> null
}

4.抽象工厂模式

interface Computer {val logo: String
}abstract class AbstractFactory {abstract fun produce(): Computercompanion object Factory {operator fun invoke(factory: AbstractFactory): AbstractFactory = factory}
}class Dell(override val logo: String = "DELL") : Computer
class Apple(override val logo: String = "APPLE") : Computerclass DellFactory : AbstractFactory() {override fun produce(): Computer {return Dell()}
}class AppleFactory : AbstractFactory() {override fun produce(): Computer {return Apple()}
}fun main() {val dell = AbstractFactory(DellFactory()).produce()println(dell.logo)
}

5.内联函数来改抽象工厂

/*** 定义一个计算机接口*/
interface Computer {val logo: String
}/*** 抽象工厂*/
abstract class AbstractFactory {abstract fun produce(): Computercompanion object Factory {//内联函数,具名化泛型参数类型inline operator fun <reified T : Computer> invoke(): AbstractFactory =when (T::class) {Dell::class -> DellFactory()Apple::class -> AppleFactory()else -> throw IllegalArgumentException()}}
}/*** 品牌计算机*/
class Dell(override val logo: String = "DELL") : Computer
class Apple(override val logo: String = "APPLE") : Computer/*** 品牌工厂*/
class DellFactory : AbstractFactory() {override fun produce(): Computer {return Dell()}
}class AppleFactory : AbstractFactory() {override fun produce(): Computer {return Apple()}
}fun main() {val dell = AbstractFactory<Apple>().produce()println(dell.logo)
}

代码修改自《Kotlin核心编程》

更多推荐

kotlin 工厂模式demo笔记

本文发布于:2024-03-23 14:36:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1739334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工厂   模式   笔记   kotlin   demo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!