[자바 디자인 패턴] 생성패턴 - 빌더 패턴
생성패턴 목록
- 팩토리 메소드 패턴 (Factory Method Pattern)
- 추상 팩토리 패턴 (Abstract Factory Pattern)
- 싱글톤 패턴 (Singleton Pattern)
- 프로토타입 패턴 (Prototype Pattern)
- 빌더 패턴 (Builder Pattern)
- 오브젝트 풀 패턴 (Object Pool Pattern)
객체의 구성과 표현을 명확히 해야하는 경우에 필요한 것 같다.
하지만 추상적으로 정의되어있는 상태에서는 함부로 적용할 수 있는 패턴이 아닌 것 같다.
장점
- 객체의 구성과 표현을 명확하게 구분합니다.
- 프로세스를 보다 잘 제어할 수 있습니다.
- 객체의 내부 표현 변경을 지원합니다.
사용시기
- 단일 단계에서 개체를 만들 수 없을 때
UML
Interface Class : Item
public interface Item{
public String name();
public String size();
public float price();
}
Implement Class : Pizza, ColdDrink
public abstract class Pizza implements Item{
@Override
public abstract float price();
}
public abstract class ColdDrink implements Item{
@Override
public abstract float price();
}
Extend Class
- VegPizza
> MediumCheezePizza
> LargeCheezePizza
public abstract class VegPizza extends Pizza{
@Override
public abstract float price();
@Override
public abstract String name();
@Override
public abstract String size();
}
public class MediumCheezePizza extends VegPizza{
@Override
public float price(){
return 220.0f;
}
@Override
public String name(){
return "Cheeze Pizza";
}
@Override
public String size(){
return "Medium Size";
}
}
public class LargeCheezePizza extends VegPizza{
@Override
public float price(){
return 260.0f;
}
@Override
public String name(){
return "Cheeze Pizza";
}
@Override
public String size(){
return "Large Size";
}
}
- NonVegPizza
> MediumNonVegPizza
> LargeNonVegPizza
public abstract class NonVegPizza extends Pizza{
@Override
public abstract float price();
@Override
public abstract String name();
@Override
public abstract String size();
}
public class MediumNonVegPizza extends NonVegPizza{
@Override
public float price(){
return 200.0f;
}
@Override
public String name(){
return "Non-Veg Pizza";
}
@Override
public String size(){
return "Medium Size";
}
}
public class LargeNonVegPizza extends NonVegPizza{
@Override
public float price(){
return 220.0f;
}
@Override
public String name(){
return "Non-Veg Pizza";
}
@Override
public String size(){
return "Large Size";
}
}
- Pepsi
> MediumPepsi
> LargePepsi
public abstract class Pepsi extends ColdDrink{
@Override
public abstract float price();
@Override
public abstract String name();
@Override
public abstract String size();
}
public class MediumPepsi extends Pepsi{
@Override
public float price(){
return 35.0f;
}
@Override
public String name(){
return "500 ml Pepsi";
}
@Override
public String size(){
return "Medium Size";
}
}
public class LargePepsi extends Pepsi{
@Override
public float price(){
return 50.0f;
}
@Override
public String name(){
return "750 ml Pepsi";
}
@Override
public String size(){
return "Large Size";
}
}
- Coke
> MediumCoke
> LargeCoke
public abstract class Coke extends ColdDrink{
@Override
public abstract float price();
@Override
public abstract String name();
@Override
public abstract String size();
}
public class MediumCoke extends Coke{
@Override
public float price(){
return 35.0f;
}
@Override
public String name(){
return "500 ml Coke";
}
@Override
public String size(){
return "Medium Size";
}
}
public class LargeCoke extends Coke{
@Override
public float price(){
return 50.0f;
}
@Override
public String name(){
return "750 ml Coke";
}
@Override
public String size(){
return "Large Size";
}
}
Operation Class : OrderedItems, OrderBuilder, BuilderDemo
public class OrderedItems{
List<Item> items = new ArrayList<Item>();
public void addItems(Item item){
items.add(item);
}
public float getCost(){
float cost = 0.0f;
for(Item item : items){
cost += item.price();
}
return cost;
}
public void showItems(){
for(Item item : items){
System.out.println("Item is : " + item.name());
System.out.println("Size is : " + item.size());
System.out.println("Price is : " + item.price());
}
}
}
public class Orderbuilder{
public OrderedItems preparePizza() throws IOException{
OrderedItems itemsOrder = new OrderedItems();
// Pizza
itemsOrder.addItems(new MediumCheezePizza());
itemsOrder.addItems(new LargeCheezePizza());
itemsOrder.addItems(new MediumNonVegPizza());
itemsOrder.addItems(new LargeNonVegPizza());
// Drink
itemsOrder.addItems(new MediumPepsi());
itemsOrder.addItems(new LargePepsi());
itemsOrder.addItems(new MediumCoke());
itemsOrder.addItems(new LargeCoke());
return itemsOrder;
}
}
public class BuilderDemo{
public static void main(String[] args) throws IOException{
OrderBuilder builder = new OrderBuilder();
OrderedItems orderedItems = builder.preparePizza();
orderedItems.showItems();
System.out.println("");
System.out.println("Total Cost : " + orderedItems.getCost());
}
}
출처 : https://www.javatpoint.com/
know-one-by-one.tistory.com/37
'JAVA > Design Patterns' 카테고리의 다른 글
[자바 디자인 패턴] 구조패턴 - 컴포짓 패턴 (0) | 2021.03.17 |
---|---|
[자바 디자인 패턴] 구조패턴 - 브릿지 패턴 (0) | 2021.03.17 |
[자바 디자인 패턴] 구조패턴 - 어댑터 패턴 (0) | 2021.03.17 |
[자바 디자인 패턴] 생성패턴 - 오브젝트풀 패턴 (0) | 2021.03.17 |
[자바 디자인 패턴] 생성패턴 - 프로토타입 패턴 (0) | 2021.03.16 |
[자바 디자인 패턴] 생성패턴 - 싱글톤 패턴 (0) | 2021.03.16 |
[자바 디자인 패턴] 생성패턴 - 추상 팩토리 패턴 (0) | 2021.03.16 |
[자바 디자인 패턴] 생성패턴 - 팩토리 메소드 패턴 (0) | 2021.03.16 |