java 设计模式 builder_Java 设计模式-建造者模式(Builder)

建造者模式(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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值