設計模式之構建者模式(Builder):初步理解


構建者(Builder)設計模式(又叫生成器設計模式):

當一個類的內部數據過於復雜的時候(通常是負責持有數據的類,比如Config、VO、PO、Entity...),要創建的話可能就需要了解這個類的內部結構,還有這些東西是怎么組織裝配等一大坨亂七八糟的東西,這個時候就會增加學習成本而且會很混亂,這個時候就想啊想一種什么法子來管理一下這個類中的數據呢,怎么在創建的時候讓它按部就班的來,並且代碼可讀性很好別讓我看花了眼啊,我要的東西也能都很好設置進來,這就是Builder模式的應用場景,Builder模式可以將一個類的構建和表示進行分離

 

看一個例子:

public class Student {

    private int id;
    private String name;
    private String passwd;
    private String sex;
    private String address;

    // 構造器盡量縮小范圍
    private Student() {
    }

    // 構造器盡量縮小范圍
    private Student(Student origin) {
        // 拷貝一份
         this.id = origin.id;
        this.name = origin.name;
        this.passwd = origin.passwd;
        this.sex = origin.sex;
        this.address = origin.address;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPasswd() {
        return passwd;
    }

    public String getSex() {
        return sex;
    }

    public String getAddress() {
        return address;
    }

    /**
     * Student的創建完全依靠Student.Builder,使用一種方法鏈的方式來創建
     *
     */
    public static class Builder {

        private Student target;

        public Builder() {
            target = new Student();
        }

        public Builder address(int id) {
            target.id = id;
            return this;
        }

        public Builder name(String name) {
            target.name = name;
            return this;
        }

        public Builder password(String passwd) {
            target.passwd = passwd;
            return this;
        }

        public Builder sex(String sex) {
            target.sex = sex;
            return this;
        }

        public Builder address(String address) {
            target.address = address;
            return this;
        }

        public Student build() {
            return new Student(target);
        }
        
    }

}

Student並不是直接new出來的,對其構造器進行了處理使其可訪問范圍盡可能的小,只讓它通過Student.Builder來構建自己,在Student.Builder中提供了一種類set的方法鏈的方式來設置值,然后在最后的build()方法的時候會返回一個Student對象,現在要創建一個Student對象,代碼如下:

Student s=new Student.Builder().name("CC").password("qwerty").sex("男").address("銀河系第二旋臂").build();

再對比一下如果不使用構造者模式(一般情況下的用法):

/**
 * 學生實體
 * @author CC11001100
 *
 */
public class Student {

    private int id;
    private String name;
    private String passwd;
    private String sex;
    private String address;

    public Student() {
    }

