기타/Java

[디자인패턴]Iterator Pattern

stonesy 2021. 12. 2. 14:39
728x90
public interface Iterator {
    boolean hasNext();
    MenuItem next();
}
import java.util.ArrayList;

public class ArrayIterator implements Iterator{
    private MenuItem[] menuItems;
    private int currentPosition = 0;

    ArrayIterator(MenuItem[] items){
        menuItems = items;
    }

    @Override
    public boolean hasNext(){
        if(menuItems.length>currentPosition)
            return true;
        else
            return false;
    }
    @Override
    public MenuItem next(){
        return menuItems[currentPosition++];
    }
}
import java.awt.*;
import java.util.ArrayList;

public class ArrayListIterator implements Iterator{
    private ArrayList myArrayList;
    private int currentPosition = 0;

    ArrayListIterator(ArrayList items){
        myArrayList = items;
    }

    @Override
    public boolean hasNext(){
        if(myArrayList.size()>currentPosition)
            return true;
        else
            return false;
    }
    @Override
    public MenuItem next(){
        return (MenuItem)myArrayList.get(currentPosition++);
    }
}
public class MenuItem {
    String name;
    String description;
    boolean vegeterian;
    double price;

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

    public String getName(){
        return name;
    }

    public String getDescription(){
        return description;
    }

    public double getPrice(){
        return price;
    }

    public boolean isVegeterian(){
        return vegeterian;
    }
}
import java.util.*;

public class MenuTest {
    public static void main(String args[]) {
        //PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
        //DinerMenu dinerMenu = new DinerMenu();

        printMenu();
    }

    public static void printMenu() {
        PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
        DinerMenu dinerMenu = new DinerMenu();

        ArrayList<MenuItem> breakfastItems = pancakeHouseMenu.getMenuItems();
        MenuItem[] lunchItems = dinerMenu.getMenuItems();

        // Hiding implementation
        System.out.println("USING FOR EACH");
        for (MenuItem menuItem : breakfastItems) {
            System.out.print(menuItem.getName());
            System.out.println("\t\t" + menuItem.getPrice());
            System.out.println("\t" + menuItem.getDescription());
        }
        for (MenuItem menuItem : lunchItems) {
            System.out.print(menuItem.getName());
            System.out.println("\t\t" + menuItem.getPrice());
            System.out.println("\t" + menuItem.getDescription());
        }

        // Exposing implementation
        System.out.println("USING FOR LOOPS");
        for (int i = 0; i < breakfastItems.size(); i++) {
            MenuItem menuItem = (MenuItem)breakfastItems.get(i);
            System.out.print(menuItem.getName());
            System.out.println("\t\t" + menuItem.getPrice());
            System.out.println("\t" + menuItem.getDescription());
        }

        for (int i = 0; i < lunchItems.length; i++) {
            MenuItem menuItem = lunchItems[i];
            System.out.print(menuItem.getName());
            System.out.println("\t\t" + menuItem.getPrice());
            System.out.println("\t" + menuItem.getDescription());
        }
    }
}
public class DinerMenu {
    static final int MAX_ITEMS = 6;
    int numberOfItems = 0;
    MenuItem[] menuItems;

    public DinerMenu() {
        menuItems = new MenuItem[MAX_ITEMS];

        addItem("Vegetarian BLT",
                "(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99);
        addItem("BLT",
                "Bacon with lettuce & tomato on whole wheat", false, 2.99);
        addItem("Soup of the day",
                "Soup of the day, with a side of potato salad", false, 3.29);
        addItem("Hotdog",
                "A hot dog, with saurkraut, relish, onions, topped with cheese",
                false, 3.05);
        addItem("Steamed Veggies and Brown Rice",
                "Steamed vegetables over brown rice", true, 3.99);
        addItem("Pasta",
                "Spaghetti with Marinara Sauce, and a slice of sourdough bread",
                true, 3.89);
    }

    public void addItem(String name, String description,
                        boolean vegetarian, double price)
    {
        MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
        if (numberOfItems >= MAX_ITEMS) {
            System.err.println("Sorry, menu is full!  Can't add item to menu");
        } else {
            menuItems[numberOfItems] = menuItem;
            numberOfItems = numberOfItems + 1;
        }
    }

    public MenuItem[] getMenuItems() {
        return menuItems;
    }

    public Iterator createIterator() {
        return new ArrayIterator(menuItems);
        // To test Alternating menu items, comment out above line,
        // and uncomment the line below.
        //return new AlternatingDinerMenuIterator(menuItems);
    }
    
    public String toString() {
        return "Objectville Diner Menu";
    }
    // other menu methods here
}
import java.util.ArrayList;

public class PancakeHouseMenu {
    ArrayList<MenuItem> menuItems;

