前言
構(gòu)建者模式邑闺,是用來構(gòu)建我們的對象跌前。如果我們構(gòu)建的對象有很多屬性值要設(shè)置,使用大量的setXX()陡舅,這樣代碼不利于閱讀抵乓,也不整潔。
實現(xiàn)
我們假設(shè)有創(chuàng)建的是一個訂單蹭沛。使用構(gòu)建者模式如下臂寝。PS:通常的構(gòu)建者模式中是不提供setXX()方法,一旦對象構(gòu)建以后就不允許修改摊灭,但是實際業(yè)務(wù)場景中有時候創(chuàng)建完一個對象咆贬,后續(xù)根據(jù)情形需要單獨修改某個屬性值,所以提供了setXX().
/**
* 訂單
*/
public class Order {
/**
* 價格
*/
private Double price;
/**
* 訂單號
*/
private String orderId;
/**
* 寄件人姓名
*/
private String sendMan;
/**
* 寄件人地址
*/
private String sendAddress;
/**
* 寄件人電話
*/
private String sendMobile;
/**
* 收件人
*/
private String receiveMan;
/**
* 收件人地址
*/
private String receiveAddress;
/**
* 收件人電話
*/
private String receiveMobile;
/**
* 備注
*/
private String remark;
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getSendMan() {
return sendMan;
}
public void setSendMan(String sendMan) {
this.sendMan = sendMan;
}
public String getSendAddress() {
return sendAddress;
}
public void setSendAddress(String sendAddress) {
this.sendAddress = sendAddress;
}
public String getSendMobile() {
return sendMobile;
}
public void setSendMobile(String sendMobile) {
this.sendMobile = sendMobile;
}
public String getReceiveMan() {
return receiveMan;
}
public void setReceiveMan(String receiveMan) {
this.receiveMan = receiveMan;
}
public String getReceiveAddress() {
return receiveAddress;
}
public void setReceiveAddress(String receiveAddress) {
this.receiveAddress = receiveAddress;
}
public String getReceiveMobile() {
return receiveMobile;
}
public void setReceiveMobile(String receiveMobile) {
this.receiveMobile = receiveMobile;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
/**
* 靜態(tài)方法獲取bulider
* @return
*/
public static Builder Builder(){
return new Builder();
}
/**
* Builder是public static 內(nèi)部類.
* 1 pulblic 為了擴大訪問域帚呼,包外也可以
* 2 static 是為了Order方法需要通過靜態(tài)方法獲取Builder
*/
public static class Builder{
private Double price;
private String orderId;
private String sendMan;
private String sendAddress;
private String sendMobile;
private String receiveMan;
private String receiveAddress;
private String receiveMobile;
private String remark;
public Builder price(Double price){
this.price = price;
return this;
}
public Builder orderId(String orderId){
this.orderId = orderId;
return this;
}
public Builder sendMan(String sendMan){
this.sendMan = sendMan;
return this;
}
public Builder sendAddress(String sendAddress){
this.sendAddress = sendAddress;
return this;
}
public Builder sendMobile(String sendMobile){
this.sendMobile = sendMobile;
return this;
}
public Builder receiveMan(String receiveMan){
this.receiveMan = receiveMan;
return this;
}
public Builder receiveAddress(String receiveAddress){
this.receiveAddress = receiveAddress;
return this;
}
public Builder receiveMobile(String receiveMobile){
this.receiveMobile = receiveMobile;
return this;
}
public Builder remark(String remark){
this.remark = remark;
return this;
}
public Order build(){
Order order = new Order();
order.orderId = this.orderId;
order.price = this.price;
order.receiveAddress = this.receiveAddress;
order.receiveMan = this.receiveMan;
order.receiveMobile = this.receiveMobile;
order.price = this.price;
order.sendAddress = this.sendAddress;
order.sendMan = this.sendMan;
order.sendMobile = this.sendMobile;
order.remark = this.remark;
return order;
}
}
}
使用方式:
public class DesignPatternApplication {
public static void main(String[] args) {
Order order = Order.Builder().orderId("123").price(12.4).receiveMan("dd")
.receiveAddress("dd").sendAddress("dd").remark("ddada").build();
//后續(xù)業(yè)務(wù)場景需要掏缎,可以修改某個屬性
order.setRemark("666");
System.out.println(order.getOrderId());
}
}
不重復(fù)造輪子,使用lombok
復(fù)雜的系統(tǒng)中煤杀,為了開發(fā)人員更專注于業(yè)務(wù)眷蜈,不必要編寫AllArgsConstructor,NoArgsConstructor,Builder沈自,getter,setter酌儒,建議使用lombok。通過簡單的注解枯途,即可完成以上重復(fù)繁瑣的工作忌怎,引入lombok后編譯器在編譯的時候會生成包含以上內(nèi)容class文件籍滴,感興趣的小伙伴可以看看引入lombok后生成的class文件