Definition 1.

Separating the construction of a complex object from its representation allows the same construction process to create different representations.

2. Introduction

The Builder pattern is the creator pattern. The Builder mode is primarily used to create complex objects that the user does not care about the construction process and details. For example: when we want to assemble a computer, we choose the CPU, memory, hard disk and so on, and then hand over to the installer, installer will assemble the computer, we do not need to care about how to assemble it.

3. The implementation

Var person = person.build ().setname ("zxy",person.show()).create() log.e ("zxy",person.show()Copy the code

Create a class

/ * * * Created by zxy on 2019/9/12 - they * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * class * Kotlin builders mode - characters ****************************************** */ class Person private constructor() { private lateinit var name: Private lateinit var job: String // Companion object {fun build(): Class Builder {var mPerson = Person()} class Builder {var mPerson = Person() SetName (name: String): Builder {// setName mperson. name = name return this} fun setPrice(job: String): Builder {// set job mPerson.job = job return this} fun create(): Return mPerson}} fun show():String{Person(name='$name', job=$job)" } }Copy the code