美文网首页安卓面试宝典程序员Android知识
【Builder】Java类设计过程参数太多时怎么办

【Builder】Java类设计过程参数太多时怎么办

作者: itbird01 | 来源:发表于2017-03-23 15:33 被阅读248次

    Java类设计过程中,如果类的构造器或者静态工厂中具有多个参数,并且其中有大量的可选参数时,我们应该怎么办?

    1.Telescoping Constructor模式(重叠构造器)

    我们首先想到的方法肯定是传统的构造器

    /**
     * Created by itbird on 2017/3/23
     */
    
    public class Person {
        private String name;
        private String sex;
        private int year;
    
        public Person(String name, String sex, int year) {
            this.name = name;
            this.sex = sex;
            this.year = year;
        }
    }
    

    但是设计以及使用过程中我们发现以下几点问题:
    (1)属性参数逐渐变多时
    属性参数逐渐变多时,由于要对之前的代码做兼容,所以不可以直接在现有构造器后面追加属性,只能不断新增构造器

    /**
     * Created by itbird on 2017/3/23
     */
    
    public class Person {
        private String name;
        private String sex;
        private int year;
        private String city;
    
        public Person(String name, String sex, int year) {
            this.name = name;
            this.sex = sex;
            this.year = year;
        }
    
        public Person(String name, String sex, int year, String city) {
            this.name = name;
            this.sex = sex;
            this.year = year;
            this.city = city;
        }
    }
    

    (2)属性参数中有大量的可选参数

    /**
     * Created by itbird on 2017/3/23
     */
    
    public class Person {
        private String name;
        private String sex;
        private int year;
        private String city;
        private String state;
        private boolean isFemale;
        private boolean isEmployed;
        private boolean isHomewOwner;
    
        public Person(String name, String sex, int year) {
            this.name = name;
            this.sex = sex;
            this.year = year;
        }
    
        public Person(String name, String sex, int year, String city) {
            this.name = name;
            this.sex = sex;
            this.year = year;
            this.city = city;
        }
    
        public Person(String name, String sex, int year, String city, String newState,
                      boolean newIsFemale, boolean newIsEmployed, boolean newIsHomeOwner) {
            this.name = name;
            this.sex = sex;
            this.year = year;
            this.city = city;
            this.state = newState;
            this.isFemale = newIsFemale;
            this.isEmployed = newIsEmployed;
            this.isHomewOwner = newIsHomeOwner;
        }
    }
    

    显而易见,这样写的类构造器虽然无可厚非,但是当有许多参数的时候,客户端代码会很难编写,并且难以阅读。如果读者想知道那些值是什么意思,必须很仔细的数着这些参数来探个究竟。

    2.JavaBeans模式

    在这种模式下,调用一个无参构造器来创建对象,然后调用setter方法来设置每个必要的参数,以及每个相关的可选参数。

    /**
     * Created by itbird on 2017/3/23
     */
    
    public class Person {
        private String name;
        private String sex;
        private int year;
        private String city;
        private String state;
        private boolean isFemale;
        private boolean isEmployed;
        private boolean isHomewOwner;
    
        public Person() {
        }
        
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public int getYear() {
            return year;
        }
    
        public void setYear(int year) {
            this.year = year;
        }
    
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
        public String getState() {
            return state;
        }
    
        public void setState(String state) {
            this.state = state;
        }
    
        public boolean isFemale() {
            return isFemale;
        }
    
        public void setFemale(boolean female) {
            isFemale = female;
        }
    
        public boolean isEmployed() {
            return isEmployed;
        }
    
        public void setEmployed(boolean employed) {
            isEmployed = employed;
        }
    
        public boolean isHomewOwner() {
            return isHomewOwner;
        }
    
        public void setHomewOwner(boolean homewOwner) {
            isHomewOwner = homewOwner;
        }
    }
    

    这种模式弥补重叠构造器模式的不足。说的明白一点,就是创建实例很容易,这样产生的代码读起来也很容易:

     Person person = new Person();
     person.setCity("重庆");
     person.setYear(12);
     person.setSex("男");
     person.setName("itbird");
    

    遗憾的是,JavaBeans模式自身有着很严重的缺点。因为构造过程被分到几个调用中,在构造过程中JavaBean可能处于非一致的状态。JavaBeans模式阻止了把类做成不可变的可能,这就需要程序员付出额外的努力来确保他的线程安全。

    3.Builder模式

    /**
     * Created by itbird on 2017/3/23
     */
    
    public class Person {
        private String name;
        private String sex;
        private int year;
        private String city;
        private String state;
        private boolean isFemale;
        private boolean isEmployed;
        private boolean isHomewOwner;
    
        public Person() {
        }
    
        public static class PersonBuilder {
            // 必要参数
            private String name;
            // 可选参数
            private String sex;
            private int year;
            private String city;
            private String state;
            private boolean isFemale;
            private boolean isEmployed;
            private boolean isHomewOwner;
    
            public PersonBuilder(String name) {
                this.name = name;
            }
    
            public PersonBuilder setSex(String sex) {
                this.sex = sex;
                return this;
            }
    
            public PersonBuilder setYear(int year) {
                this.year = year;
                return this;
            }
    
            public PersonBuilder setCity(String city) {
                this.city = city;
                return this;
            }
    
            public PersonBuilder setState(String state) {
                this.state = state;
                return this;
            }
    
            public PersonBuilder setFemale(boolean female) {
                isFemale = female;
                return this;
            }
    
            public PersonBuilder setEmployed(boolean employed) {
                isEmployed = employed;
                return this;
            }
    
            public PersonBuilder setHomewOwner(boolean homewOwner) {
                isHomewOwner = homewOwner;
                return this;
            }
    
            public Person build() {
                Person person = new Person();
                person.name = name;
                person.sex = sex;
                person.city = city;
                person.isEmployed = isEmployed;
                person.isFemale = isFemale;
                person.isHomewOwner = isHomewOwner;
                person.state = state;
                person.year = year;
                return person;
            }
        }
    }
    

    调用的实例:

     Person person = new Person.PersonBuilder("itbird")
                        .setCity("重庆").setYear(15).build();
    

    显然,使用Builder模式解决了上诉的难题,达到了“以不变(Builder)应万变(参数)”的目的。

    总结:

    Java类设计过程中,如果类的构造器或者静态工厂中具有多个参数,并且其中有大量的可选参数时,我们应该考虑使用构建器,与传统的重叠构造器模式相比,使用Builder模式的代码更易于阅读和编写,有更好的可扩展性,同时构建器也比JavaBeans更加安全。
    从最后的实例演练中我们知道,在实际开发过程中,我们往往是需要几种模式混合使用,这样才能在保证代码健壮性、可读性的同时,去保证代码的可扩展性以及线程安全性等,这才是代码设计开发过程中真正的“以不变应万变”之道。

    相关文章

      网友评论

        本文标题:【Builder】Java类设计过程参数太多时怎么办

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