μ§μ₯ λλ£κ° Setterλμ Builderλ₯Ό μ¬μ©νκΈΈλ κΆκΈν΄μ μ°Ύμ보μλ€.
μ Builder ν¨ν΄μ μ¬μ©ν κΉ?
μ½λμ κ°λ μ±, μμ μ±, μ μ§λ³΄μμ±μ΄ ν₯μλκΈ° λλ¬Έμ΄λ€.
κ°λ μ±
Setterλ₯Ό μ¬μ©νλ©΄ κ°μ²΄λ₯Ό μμ±ν ν μ¬λ¬ λ²μ λ©μλ νΈμΆμ΄ νμνλ€.
λ°λ©΄, Builder ν¨ν΄μ μ¬μ©νλ©΄ 체μ΄λ λ°©μμΌλ‘ ν λ²μ κ°μ²΄λ₯Ό ꡬμ±ν μ μμ΄ μ½λκ° λ μ½κΈ° μ¬μμ§λ€.
Setter λ°©μ
// Setter λ°©μ
Book book = new Book();
book.setTitle("μ±μμ£Όμμ");
book.setAuthor("νκ°");
book.setPrice(13500);
book.setPublisher("μ°½λΉ");
- κ°μ²΄λ₯Ό μμ±ν ν μ¬λ¬ λ²μ set() νΈμΆμ΄ νμ
- νμ κ°μ΄ λλ½λ κ°λ₯μ±μ΄ μμ
Builder λ°©μ
// Builder λ°©μ
Book book = new Book.Builder()
.title("μ±μμ£Όμμ")
.author("νκ°")
.price(13500)
.publisher("μ°½λΉ")
.build();
- 체μ΄λ λ°©μμΌλ‘ λ μ½κΈ° μ¬μ΄ μ½λ
- κ°μ²΄κ° μμ±λ λ νμ κ°μ΄ 보μ₯λ¨
μμ μ±
setter μ¬μ©μ
class Book {
private String title;
private String author;
private int price;
private String publisher;
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPrice(int price) {
this.price = price;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public void printBookInfo() {
System.out.println("μ±
μ λͺ©: " + title);
System.out.println("μ μ: " + author);
System.out.println("κ°κ²©: " + price);
System.out.println("μΆνμ¬: " + publisher);
}
}
public class Main {
public static void main(String[] args) {
Book book = new Book();
book.setTitle("μ±μμ£Όμμ");
book.setPrice(13500);
// author(μ μ)μ publisher(μΆνμ¬)λ₯Ό μ€μ νμ§ μμ (λλ½λ κ°)
book.printBookInfo();
}
}
setterλ₯Ό μ¬μ©νλ©΄ νλλ₯Ό λ°λ‘ λ°λ‘ μ€μ ν μ μμ΄μ, νμ κ°μ΄ λλ½λ μνμ΄ μλ€.
Builder λ°©μ
class Book {
private final String title; // νμ
private final String author; // νμ
private final int price; // νμ
private final String publisher; // μ ν
private Book(Builder builder) {
this.title = builder.title;
this.author = builder.author;
this.price = builder.price;
this.publisher = builder.publisher;
}
public static class Builder {
private final String title;
private final String author;
private final int price;
private String publisher = "λ―Έμ "; // κΈ°λ³Έκ°
public Builder(String title, String author, int price) {
this.title = title;
this.author = author;
this.price = price;
}
public Builder publisher(String publisher) {
this.publisher = publisher;
return this;
}
public Book build() {
return new Book(this);
}
}
public void printBookInfo() {
System.out.println("μ±
μ λͺ©: " + title);
System.out.println("μ μ: " + author);
System.out.println("κ°κ²©: " + price);
System.out.println("μΆνμ¬: " + publisher);
}
}
public class Main {
public static void main(String[] args) {
// νμ κ°(title, author, price) μμ΄λ μμ±μ΄ λΆκ°λ₯νλ€.
Book book = new Book.Builder("μ±μμ£Όμμ", "νκ°", 13500)
.publisher("μ°½λΉ") // μ ν κ°
.build();
book.printBookInfo();
}
}
- νμ κ°(titie, author, price)μ μμ±μμκ² κ°μ
- κ°μ²΄ μμ± μμ μ λͺ¨λ νμ κ°μ΄ μ€μ λλ―λ‘ μμ μ
'πΏππππππππππ π»πππππππ > πΉπ°π π°' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
| [JAVA] HttpURLConnection (URLConnectoin)μ΄λ (0) | 2025.11.10 |
|---|---|
| [Java] URL μ΄λ (1) | 2025.11.04 |
| [JAVA] jar νμΌ μμ± λͺ λ Ήμ΄ jar cvf aProject.jar -C aProject . (0) | 2025.01.11 |
| [JAVA] λ€νμ± (0) | 2024.11.27 |
| [JAVA] instanceof μ°μ°μ (0) | 2024.11.26 |