建造者模式

Posted yangfei629

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了建造者模式相关的知识,希望对你有一定的参考价值。

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

 

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


 

重载构造方法

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

 

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

以上是关于建造者模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式·建造者模式

设计模式之建造者模式(创建型)

《大话设计模式》——建造者模式

建造者模式(Builder Pattern)

设计模式—建造者模式(Builder)

什么是建造者模式