How to implement Builder pattern in Kotlin?

后端 未结 13 1596
半阙折子戏
半阙折子戏 2020-11-29 16:18

Hi I am a newbie in the Kotlin world. I like what I see so far and started to think to convert some of our libraries we use in our application from Java to Kotlin.

T

13条回答
  •  情深已故
    2020-11-29 17:19

    For a simple class you don't need a separate builder. You can make use of optional constructor arguments as Kirill Rakhman described.

    If you have more complex class then Kotlin provides a way to create Groovy style Builders/DSL:

    Type-Safe Builders

    Here is an example:

    Github Example - Builder / Assembler

提交回复
热议问题