建造者模式(Builder Pattern)是属于创建型模式,它的意图是:将一个复杂的对象的创建和表示分离,是的同样的构建过程可以创建不同的表示。
如果有一个类Test有1个属性 name,不管我们如何穿件这个对象的实例都不会很麻烦。
public class Test {
private String name;
public Test(String name) {
super();
this.name = name;
}
public Test() {
super();
}
/**
* Getter method for property name.
*
* @return property value of name
*/
public String getName() {
return name;
}
/**
* Setter method for property name.
*
* @param name value to be assigned to property name
*/
public void setName(String name) {
this.name = name;
}
}
public class TestClient {
/**
*
* @param args
*/
public static void main(String[] args) {
Test test=new Test();
test.setName("MyTest");
Test test2=new Test("MyTest2");
}
}
那么,如果一个类的属性有很多,甚至是数十个的时候构建一个对象将是一件很痛苦的过程,如下就是在项目中遇到的一个构建一个对象。
TaxBillModel taxBillModel = new TaxBillModel();
taxBillModel.setApplyBillDate(new Date());
taxBillModel.setBillBatchNo(billBatchNo);
taxBillModel.setBillDate(new Date());
taxBillModel.setChargeOffDate(new Date());
taxBillModel.setCheckDate(new Date());
taxBillModel.setExchangeDate(new Date());
taxBillModel.setExchangeRate(Double.parseDouble(exchangeRate));
taxBillModel.setExpiryDate(new Date());
taxBillModel.setFailureAmount(new Money(Double.parseDouble(failureAmount)));
taxBillModel.setFailureCount(Long.parseLong(failureCount));
taxBillModel.setGmtCreate(new Date());
taxBillModel.setGmtModified(new Date());
taxBillModel.setOriginalAmount(new Money(Double.parseDouble(originalAmount)));
taxBillModel.setPartnerName(partnerName);
taxBillModel.setRealCurrency(realCurrency);
taxBillModel.setRealAmount(new Money(Long.parseLong(realAmount)));
taxBillModel.setPartnerId(parnterId);
taxBillModel.setRemitNo(remitNo);
taxBillModel.setSuccessCount(Long.parseLong(successCount));
taxBillModel.setCurrency(currency);
taxBillModel.setStatus("RA");
在这样的一个情况下我们应该使用Builder Pattern 去解决问题。
public class MyModel {
private int id;
/**名称**/
private String name;
/**币种**/
private String currency;
/**金额**/
private BigDecimal amount;
/**
* Getter method for property id.
*
* @return property value of id
*/
public int getId() {
return id;
}
/**
* Setter method for property id.
*
* @param id value to be assigned to property id
*/
public void setId(int id) {
this.id = id;
}
/**
* Getter method for property name.
*
* @return property value of name
*/
public String getName() {
return name;
}
/**
* Setter method for property name.
*
* @param name value to be assigned to property name
*/
public void setName(String name) {
this.name = name;
}
/**
* Getter method for property currency.
*
* @return property value of currency
*/
public String getCurrency() {
return currency;
}
/**
* Setter method for property currency.
*
* @param currency value to be assigned to property currency
*/
public void setCurrency(String currency) {
this.currency = currency;
}
/**
* Getter method for property amount.
*
* @return property value of amount
*/
public BigDecimal getAmount() {
return amount;
}
/**
* Setter method for property amount.
*
* @param amount value to be assigned to property amount
*/
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
}
public class MyModelBuilder {
private int id;
private String name;
private String currency;
private BigDecimal amount;
public MyModelBuilder addId(int id) {
this.id = id;
return this;
}
public MyModelBuilder addName(String name) {
this.name = name;
return this;
}
public MyModelBuilder addMoney(String currency, BigDecimal amount) {
this.currency = currency;
this.amount = amount;
return this;
}
public MyModel toMyModel() {
MyModel myModel = new MyModel();
myModel.setAmount(amount);
myModel.setCurrency(currency);
myModel.setId(id);
myModel.setName(name);
return myModel;
}
}
public class Client {
/**
*
* @param args
*/
public static void main(String[] args) {
MyModelBuilder builder = new MyModelBuilder();
builder.addMoney("USD", BigDecimal.valueOf(100));
builder.addId(0);
builder.addName("Test");
MyModel myModel = builder.toMyModel();
System.out.println(myModel);
}
}