-
Notifications
You must be signed in to change notification settings - Fork 0
Builder
Amir edited this page May 28, 2022
·
1 revision
عضو گروه Creatinal
این پترن بیشتر زمانی مورد اهمیته که با کلاسای بزرگ و پیچیده سر و کار داریم. به عبارت ساده تر کمکمون میکنه که اشیاء با پیچیدگی زیاد، راحت تر و یا شاید با اشیاء ساده تر ساخته بشن.

- امکان ساخت نمونههای مختلفی از یک شی را فراهم میسازد
- باعث جلوگیری از ساخت تعداد زیادی Constructor برای ساخت اشیا میشود
- ساخت اشیای پیچیده و مرکب را سادهتر میکند
- امکان ست کردن پارامترهای دلخواه برای شئ
- بالا بردن حجم کد
Home home = new Home(10, 15, 45, Red, Wood)Home home = new Home.Builder()
.setDoorWidth(10)
.setWindowWidth(15)
.setArea(45)
.setDoorColor(Red)
.setWindowType(Wood)class Home {
int doorWidth;
int windowWidth;
int area;
String doorColor;
String windowType;
public Home(int doorWidth, int windowWidth, int area, String doorColor, String windowType) {
this.doorWidth = doorWidth;
this.windowWidth = windowWidth;
this.area = area;
this.doorColor = doorColor;
this.windowType = windowType;
}
}final public class Home {
public int doorWidth;
public int windowWidth;
public int area;
public String doorColor;
public String windowType;
public Home(Builder builder) {
this.doorWidth = builder.doorWidth;
this.windowWidth = builder.windowWidth;
this.area = builder.area;
this.doorColor = builder.doorColor;
this.windowType = builder.windowType;
}
public static class Builder{
private int doorWidth;
private int windowWidth;
private int area;
private String doorColor;
private String windowType;
public Builder setDoorWidth(int doorWidth) {
this.doorWidth = doorWidth;
return this;
}
public Builder setWindowWidth(int windowWidth) {
this.windowWidth = windowWidth;
return this;
}
public Builder setArea(int area) {
this.area = area;
return this;
}
public Builder setDoorColor(String doorColor) {
this.doorColor = doorColor;
return this;
}
public Builder setWindowType(String windowType) {
this.windowType = windowType;
return this;
}
public Home build(){
return new Home(this);
}
}
}