    public PancakeHouseMenu() {
        menuItems = new ArrayList<MenuItem>();

        addItem("K&B's Pancake Breakfast",
                "Pancakes with scrambled eggs, and toast",
                true,
                2.99);

        addItem("Regular Pancake Breakfast",
                "Pancakes with fried eggs, sausage",
                false,
                2.99);

        addItem("Blueberry Pancakes",
                "Pancakes made with fresh blueberries",
                true,
                3.49);

        addItem("Waffles",
                "Waffles, with your choice of blueberries or strawberries",
                true,
                3.59);
    }

    public void addItem(String name, String description,
                        boolean vegetarian, double price)
    {
        MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
        menuItems.add(menuItem);
    }

    public ArrayList<MenuItem> getMenuItems() {
        return menuItems;
    }

    public Iterator createIterator() {
        return new ArrayListIterator(menuItems);
        // To test Alternating menu items, comment out above line,
        // and uncomment the line below.
        //return new AlternatingDinerMenuIterator(menuItems);
    }

    public String toString() {
        return "Objectville Pancake House Menu";
    }

    // other menu methods here
}
public class Main {
    public static void main(String[] args){
        PancakeHouseMenu myMenu = new PancakeHouseMenu();
        Iterator pancake = myMenu.createIterator();
        DinerMenu yourMenu = new DinerMenu();
        Iterator diner = yourMenu.createIterator();

        System.out.println(myMenu.toString());
        while(pancake.hasNext()){
            MenuItem menuItem = pancake.next();
            System.out.print(menuItem.getName());
            System.out.println("\t\t"+menuItem.getPrice());
            System.out.println("\t"+menuItem.getDescription());
        }
        System.out.println(yourMenu.toString());
        while(diner.hasNext()){
            MenuItem menuItem = diner.next();
            System.out.print(menuItem.getName());
            System.out.println("\t\t"+menuItem.getPrice());
            System.out.println("\t"+menuItem.getDescription());
        }
    }
}

<실행결과>

Objectville Pancake House Menu
K&B's Pancake Breakfast 2.99
Pancakes with scrambled eggs, and toast
Regular Pancake Breakfast 2.99
Pancakes with fried eggs, sausage
Blueberry Pancakes 3.49
Pancakes made with fresh blueberries
Waffles 3.59
Waffles, with your choice of blueberries or strawberries
Objectville Diner Menu
Vegetarian BLT 2.99
(Fakin') Bacon with lettuce & tomato on whole wheat
BLT 2.99
Bacon with lettuce & tomato on whole wheat
Soup of the day 3.29
Soup of the day, with a side of potato salad
Hotdog 3.05
A hot dog, with saurkraut, relish, onions, topped with cheese
Steamed Veggies and Brown Rice 3.99
Steamed vegetables over brown rice
Pasta 3.89
Spaghetti with Marinara Sauce, and a slice of sourdough bread

728x90

'기타 > Java' 카테고리의 다른 글

JAVA(2)  (0) 2024.01.28
JAVA(1)  (0) 2024.01.20
[Design Pattern]Adapter Pattern  (0) 2021.11.16
[Design Pattern]Command Pattern  (0) 2021.11.16
[Java]Design Pattern-Singleton  (0) 2021.11.03