码迷,mamicode.com
首页 > 其他好文 > 详细

建造者模式

时间:2019-08-18 19:31:12      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:参数   his   beans   构建   highlight   创建   view   pac   java   

构建者模式是为了把对象的构建和表现分离

 

当一个对象比较复杂时,我们需要如何做?


 

重载构造方法

public class BuildMode {
    private String param1;

    private String param2;

    private String param3;

    private String param4;

    public BuildMode(){
        new BuildMode(param1,"","","");
    }

    public BuildMode(String param1){
        new BuildMode(param1,"","","");
    }

    public BuildMode(String param1,String param2){
        new BuildMode(param1,param2,"","");
    }

    public BuildMode(String param1,String param2,String param3){
        new BuildMode(param1,param2,param3,"");
    }

    public BuildMode(String param1,String param2,String param3,String param4){
        this.param1 = param1;
        this.param2 = param2;
        this.param3 = param3;
        this.param4 = param4;
    }
}

构造方法重用 ,使用者根据参数来创建对象,代码比较繁琐

  


 

javaBeans方式

public class BuildMode {
    private String param1;

    private String param2;

    private String param3;

    private String param4;

    public String getParam1() {
        return param1;
    }

    public void setParam1(String param1) {
        this.param1 = param1;
    }

    public String getParam2() {
        return param2;
    }

    public void setParam2(String param2) {
        this.param2 = param2;
    }

    public String getParam3() {
        return param3;
    }

    public void setParam3(String param3) {
        this.param3 = param3;
    }

    public String getParam4() {
        return param4;
    }

    public void setParam4(String param4) {
        this.param4 = param4;
    }
}

使用:

BuildMode buildMode = new BuildMode()

buildMode.setParam1("param1");

buildMode.setParam2("param2");

buildMode.setParam3("param3");

buildMode.setParam4("param4");

 

 代码可读行不好。


 

构建者模式

技术图片
package com.yangfei.design;

public class BuildMode {
    private String param1;

    private String param2;

    private String param3;

    private String param4;

    public BuildMode(Build build){
        this.param1 =  build.param1;
        this.param2 =  build.param2;
        this.param3 =  build.param3;
        this.param4 =  build.param4;
    }

    public static final class Build{
        private String param1;

        private String param2;

        private String param3;

        private String param4;

        public Build(String required){
            this.param4 = required;
        }

        public  Build setParam1(String param1) {
            this.param1 = param1;
            return this;
        }

        public  Build setParam2(String param2) {
            this.param2 = param2;
            return this;
        }

        public  Build setParam3(String param3) {
            this.param3 = param3;
            return this;
        }

        public BuildMode build() {
            return new BuildMode(this);
        }

    }

    public static void main(String[] args) {
        BuildMode.Build build = new BuildMode.Build("required");
        //使用建造者模式 可读行更好
        BuildMode buildModeInstance = build.setParam1("param1")
                .setParam2("param2")
                .setParam3("param3").build();
    }
}
View Code

 

当参数越来越多时 推荐使用建造者模式!

建造者模式

标签:参数   his   beans   构建   highlight   创建   view   pac   java   

原文地址:https://www.cnblogs.com/yangfei629/p/11373306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!