zoukankan      html  css  js  c++  java
  • Builder 模式

    如果某一个类具有多个参数吗,并且其中有些参数不是必须的,那么在提供构造函数的时候就比较麻烦了。通常做法有三种:重叠构造函数方式,JavaBean 方式,Builder 方式。

    重 叠构造函数方式就是构造多个重载的构造函数,那么他们具有不同的参数,这样来满足不同的需求。JavaBean 方式则是提供一个默认无参的构造函数和所有属性的setter方法,通过setter方法进行初始化。Builder 方式是通过在类内部创建一个静态内部类,通过内部类对类进行构造,具体代码参考如下:

     1 public class Location {
     2 
     3     private final String country;
     4     private final String province;
     5     private final String city;
     6     private final String area;
     7     private final String street;
     8     private final String community;
     9     
    10     //Builder内部类,其成员和Location完全一致,Builder就相当于Location的影子。
    11     public static class Builder{
    12         private final String country;
    13         private final String province;
    14         private String city      = "";
    15         private String area      = "";
    16         private String street    = "";
    17         private String community = "";
    18         
    19         //对于必选参数,直接在Builder构造函数中设置好
    20         public Builder(String country, String province){
    21             this.country = country;
    22             this.province = province;
    23         }
    24         
    25         //对于可选参数,一个一个设置,同时返回Builder对象自身
    26         public Builder city(String city){
    27             this.city = city;
    28             return this;
    29         }
    30         public Builder area(String area){
    31             this.area = area;
    32             return this;
    33         }
    34         public Builder street(String street){
    35             this.street = street;
    36             return this;
    37         }
    38         public Builder community(String community){
    39             this.community = community;
    40             return this;
    41         }
    42         
    43         //利用builder对象构建Location对象
    44         public Location build(){
    45             return new Location(this);
    46         }
    47     }
    48     
    49     //Location构造方法,直接通builder对象中取值
    50     public Location(Builder builder){
    51         this.country   = builder.country;
    52         this.province  = builder.province;
    53         this.city      = builder.city;
    54         this.area      = builder.area;
    55         this.street    = builder.street;
    56         this.community = builder.community;
    57     }
    58     
    59     public static void main(String[] args){
    60         Location location = new Location.Builder("China", "Liao Ning").city("Da Lian").area("ShaHeKou").community("弘基书香园").build();
    61         System.out.println(location);
    62     }
    63     
    64 }

    这 是一个 Location 信息类,其中国家和省份是必须填写的。通过Builder内部类去对所有可选参数赋值,并返回赋值后的Builder对象,对于没有赋值的可选参数使用默 认值,Builder内部类提供一个build()方法将自己传入构建Location对象。由于Builder内部类就是Location的影子,直接 用Builder属性值赋值给Location完成构造。

    这样在构建Locaiton的时候就可以根据实际需要随意设置可选参数,非常灵活方便。缺点就是造成额外builder对象的内存消耗。

  • 相关阅读:
    五种排序算法
    call,apply,bind实现
    js面试题
    Date日期方法
    操作符
    var、let和const
    script标签属性
    HbuilderX如何让项目运行到微信开发者工具
    vite搭建vue3项目
    瀑布流实例及懒加载(echo.js)
  • 原文地址:https://www.cnblogs.com/orientsun/p/2609433.html
Copyright © 2011-2022 走看看