Java方法参数过多

2016-04-15  本文已影响3345人  WilsonMing

重构--Java方法参数过多

public void getNews(Context context,
                        Callback callback,
                        String uuid,
                        String uid,
                        String from,
                        String token,
                        String uid,
                        String .....){
                        //逻辑
}
public class Person {
    public String lastName;
    public String firstName;
    public String middleName;
    public String salutation;
    public String suffix;
    public String streetAddress;
    public String city;
    public String state;
    public boolean isFemale;
    public boolean isEmployed;
    public boolean isHomeOwner;
    public Person(String lastName, String firstName, String middleName, String salutation,
                  String suffix, String streetAddress, String city, String state,
                  boolean isFemale, boolean isEmployed, boolean isHomeOwner) {
        this.lastName = lastName;
        this.firstName = firstName;
        this.middleName = middleName;
        this.salutation = salutation;
        this.suffix = suffix;
        this.streetAddress = streetAddress;
        this.city = city;
        this.state = state;
        this.isFemale = isFemale;
        this.isEmployed = isEmployed;
        this.isHomeOwner = isHomeOwner;
    }
}
解决方法
public class Person {
    public FullName fullName;
    public Address address;
    public boolean isFemale;
    public boolean isEmployed;
    public boolean isHomeOwner;
    public Person(FullName fullName, Address address, boolean isFemale, boolean isEmployed, boolean isHomeOwner) {
        this.fullName = fullName;
        this.address = address;
        this.isFemale = isFemale;
        this.isEmployed = isEmployed;
        this.isHomeOwner = isHomeOwner;
    }
}
public class FullName {
    public String lastName;
    public String firstName;
    public String middleName;
    public String salutation;
    public String suffix;
    public FullName(String lastName, String firstName, String middleName, String salutation, String suffix) {
        this.lastName = lastName;
        this.firstName = firstName;
        this.middleName = middleName;
        this.salutation = salutation;
        this.suffix = suffix;
    }
}
 public class Address {
    public String streetAddress;
    public String city;
    public String state;
    public Address(String streetAddress, String city, 
                              String state) {
        this.streetAddress = streetAddress;
        this.city = city;
        this.state = state;
    }
}
public class Person {
    public FullName fullName;
    public Address address;
    public boolean isFemale;
    public boolean isEmployed;
    public boolean isHomeOwner;
    public Person(FullName fullName, Address address, boolean isFemale, boolean isEmployed, boolean isHomeOwner) {
        this.fullName = fullName;
        this.address = address;
        this.isFemale = isFemale;
        this.isEmployed = isEmployed;
        this.isHomeOwner = isHomeOwner;
    }
      public static class Builder {
        private FullName fullName;
        private Address address;
        private boolean isFemale;
        private boolean isEmployed;
        private boolean isHomeOwner;
        /**
         * 如果有必填参数这里可以构造必填构造方法
         */
        public Builder() {
        }
        public Builder setFullName(FullName fullName) {
            this.fullName = fullName;
            return this;
        }
        public Builder setAddress(Address address) {
            this.address = address;
            return this;
        }
        public Builder setFemale(boolean female) {
            isFemale = female;
            return this;
        }
        public Builder setEmployed(boolean employed) {
            isEmployed = employed;
            return this;
        }
        public Builder setHomeOwner(boolean homeOwner) {
            isHomeOwner = homeOwner;
            return this;
        }
        public Person create() {
            return new Person(fullName, address, isFemale, isEmployed, isHomeOwner);
        }
    }
}
public class FullName {
    public String lastName;
    public String firstName;
    public String middleName;
    public String salutation;
    public String suffix;
    public FullName(String lastName, String firstName, String middleName, String salutation, String suffix) {
        this.lastName = lastName;
        this.firstName = firstName;
        this.middleName = middleName;
        this.salutation = salutation;
        this.suffix = suffix;
    }
    public static class Builder {
        private String lastName;
        private String firstName;
        private String middleName;
        private String salutation;
        private String suffix;
        public Builder() {
        }
        public Builder setLastName(String lastName) {
            this.lastName = lastName;
            return this;
        }
        public Builder setFirstName(String firstName) {
            this.firstName = firstName;
            return this;
        }
        public Builder setMiddleName(String middleName) {
            this.middleName = middleName;
            return this;
        }
        public Builder setSalutation(String salutation) {
            this.salutation = salutation;
            return this;
        }
        public Builder setSuffix(String suffix) {
            this.suffix = suffix;
            return this;
        }
        public FullName create() {
            return new FullName(lastName, firstName, middleName, salutation, suffix);
        }
    }
}
public class Address {
    public String streetAddress;
    public String city;
    public String state;
    public Address(String streetAddress, String city, String state) {
        this.streetAddress = streetAddress;
        this.city = city;
        this.state = state;
    }
    public static class Builder {
        private String streetAddress;
        private String city;
        private String state;
        public Builder() {
        }
        public Builder setStreetAddress(String streetAddress) {
            this.streetAddress = streetAddress;
            return this;
        }
        public Builder setCity(String city) {
            this.city = city;
            return this;
        }
        public Builder setState(String state) {
            this.state = state;
            return this;
        }
        public Address create() {
            return new Address(streetAddress, city, state);
        }
    }
}

调用的地方

public static void main(String[] args) {
        FullName fullName = new FullName.Builder().setFirstName("yes")
                .setLastName("no").create();
        Address address = new Address.Builder().setCity("china").setState("12")
                .create();
        Person person = new Person.Builder().setAddress(address)
                .setFullName(fullName).create();
    }
上一篇 下一篇

猜你喜欢

热点阅读