最近闲着无聊,研究了一下 Mybatis 的源码,发现有如下的设计方式。
package org.apache.ibatis.mapping;
import javax.sql.DataSource;
import org.apache.ibatis.transaction.TransactionFactory;
public final class Environment {
private final String id;
private final TransactionFactory transactionFactory;
private final DataSource dataSource;
public Environment(String id, TransactionFactory transactionFactory, DataSource dataSource) {
if (id == null) {
throw new IllegalArgumentException("Parameter 'id' must not be null");
}
if (transactionFactory == null) {
throw new IllegalArgumentException("Parameter 'transactionFactory' must not be null");
}
this.id = id;
if (dataSource == null) {
throw new IllegalArgumentException("Parameter 'dataSource' must not be null");
}
this.transactionFactory = transactionFactory;
this.dataSource = dataSource;
}
public static class Builder {
private String id;
private TransactionFactory transactionFactory;
private DataSource dataSource;
public Builder(String id) {
this.id = id;
}
public Builder transactionFactory(TransactionFactory transactionFactory) {
this.transactionFactory = transactionFactory;
return this;
}
public Builder dataSource(DataSource dataSource) {
this.dataSource = dataSource;
return this;
}
public String id() {
return this.id;
}
public Environment build() {
return new Environment(this.id, this.transactionFactory, this.dataSource);
}
}
public String getId() {
return this.id;
}
public TransactionFactory getTransactionFactory() {
return this.transactionFactory;
}
public DataSource getDataSource() {
return this.dataSource;
}
}
Environment
类定义了一个内部的Builder
类?Builder
类对外部类的赋值?consturctor(a, b)
,极有可能复制成 b, a。package com.lucas;
public class LucasLuo {
private final String name;
private final String sex;
private final int age;
private final int height;
private final int weight;
private final boolean single;
private final String mobile;
private final String qq;
private final String wechat;
private final String email;
public static class Builder {
private String name;
private String sex;
private int age;
private int height;
private int weight;
private boolean single;
private String mobile;
private String qq;
private String wechat;
private String email;
public Builder name(String name) {
this.name = name;
return this;
}
public Builder sex(String sex) {
this.sex = sex;
return this;
}
public Builder age(int age) {
this.age = age;
return this;
}
public Builder height(int height) {
this.height = height;
return this;
}
public Builder weight(int weight) {
this.weight = weight;
return this;
}
public Builder single(boolean single) {
this.single = single;
return this;
}
public Builder mobile(String mobile) {
this.mobile = mobile;
return this;
}
public Builder qq(String qq) {
this.qq = qq;
return this;
}
public Builder wechat(String wechat) {
this.wechat = wechat;
return this;
}
public Builder email(String email) {
this.email = email;
return this;
}
public LucasLuo build() {
return new LucasLuo(this);
}
}
private LucasLuo(Builder builder) {
this.name = builder.name;
this.sex = builder.sex;
this.age = builder.age;
this.height = builder.height;
this.weight = builder.weight;
this.single = builder.single;
this.mobile = builder.mobile;
this.qq = builder.qq;
this.wechat = builder.wechat;
this.email = builder.email;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public int getAge() {
return age;
}
public int getHeight() {
return height;
}
public int getWeight() {
return weight;
}
public boolean getSingle() {
return single;
}
public String getMobile() {
return mobile;
}
public String getQq() {
return qq;
}
public String getWechat() {
return wechat;
}
public String getEmail() {
return email;
}
}
package com.lucas;
public class LucasMain {
public static void main(String[] args) {
LucasLuo lucasluo =
new LucasLuo
.Builder()
.name("Lucas Luo")
.age(20)
.sex("男")
.height(175)
.weight(70)
.single(true) // 这是重点,欢迎单身MM联络
.qq("123456")
.wechat("lucasluo")
.email("lucasluo@somesite.com")
.build();
System.out.println(lucasluo.getName());
}
}
当参数有很多个的时候,使用构建器替代静态工厂或重载构造函数是一个不错的办法。
电脑里钻进了几只小蟑螂,怎么抠也抠不出来。
写了一段代码,起了四个线程。
while true。
热。
爬出来了。
碾死。
Done