java的Builder原理和如何实现详解

首先给一个简单的Builder设计模式的例子: 主实现类代码如下: *** 实体类 包含一个静态内部类 Builder* public class Compa

首先给一个简单的Builder设计模式的例子:

主实现类代码如下:

/**
 * 实体类 包含一个静态内部类 Builder
 */
public class CompanyClient {
    public String companyName;
    // 用final修饰的成员变量表示常量,只能被赋值一次,赋值后值无法改变! final修饰的变量有三种:静态变量、实例变量和局部变量。
    public String companyAddress;
  
    public double companyRegfunds;
    public String mPerson;
    public String mType;     
     //构造方法 
     public CompanyClient() {
         this(new Builder());
     }
     //构造方法
     public CompanyClient(Builder builder){
         this.companyName = builder.companyName;
         this.companyAddress = builder.companyAddress;
         this.companyRegfunds = builder.companyRegfunds;
         this.mPerson = builder.person;
         this.mType = builder.type;
     }
     public String getCompanyName() {
         return companyName;
     }
  
     public String getCompanyAddress() {
         return companyAddress;
     }
  
     public double getCompanyRegfunds() {
         return companyRegfunds;
     }
  
     public String getmPerson() {
         return mPerson;
     }
  
     public String getmType() {
         return mType;
     }
         
     public Builder newBuilder() {
         return new Builder(this);
     }
  
     @Override //重写toString方法后,当print这个对象的时候,会默认的调用toString()这个方法。
     public String toString() {
         return "CompanyClient{" +
                 "companyName='" + companyName + '\'' +
                 ", companyAddress='" + companyAddress + '\'' +
                 ", companyRegfunds=" + companyRegfunds +"千万"+
                 ", mPerson=" + mPerson +
                 ", mType='" + mType + '\'' +
                 '}';
     }
     /**
     *静态内部类 Builder
     */
     public static class Builder{
        public String companyName;
        public String companyAddress;
        public  double companyRegfunds;
        public  String person;
        public String type;
        //构造方法
        public Builder() {
            companyName = companyName;
            companyAddress = companyAddress;
            companyRegfunds = companyRegfunds;
            person = person;
            type = type;
  
        }
        //构造方法
        Builder(CompanyClient companyClient){
            this.companyName = companyClient.companyName;
            this.companyAddress = companyClient.companyAddress;
            this.companyRegfunds = companyClient.companyRegfunds;
            this.person = companyClient.mPerson;
            this.type = companyClient.mType;
        }
  
        public Builder setCompanyName(String name) {
            companyName = name;
            return this;
        }
  
        public Builder setCompanyAddress(String address) {
            companyAddress = address;
            return this;
        }
  
        public Builder setCompanyRegfunds(double regfunds) {
            companyRegfunds = regfunds;
            return this;
        }
  
        public Builder setmPerson(String per) {
            person = per;
            return this;
        }
  
        public Builder setmType(String typeStr) {
            type = typeStr;
            return this;
        }
         //构建一个实体
         public CompanyClient build() {
             return new CompanyClient(this);
         }
    }
 }

测试类实现代码如下:

public class TestBuilder {
    public static void main(String[] args) {
 
        CompanyClient client = new CompanyClient.Builder()
                .setCompanyName("alibaba")
                .setCompanyAddress("wangjing")
                .setCompanyRegfunds(5)
                .setmPerson("10000")
                .build();
        System.out.println(client);
        System.out.println("---------------------");
        CompanyClient.Builder builder = new CompanyClient.Builder();
        builder.setCompanyName("huawei");
        builder.setCompanyAddress("haidian");
        builder.setCompanyRegfunds(20);
        builder.setmType("communication");
        CompanyClient client1 = builder.build();
        System.out.println(client1);
        System.out.println("---------------------");
        CompanyClient.Builder build1 = client1.newBuilder();
        build1.setCompanyName("baidu");
        CompanyClient client2 = build1.build();
        System.out.println(client2);
    }
}

输出结果如下:

CompanyClient{companyName='alibaba', companyAddress='wangjing', companyRegfunds=5.0千万, mPerson=10000, mType='null'}
---------------------
CompanyClient{companyName='huawei', companyAddress='haidian', companyRegfunds=20.0千万, mPerson=null, mType='communication'}
---------------------
CompanyClient{companyName='baidu', companyAddress='haidian', companyRegfunds=20.0千万, mPerson=null, mType='communication'}

首先看main函数中的第一行代码:

CompanyClient client = new CompanyClient.Builder()
                .setCompanyName("alibaba")
                .setCompanyAddress("wangjing")
                .setCompanyRegfunds(5)
                .setmPerson("10000")
                .build();

之所以可以这么执行,是因为内部静态类Builder里定义了一份与CompanyClient类一模一样的变量,通过一系列的成员函数进行设置属性值,但是返回值都是this,也就是都是Builder对象,最后提供了一个build函数用于创建CompanyClient对象,返回的是CompanyClient对象,对应的构造函数在CompanyClient 类中进行定义,也就是构造函数的入参是Builder对象,然后依次对自己的成员变量进行赋值,对应的值都是Builder对象中的值。此外Builder类中的成员函数返回Builder对象自身的另一个作用就是让它支持链式调用,使代码可读性大大增强。

总结下,Java环境下builder设计模式:

定义一个静态内部类Builder,内部的成员变量和外部类一样

Builder类通过一系列的方法用于成员变量的赋值,并返回当前对象本身(this)

Builder类提供一个build方法或者create方法用于创建对应的外部类,该方法内部调用了外部类的一个私有构造函数,该构造函数的参数就是内部类Builder

外部类提供一个私有构造函数供内部类调用,在该构造函数中完成成员变量的赋值,取值为Builder对象中对应的值

到此这篇关于java的Builder原理和实现详解的文章就介绍到这了,更多相关java Builder原理和实现内容请搜索好代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好代码网!

您可能有感兴趣的文章
Java 实例 - 字符串反转

Java实例--字符串替换

Java实例--删除字符串中最后一个字符

Java实例--查找字符串最后一次出现的位置

Java实例--字符串比较