티스토리 뷰
Builder Pattern
Builder pattern is how you create a complex object especially when you have many arguments.
It gives apparent division between the construction and representation of an object
Example Code
public class BuilderPattern {
private long id;
private String name;
private String email;
private String phone;
private String address;
//Inner static class
public static class Builder{
//Essential parameters
private final long id;
private final String name;
//Optional parameters
private String email = "";
private String phone = "";
private String address = "";
public Builder(long id, String name) {
this.id = id;
this.name = name;
}
//Setter methods. return 'this' reference
public Builder email(String email) {
this.email = email;
return this; //객체 자신의 참조값 전달. Chain method 사용가능
}
public Builder phone(String phone) {
this.phone = phone;
return this;
}
public Builder address(String address) {
this.address = address;
return this;
}
public BuilderPattern build() {
return new BuilderPattern(this);
}
}
private BuilderPattern(Builder builder) {
this.id = builder.id;
this.name = builder.name;
this.email = builder.email;
this.phone = builder.phone;
this.address = builder.address;
}
@Override
public String toString() {
return "id:" + id + " name:" + name + " email:" + email + " phone:" + phone + " address:" + address;
}
}
|
Main
public class Main {
public static void main(String[] args) {
BuilderPattern.Builder builder = new BuilderPattern.Builder(1, "jen");
builder.email("jen@gmail.com");
builder.phone("2182849334");
builder.address("Vancouver, CA");
BuilderPattern person = builder.build();
System.out.println(person.toString());
// Method chaining
BuilderPattern builder2 = new BuilderPattern
.Builder(2, "jin") // essential input
.phone("6474481029")
.address("Toronto, CA")
.build(); // build() returns Object
System.out.println(builder2.toString());
}
}
|
Output
id:1 name:jen email:jen@gmail.com phone:2182849334 address:Vancouver, CA
id:2 name:jin email: phone:6474481029 address:Toronto, CA
|
Reference
'JAVA' 카테고리의 다른 글
Difference between Setter and Constructor (0) | 2019.08.11 |
---|---|
Read CSV File and store data to MySQL (0) | 2019.08.01 |
Access ArrayList from another class (0) | 2019.08.01 |
The server time zone Error (0) | 2019.07.31 |
Connect to MySQL using Java JDBC (0) | 2019.07.31 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- code refactoring
- math.max
- spring boot application
- javascript
- algorithm
- Collection Framework
- 프로그래머스
- repeat()
- java
- easy javascript algorithm
- HashMap
- math.abs
- hackerrank javascript
- easy algorithm
- hackerrank javascript solution
- Object type casting
- string class in java
- hackerrank solution
- 프로그래머스 알고리즘
- 프로그래머스 알고리즘문제
- equals()
- HackerRank Algorithm
- rest parameter
- C++
- ... in Javascript
- hackerrank
- Javascript Algorithm
- compareTo()
- substring()
- 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함