您现在的位置是:主页 > Web前端技术 > Web前端技术

java中Builder原理的示例分析开发技术

IDCBT2021-12-30服务器技术人已围观

简介这篇文章主要为大家展示了“java中Builder原理的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“java中Builder原理的示例分

这篇文章主要为大家展示了“java中Builder原理的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“java中Builder原理的示例分析”这篇文章吧。

首先给一个简单的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);
         }
    }
 }

标签:

很赞哦! ()

本栏推荐