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 |
댓글 영역