public class Item {
private int id;
private String name;
private int price;
private int count;
private Item(int id, String name, int price, int count) {
this.id = id;
this.name = name;
this.price = price;
this.count = count;
}
@Override
public String toString() {
return "" + this.id + " " + this.name + " " + this.price + " " + this.count;
}
public static ItemBuilder builder() {
return new ItemBuilder();
}
public static class ItemBuilder {
private int id;
private String name;
private int price;
private int count;
public ItemBuilder id(int id) {
this.id = id;
return this;
}
public ItemBuilder name(String name) {
this.name = name;
return this;
}
public ItemBuilder price(int price) {
this.price = price;
return this;
}
public ItemBuilder count(int count) {
this.count = count;
return this;
}
public Item build() {
return new Item(id, name, price, count);
}
}
}
ํ ์คํธ
public class Main {
public static void main(String[] args) throws Exception {
Item item = Item.builder().id(1).name("์๊ฑด").price(1400).count(5).build();
System.out.println(item);
Item item2 = Item.builder().id(1).name("์๊ฑด").price(1400).count(5).build();
System.out.println(item2);
System.out.println(item == item2);
}
}
| [๋์์ธํจํด] ํฉํฐ๋ฆฌํจํด (0) | 2024.05.05 |
|---|---|
| [๋์์ธํจํด] ์ฑ๊ธํด ํจํด (0) | 2024.05.01 |
| [๋์์ธํจํด์ ์๋ฆ๋ค์] 3์ฅ. ์ค๊ณ ์์น : ๋จ์ผ์ฑ ์์์น, ๊ฐ๋ฐฉํ์ ์์น (0) | 2024.04.20 |
| ์ด๋ํฐ ํจํด (0) | 2024.03.05 |