美文网首页
Java&Kotlin设计模式-Builder模式

Java&Kotlin设计模式-Builder模式

作者: stark_tony | 来源:发表于2021-04-02 09:58 被阅读0次

    Builder模式在一些复杂实体类构建,或者封装的统一多样化控件中使用方便。

    示例一:实体对象的构建User(Java版本)

        public class User {
        
            public String phone;
            public String area;
        
            public User() {
                this(new Builder());
            }
        
            public User(Builder builder) {
                this.phone = builder.phone;
                this.area = builder.area;
            }
        
            public static final class Builder{
                public String phone;
                public String area;
        
                //函数式编程,不用每个都setXX属性
                public Builder setPhone(String phone) {
                    this.phone = phone;
                    return this;
                }
        
                public Builder setArea(String area) {
                    this.area = area;
                    return this;
                }
        
                public Builder() {
                    //默认初始化方法
                    phone = "";
                    area = "";
                }
        
                Builder(User user) {
                    //回写builder属性
                    phone = user.phone;
                    area = user.area;
                }
        
                public User build() {
                    //创建User对象
                    return new User(this);
                }
        }
    }
    

    示例一:实体对象的构建User(Kotlin版本)

        class User @JvmOverloads constructor(builder: Builder = Builder()) {
            var phone: String
            var area: String
        
            class Builder {
                var phone: String
                var area: String
        
                //函数式编程,不用每个都setXX属性
                fun setPhone(phone: String): Builder {
                    this.phone = phone
                    return this
                }
        
                fun setArea(area: String): Builder {
                    this.area = area
                    return this
                }
        
                constructor() {
                    //默认初始化方法
                    phone = ""
                    area = ""
                }
        
                internal constructor(user: User) {
                    //回写builder属性
                    phone = user.phone
                    area = user.area
                }
        
                fun build(): User {
                    //创建User对象
                    return User(this)
                }
            }
        
            init {
                phone = builder.phone
                area = builder.area
            }
    }
    

    示例一:实体对象的构建

        /**
         * 类型安全的构造器
         */
        class Car(
            val model: String? ,
            val year: Int
        ){
            companion object {
                /**
                 * 带接收者的函数类型,这意味着我们需要向函数传递一个Builder类型的实例
                 */
                inline fun build(block: Builder.() -> Unit) = Builder().apply(block).build()
            }
        
            class Builder{
                var model: String? = null
                var year: Int = -1
        
                fun build() = Car(model ,year)
            }
    }
    

    使用

    val  car = Car.build {
            model = "名字"
            year = 2017
        }
    

    示例二:通用多样化控件的构建

        class Dialog private constructor(
            val title: String,
            val text: String?,
            val onAccept: (() -> Unit)?
        ) {
            class Builder(val title: String) {
                var text: String? = null
                var onAccept: (() -> Unit)? = null
                fun setText(text: String?): Builder {
                    this.text = text
                    return this
                }
                fun setOnAccept(onAccept: (() -> Unit)?): Builder {
                    this.onAccept = onAccept
                    return this
                }
                fun build() = Dialog(title, text, onAccept)
            }
        }
    

    使用

        val dialog1 =Dialog.Builder("Some title")
            .setText("Great dialog")
            .setOnAccept { Log.i("tag" ,"I was clicked") }
            .build()
        val dialog2 = Dialog.Builder("Another dialog")
            .setText("I have no buttons")
            .build()
        val dialog3 = Dialog.Builder("Dialog with just a title").build()
    

    感谢参考文章 Kotlin设计模式-Builder模式

    相关文章

      网友评论

          本文标题:Java&Kotlin设计模式-Builder模式

          本文链接:https://www.haomeiwen.com/subject/cuyfkltx.html