美文网首页
设计模式及在Android中的应用 - Builder模式

设计模式及在Android中的应用 - Builder模式

作者: chengjian666 | 来源:发表于2017-06-18 21:02 被阅读11次
    【CSDN 同步发布:http://blog.csdn.net/riskychengallesgut/article/details/72618400
    1. 了解Builder模式:

    将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
    光看定义并不能理解究竟啥是Builder模式,通过一个具体的实例就会十分清晰。我们顶一个Person类,该Person类包含很多属性,比如name、age、gender、weight等,并且允许这些属性可以不设置,允许为空,则该类的定义如下:

    public class Person {
        private String name;
        private int age;
        private double height;
        private double weight;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public double getHeight() {
            return height;
        }
    
        public void setHeight(double height) {
            this.height = height;
        }
    
        public double getWeight() {
            return weight;
        }
    
        public void setWeight(double weight) {
            this.weight = weight;
        }
    }
    

    然后在使用实例化时,我们为了方便定义若干个构造方法:

    public Person(String name, int age, double height, double weight) {
        this.name = name;
        this.age = age;
        this.height = height;
        this.weight = weight;
    }
    

    或者为了方便创建空对象,还会创建空的构造函数,并且还有可能需要传递部分参数,需要定义若干个差异化的构造函数,比如:

    public Person(String name) {
        this.name = name;
    }
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public Person(String name, int age, double height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }
    

    这样不可避免的坏处有:

    • 1)、可读性差;
    • 2)、构造函数编写繁琐;
    • 3)、调用时容易出错;
    1. Builder模式引入

    然后当我们采用Builder模式时,一切就都迎刃而解,先看代码:

    public class Person {
        private String name;
        private int age;
        private double height;
        private double weight;
    
        privatePerson(Builder builder) {
            this.name=builder.name;
            this.age=builder.age;
            this.height=builder.height;
            this.weight=builder.weight;
        }
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public double getHeight() {
            return height;
        }
    
        public void setHeight(double height) {
            this.height = height;
        }
    
        public double getWeight() {
            return weight;
        }
    
        public void setWeight(double weight) {
            this.weight = weight;
        }
    
        static class Builder{
            private String name;
            private int age;
            private double height;
            private double weight;
            public Builder name(String name){
                this.name=name;
                return this;
            }
            public Builder age(int age){
                this.age=age;
                return this;
            }
            public Builder height(double height){
                this.height=height;
                return this;
            }
    
            public Builder weight(double weight){
                this.weight=weight;
                return this;
            }
    
            public Person build(){
                return new Person(this);
            }
        }
    }
    

    关于以上代码,十分清晰,有如下需要注意的地方:

    • 1)、Person的构造函数定义为私有;
    • 2)、Person类中属性设置返回值为Builder,这样可以实现串联函数的易用性和可读性的优势;
    • 3)、对外创建实例时,通过build返回实例,其参数即为当前builder实例;
    • 4)、某些不被设置的属性,并不会影响实例的创建;
      用法如下:
    Person.Builder builder=new Person.Builder();
    Person person=builder
            .name("张三")
            .age(18)
            .height(178.5)
            .weight(67.4)
            .build();
    
    1. Android中的应用

    在Android中Builder模式有大量的应用,其中最常见的就是AlertDialog的创建:

    AlertDialog.Builder builder=new AlertDialog.Builder(this);
        AlertDialog dialog=builder.setTitle("标题")
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setView(R.layout.myview)
                .setPositiveButton(R.string.positive, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
                    }
                })
                .setNegativeButton(R.string.negative, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
                    }
                })
                .create();
    dialog.show();
    

    并且在各大框架中也有十分广泛的应用,对Builder进行总结:
    λ 定义静态内部类Builder,其内部成员变量与外部类中的一样;
    λ Builder类通过各方法进行成员变量的赋值,并且返回的是当前对象(Builder)本身,用以实现串联函数;
    外部类的构造方法声明为私有,仅对内部类的Builder中的build函数生效。

    相关文章

      网友评论

          本文标题:设计模式及在Android中的应用 - Builder模式

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