目錄
一、介紹
二、示例
二、示例建造者模式是日常開發中比較常見的設計模式,它的主要作用就是將復雜事物創建的過程抽象出來,該 抽象的不同實現方式不同,創建出的對象也不同。 通俗的講,創建一個對象一般都會有一個固定的步驟,這個固定的步驟我們把它抽象出來,每個抽象步驟都會有不同的實現方式,不同的實現方式創建出的對象也將不同
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());
}
}
你是否還在尋找穩定的海外服務器提供商?創新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
當前名稱:建造者模式-創新互聯
瀏覽地址:http://vcdvsql.cn/article0/pjpoo.html
成都網站建設公司_創新互聯,為您提供網站內鏈、軟件開發、定制網站、標簽優化、靜態網站、網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