Design Architecture/Design Pattern 예제

Composite 패턴 예제

lipnus 2021. 9. 28. 21:40
반응형

Menu-->MenuComponent는 자손을 가리키는 것.

 

public abstract class MenuComponent {
    public void add(MenuComponent menuComponent) {
        throw new UnsupportedOperationException();
    }

    public void remove(MenuComponent menuComponent) {
        throw new UnsupportedOperationException();
    }

    public MenuComponent getChild(int i) {
        throw new UnsupportedOperationException();
    }

    public String getName(){
        throw new UnsupportedOperationException();
    }

    public String getDescription(){
        throw new UnsupportedOperationException();
    }

    public double getPrice(){
        throw new UnsupportedOperationException();
    }

    public boolean isVegetarian(){
        throw new UnsupportedOperationException();
    }

    public void print(){
        throw new UnsupportedOperationException();
    }
}



public class Menu extends MenuComponent {
    ArrayList<MenuComponent> menuComponents = new ArrayList();
    String name;
    String description;

    public Menu(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public void add(MenuComponent menuComponent) {
        menuComponents.add(menuComponent);
    }

    public void remove(MenuComponent menuComponent){
        menuComponents.remove(menuComponent);
    }

    public MenuComponent getChild(int i){
        return menuComponents.get(i);
    }

    public String getName(){
        return name;
    }

    public String getDescription() {
        return description;
    }

    public void print() {
        System.out.println(getName());
        System.out.println(getDescription());
        System.out.println("----------------------------------------------------");

        Iterator<MenuComponent> iterator = menuComponents.iterator();    //Menu 정보 뿐아니라 Menu안의 아이템까지 출력
        while(iterator.hasNext()){
            MenuComponent menuComponent = iterator.next();
            menuComponent.print();
        }
    }
}



public class MenuItem extends MenuComponent {
    String name;
    String description;
    boolean vegetarian;
    double price;

    public MenuItem(String name, String description, boolean vegetarian, double price) {
        this.name = name;
        this.description = description;
        this.vegetarian = vegetarian;
        this.price = price;
    }

    public String getName(){
        return name;
    }

    public String getDescription() {
        return description;
    }

    public double getPrice() {
        return price;
    }

    public boolean isVegetarian() {
        return vegetarian;
    }

    public void print() {
        System.out.println(getName());
        if (isVegetarian()) System.out.println("(v)");
        System.out.println(getPrice());
        System.out.println(getDescription());
        System.out.println();
    }
}



public class Waitress {
    MenuComponent  allMenus;

    public Waitress(MenuComponent allMenus) {
        this.allMenus = allMenus;
    }

    public void printMenu(){
        allMenus.print();
    }
}



public class Main {
    public static void main(String[] args) {
        MenuComponent AMenu = new Menu("A 메뉴", "AAAA");
        MenuComponent BMenu = new Menu("B 메뉴", "BBBB");
        MenuComponent CMenu = new Menu("C 메뉴", "CCCC");
        MenuComponent DMenu = new Menu("D 메뉴", "DDDD");

        MenuComponent allMenus = new Menu("전체 메뉴", "This is All Menu.");
        allMenus.add(AMenu);
        allMenus.add(BMenu);
        allMenus.add(CMenu);

        BMenu.add(new MenuItem("파스타", "파스타입니다", true, 3.89));
        BMenu.add(DMenu);
        DMenu.add(new MenuItem("애플파이", "애플파이입니다", true, 1.59));

        Waitress waitress = new Waitress(allMenus);
        waitress.printMenu();
    }
}

 

 

전체 메뉴
This is All Menu.
----------------------------------------------------
A 메뉴
AAAA
----------------------------------------------------
B 메뉴
BBBB
----------------------------------------------------
파스타
(v)
3.89
파스타입니다

D 메뉴
DDDD
----------------------------------------------------
애플파이
(v)
1.59
애플파이입니다

C 메뉴
CCCC
----------------------------------------------------

Process finished with exit code 0

 

 

 

 

 

https://jusungpark.tistory.com/26?category=630296 

 

반응형

'Design Architecture > Design Pattern 예제' 카테고리의 다른 글

Commnad패턴 예제  (0) 2022.04.26
Visitor 패턴 예제  (0) 2022.03.25
Decorator 예제  (0) 2021.09.28
Template Method 예제  (0) 2021.09.28
빌더(Builder) 패턴 예제  (0) 2021.09.26