Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- ubuntu
- IPv6
- v-model vs v-bind
- design pattern
- camel case error
- kebab case
- MVVM
- executable jar
- builder pattern
- Linux
- java
- zen of python
- Python
- vuejs
- click.native
- Singleton pattern
- data
- Spring Boot
- maven
- prototype pattern
- Factory Method Pattern
- v-show vs v-if
- mount point
- $event.target.value
Archives
- Today
- Total
Eddie's Blog
[design pattern] Builder Pattern 본문
유형: 생성자 패턴
복잡한 객체의 구성 요소들을 조합해 생성하는 패턴으로, 그 생성과 조합에 대한 책임을 객체가 직접 지지않고 Builder에게 위임한다.
필요한 구성 요소의 조합에 따라 각각의 생성자가 필요
public class Person {
private String firstName;
private String middleName;
private String lastName;
private Integer age;
private String mothersName;
private Double height;
private Double weight;
public Person(String firstName, String middleName, String lastName, int age, double height, double weight) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.age = age;
this.height = height;
this.weight = weight;
}
public Person(String firstName, String middleName, String lastName, int age) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.age = age;
}
public Person(String firstName, String middleName, String lastName) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
/* getters */
}
Person 객체의 생성을 Builder에게 위임한다.
public class Person {
private String firstName;
private String middleName;
private String lastName;
private Integer age;
private String mothersName;
private Double height;
private Double weight;
public Person(String firstName, String middleName, String lastName, int age, double height, double weight) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.age = age;
this.height = height;
this.weight = weight;
}
public static class Builder {
private String firstName;
private String middleName;
private String lastName;
private Integer age;
private Double height;
private Double weight;
public Builder(){}
public Builder setFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public Builder setMiddleName(String middleName) {
this.middleName = middleName;
return this;
}
public Builder setLastName(String lastName) {
this.lastName = lastName;
return this;
}
public Builder setAge(Integer age) {
this.age = age;
return this;
}
public Builder setHeight(Double height) {
this.height = height;
return this;
}
public Builder setWeight(Double weight) {
this.weight = weight;
return this;
}
public Person build() {
return new Person(firstName, middleName, lastName, age, height, weight);
}
}
}
필요한 구성 요소를 조합하여 객체를 생성한다.
Person person = new Person.Builder()
.setAge(30)
.setFirstName("Michael")
.setLastName("Jordan")
.setHeight(198)
.build();
'programmings' 카테고리의 다른 글
[design pattern] Prototype Pattern (0) | 2019.03.28 |
---|---|
[design pattern] Factory Method Pattern (0) | 2019.03.28 |
[design pattern] Abstract Factory Pattern (0) | 2019.03.28 |
[spring boot] How to package a project with shell-executable jar (0) | 2019.02.11 |
[python] The Zen of Python, by Tim Peters (0) | 2018.06.13 |
Comments