    public Student(String name, String passwd, String sex, String address) {
        super();
        this.name = name;
        this.passwd = passwd;
        this.sex = sex;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

創建對象:

Student s=new Student("CC","qwerty","男","銀河系第二旋臂");

對比一下進行一個優劣性分析:

一般的套路:優點是比較簡單,開發效率高,缺點是如果參數真的很多的話鬼知道每個對應的是什么意思啊。

Builder模式:優點是可以將構造器的setter方法名取成類似注釋的方式,這樣我們可以很清晰的知道剛才究竟設置的什么值,可讀性較高,缺點是比較冗長。

 

總結:初步的理解Builder模式解決了要設置的參數過多不好管理的問題(感覺每次構建不同對象是廢話 - -)。

 

從Struts2框架中拿出來的兩個Builder模式的例子(都是Config類):

ActionConfig :

public class ActionConfig extends Located implements Serializable {

    public static final String WILDCARD = "*";

    protected List<InterceptorMapping> interceptors; // a list of interceptorMapping Objects eg. List<InterceptorMapping>
    protected Map<String,String> params;
    protected Map<String, ResultConfig> results;
    protected List<ExceptionMappingConfig> exceptionMappings;
    protected String className;
    protected String methodName;
    protected String packageName;
    protected String name;
    protected Set<String> allowedMethods;

    protected ActionConfig(String packageName, String name, String className) {
        this.packageName = packageName;
        this.name = name;
        this.className = className;
        params = new LinkedHashMap<String, String>();
        results = new LinkedHashMap<String, ResultConfig>();
        interceptors = new ArrayList<InterceptorMapping>();
        exceptionMappings = new ArrayList<ExceptionMappingConfig>();
        allowedMethods = new HashSet<String>();
        allowedMethods.add(WILDCARD);
    }

    /**
     * Clones an ActionConfig, copying data into new maps and lists
     * @param orig The ActionConfig to clone
     * @Since 2.1
     */
    protected ActionConfig(ActionConfig orig) {
        this.name = orig.name;
        this.className = orig.className;
        this.methodName = orig.methodName;
        this.packageName = orig.packageName;
        this.params = new LinkedHashMap<String,String>(orig.params);
        this.interceptors = new ArrayList<InterceptorMapping>(orig.interceptors);
        this.results = new LinkedHashMap<String,ResultConfig>(orig.results);
        this.exceptionMappings = new ArrayList<ExceptionMappingConfig>(orig.exceptionMappings);
        this.allowedMethods = new HashSet<String>(orig.allowedMethods);
    }

    public String getName() {
        return name;
    }

    public String getClassName() {
        return className;
    }

    public List<ExceptionMappingConfig> getExceptionMappings() {
        return exceptionMappings;
    }

    public List<InterceptorMapping> getInterceptors() {
        return interceptors;
    }

    public Set<String> getAllowedMethods() {
        return allowedMethods;
    }

    /**
     * Returns name of the action method
     *
     * @return name of the method to execute
     */
    public String getMethodName() {
        return methodName;
    }

    /**
     * @return Returns the packageName.
     */
    public String getPackageName() {
        return packageName;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public Map<String, ResultConfig> getResults() {
        return results;
    }

    public boolean isAllowedMethod(String method) {
        if (allowedMethods.size() == 1 && WILDCARD.equals(allowedMethods.iterator().next())) {
            return true;
        } else {
            return allowedMethods.contains(method);
        }
    }

    @Override public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (!(o instanceof ActionConfig)) {
            return false;
        }

        final ActionConfig actionConfig = (ActionConfig) o;

        if ((className != null) ? (!className.equals(actionConfig.className)) : (actionConfig.className != null)) {
            return false;
        }

        if ((name != null) ? (!name.equals(actionConfig.name)) : (actionConfig.name != null)) {
            return false;
        }

        if ((interceptors != null) ? (!interceptors.equals(actionConfig.interceptors)) : (actionConfig.interceptors != null))
        {
            return false;
        }

        if ((methodName != null) ? (!methodName.equals(actionConfig.methodName)) : (actionConfig.methodName != null)) {
            return false;
        }

        if ((params != null) ? (!params.equals(actionConfig.params)) : (actionConfig.params != null)) {
            return false;
        }

        if ((results != null) ? (!results.equals(actionConfig.results)) : (actionConfig.results != null)) {
            return false;
        }

        if ((allowedMethods != null) ? (!allowedMethods.equals(actionConfig.allowedMethods)) : (actionConfig.allowedMethods != null)) {
            return false;
        }

        return true;
    }


    @Override public int hashCode() {
        int result;
        result = (interceptors != null ? interceptors.hashCode() : 0);
        result = 31 * result + (params != null ? params.hashCode() : 0);
        result = 31 * result + (results != null ? results.hashCode() : 0);
        result = 31 * result + (exceptionMappings != null ? exceptionMappings.hashCode() : 0);
        result = 31 * result + (className != null ? className.hashCode() : 0);
        result = 31 * result + (methodName != null ? methodName.hashCode() : 0);
        result = 31 * result + (packageName != null ? packageName.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (allowedMethods != null ? allowedMethods.hashCode() : 0);
        return result;
    }

    @Override public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{ActionConfig ");
        sb.append(name).append(" (");
        sb.append(className);
        if (methodName != null) {
            sb.append(".").append(methodName).append("()");
        }
        sb.append(")");
        sb.append(" - ").append(location);
        sb.append("}");
        return sb.toString();
    }

    /**
     * The builder for this object.  An instance of this object is the only way to construct a new instance.  The
     * purpose is to enforce the immutability of the object.  The methods are structured in a way to support chaining.
     * After setting any values you need, call the {@link #build()} method to create the object.
     */
    public static class Builder implements InterceptorListHolder{

        private ActionConfig target;

        public Builder(ActionConfig toClone) {
            target = new ActionConfig(toClone);
        }

        public Builder(String packageName, String name, String className) {
            target = new ActionConfig(packageName, name, className);
        }

        public Builder packageName(String name) {
            target.packageName = name;
            return this;
        }

        public Builder name(String name) {
            target.name = name;
            return this;
        }

        public Builder className(String name) {
            target.className = name;
            return this;
        }

        public Builder defaultClassName(String name) {
            if (StringUtils.isEmpty(target.className)) {
                  target.className = name;
            }
            return this;
        }
        
        public Builder methodName(String method) {
            target.methodName = method;
            return this;
        }

        public Builder addExceptionMapping(ExceptionMappingConfig exceptionMapping) {
            target.exceptionMappings.add(exceptionMapping);
            return this;
        }

        public Builder addExceptionMappings(Collection<? extends ExceptionMappingConfig> mappings) {
            target.exceptionMappings.addAll(mappings);
            return this;
        }

        public Builder exceptionMappings(Collection<? extends ExceptionMappingConfig> mappings) {
            target.exceptionMappings.clear();
            target.exceptionMappings.addAll(mappings);
            return this;
        }

        public Builder addInterceptor(InterceptorMapping interceptor) {
            target.interceptors.add(interceptor);
            return this;
        }

        public Builder addInterceptors(List<InterceptorMapping> interceptors) {
            target.interceptors.addAll(interceptors);
            return this;
        }

        public Builder interceptors(List<InterceptorMapping> interceptors) {
            target.interceptors.clear();
            target.interceptors.addAll(interceptors);
            return this;
        }

        public Builder addParam(String name, String value) {
            target.params.put(name, value);
            return this;
        }

        public Builder addParams(Map<String,String> params) {
            target.params.putAll(params);
            return this;
        }

        public Builder addResultConfig(ResultConfig resultConfig) {
            target.results.put(resultConfig.getName(), resultConfig);
            return this;
        }

        public Builder addResultConfigs(Collection<ResultConfig> configs) {
            for (ResultConfig rc : configs) {
                target.results.put(rc.getName(), rc);
            }
            return this;
        }

        public Builder addResultConfigs(Map<String,ResultConfig> configs) {
            target.results.putAll(configs);
            return this;
        }

        public Builder addAllowedMethod(String methodName) {
            target.allowedMethods.add(methodName);
            return this;
        }

        public Builder addAllowedMethod(Collection<String> methods) {
            target.allowedMethods.addAll(methods);
            return this;
        }

        public Builder location(Location loc) {
            target.location = loc;
            return this;
        }

        public ActionConfig build() {
            target.params = Collections.unmodifiableMap(target.params);
            target.results = Collections.unmodifiableMap(target.results);
            target.interceptors = Collections.unmodifiableList(target.interceptors);
            target.exceptionMappings = Collections.unmodifiableList(target.exceptionMappings);
            target.allowedMethods = Collections.unmodifiableSet(target.allowedMethods);
            ActionConfig result = target;
            target = new ActionConfig(target);
            return result;
        }
    }
}

ExceptionMappingConfig:

public class ExceptionMappingConfig extends Located implements Serializable {

    private String name;
    private String exceptionClassName;
    private String result;
    private Map<String,String> params;


    protected ExceptionMappingConfig(String name, String exceptionClassName, String result) {
        this.name = name;
        this.exceptionClassName = exceptionClassName;
        this.result = result;
        this.params = new LinkedHashMap<String,String>();
    }

    protected ExceptionMappingConfig(ExceptionMappingConfig target) {
        this.name = target.name;
        this.exceptionClassName = target.exceptionClassName;
        this.result = target.result;
        this.params = new LinkedHashMap<String,String>(target.params);
    }

    public String getName() {
        return name;
    }

    public String getExceptionClassName() {
        return exceptionClassName;
    }

    public String getResult() {
        return result;
    }

    public Map<String,String> getParams() {
        return params;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (!(o instanceof ExceptionMappingConfig)) {
            return false;
        }

        final ExceptionMappingConfig exceptionMappingConfig = (ExceptionMappingConfig) o;

        if ((name != null) ? (!name.equals(exceptionMappingConfig.name)) : (exceptionMappingConfig.name != null)) {
            return false;
        }

        if ((exceptionClassName != null) ? (!exceptionClassName.equals(exceptionMappingConfig.exceptionClassName)) : (exceptionMappingConfig.exceptionClassName != null))
        {
            return false;
        }

        if ((result != null) ? (!result.equals(exceptionMappingConfig.result)) : (exceptionMappingConfig.result != null))
        {
            return false;
        }

        if ((params != null) ? (!params.equals(exceptionMappingConfig.params)) : (exceptionMappingConfig.params != null))
        {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int hashCode;
        hashCode = ((name != null) ? name.hashCode() : 0);
        hashCode = (29 * hashCode) + ((exceptionClassName != null) ? exceptionClassName.hashCode() : 0);
        hashCode = (29 * hashCode) + ((result != null) ? result.hashCode() : 0);
        hashCode = (29 * hashCode) + ((params != null) ? params.hashCode() : 0);

        return hashCode;
    }

    /**
     * The builder for this object.  An instance of this object is the only way to construct a new instance.  The
     * purpose is to enforce the immutability of the object.  The methods are structured in a way to support chaining.
     * After setting any values you need, call the {@link #build()} method to create the object.
     */
    public static class Builder{

        private ExceptionMappingConfig target;

        public Builder(ExceptionMappingConfig toClone) {
            target = new ExceptionMappingConfig(toClone);
        }

        public Builder(String name, String exceptionClassName, String result) {
            target = new ExceptionMappingConfig(name, exceptionClassName, result);
        }

        public Builder name(String name) {
            target.name = name;
            return this;
        }

        public Builder exceptionClassName(String name) {
            target.exceptionClassName = name;
            return this;
        }

        public Builder result(String result) {
            target.result = result;
            return this;
        }

        public Builder addParam(String name, String value) {
            target.params.put(name, value);
            return this;
        }

        public Builder addParams(Map<String,String> params) {
            target.params.putAll(params);
            return this;
        }

        public Builder location(Location loc) {
            target.location = loc;
            return this;
        }

        public ExceptionMappingConfig build() {
            target.params = Collections.unmodifiableMap(target.params);
            ExceptionMappingConfig result = target;
            target = new ExceptionMappingConfig(target);
            return result;
        }
    }

}

 

參考資料:

采用Builder模式構造對象

Java方法參數太多怎么辦—Part3—Builder模式


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM