目錄
一、介紹
二、示例
二、示例建造者模式是日常開發(fā)中比較常見的設(shè)計模式,它的主要作用就是將復(fù)雜事物創(chuàng)建的過程抽象出來,該 抽象的不同實現(xiàn)方式不同,創(chuàng)建出的對象也不同。 通俗的講,創(chuàng)建一個對象一般都會有一個固定的步驟,這個固定的步驟我們把它抽象出來,每個抽象步驟都會有不同的實現(xiàn)方式,不同的實現(xiàn)方式創(chuàng)建出的對象也將不同
package com.example.designmode.demo.builder;
public class Student {
private String name;
private Integer age;
private Integer sex;
private String phone;
public static final class Builder {
private String name;
private Integer age;
private Integer sex;
private String phone;
public Builder() {
}
public static Builder aStudent() {
return new Builder();
}
public Builder withName(String name) {
this.name = name;
return this;
}
public Builder withAge(Integer age) {
this.age = age;
return this;
}
public Builder withSex(Integer sex) {
this.sex = sex;
return this;
}
public Builder withPhone(String phone) {
this.phone = phone;
return this;
}
public Student build() {
Student student = new Student();
student.name = this.name;
student.sex = this.sex;
student.age = this.age;
student.phone = this.phone;
return student;
}
}
}
package com.example.designmode.demo.builder;
public class Test {
public static void main(String[] args) {
Student build = new Student.Builder().withName("123").withAge(18).build();
System.out.println(build.toString());
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
當前名稱:建造者模式-創(chuàng)新互聯(lián)
瀏覽地址:http://aaarwkj.com/article0/pjpoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、軟件開發(fā)、定制網(wǎng)站、標簽優(yōu)化、靜態(tài)網(wǎng)站、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容