迭代器设计模式

在这个例子中,我们将实现两个菜单,一个午餐菜单使用 ArrayList 实现,另一个早餐菜单使用数组实现。然后,使用迭代器模式遍历两个菜单,并将它们合并到一起,最后输出所有菜单项。

步骤:

  1. 创建迭代器接口:定义用于遍历集合的标准接口。
  2. 实现午餐菜单和早餐菜单类:分别使用 ArrayList 和数组实现菜单。
  3. 创建迭代器实现类:实现菜单各自的迭代器。
  4. 实现菜单的遍历与合并

代码实现

1. 定义 Iterator 接口
import java.util.Iterator;

public interface MenuIterator<T> {
    boolean hasNext();
    T next();
}
2. 创建 MenuItem 类表示菜单项

public class MenuItem {
    private String name;
    private String description;
    private double price;

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

    public String getName() {
        return name;
    }
    
    public String getDescription() {
        return description;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return name + ": " + description + " -- ¥" + price;
    }
}

3. 实现 LunchMenu 类,使用 ArrayList 存储菜单项
import java.util.ArrayList;
import java.util.List;

public class LunchMenu {
    List<MenuItem> menuItems;

    public LunchMenu() {
        menuItems = new ArrayList<>();
        addItem("茄汁面", "番茄鸡蛋汤面", 8);
        addItem("蒜汁面", "凉拌面", 7);
        addItem("臊子面", "羊肉臊子面", 15);
    }

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

    public MenuIterator<MenuItem> createIterator() {
        return new LunchMenuIterator(menuItems);
    }
}
4. 实现 LunchMenuIterator
import java.util.List;

public class LunchMenuIterator implements MenuIterator<MenuItem> {
    private List<MenuItem> items;
    private int position = 0;

    public LunchMenuIterator(List<MenuItem> items) {
        this.items = items;
    }

    @Override
    public boolean hasNext() {
        return position < items.size();
    }

    @Override
    public MenuItem next() {
        return items.get(position++);
    }
}
5. 实现 BreakfastMenu 类,使用数组存储菜单项
public class BreakfastMenu {
    private static final int MAX_ITEMS = 5;
    private int numberOfItems = 0;
    private MenuItem[] menuItems;

    public BreakfastMenu() {
        menuItems = new MenuItem[MAX_ITEMS];
        addItem("胡辣汤", "素胡辣汤", 3);
        addItem("油条", "按斤称", 2);
        addItem("包子", " 莲藕馅", 1.5);
    }

    public void addItem(String name, String description, double price) {
        if (numberOfItems > MAX_ITEMS) {
            System.out.println("菜单满了,少吃点,不能添加了");
        } else {
            MenuItem menuItem = new MenuItem(name, description, price);
            menuItems[numberOfItems] = menuItem;
            numberOfItems++;
        }
    }

    public MenuIterator<MenuItem> createIterator() {
        return new BreakfastMenuIterator(menuItems);
    }
}

6. 实现 BreakfastMenuIterator
public class BreakfastMenuIterator implements MenuIterator{
    private MenuItem[] items;
    private int position = 0;

    public BreakfastMenuIterator(MenuItem[] items) {
        this.items = items;
    }

    @Override
    public boolean hasNext() {
        return position < items.length && items[position] != null;
    }

    @Override
    public Object next() {
        return items[position++];
    }
}

7. 实现 Waitress 类用于遍历并打印合并后的菜单
public class Waitress {
    BreakfastMenu breakfastMenu;
    LunchMenu launchMenu;

    public Waitress(BreakfastMenu breakfastMenu, LunchMenu launchMenu) {
        this.breakfastMenu = breakfastMenu;
        this.launchMenu = launchMenu;
    }

    public void printMenu(){
        MenuIterator<MenuItem> breakfastMenuIterator = breakfastMenu.createIterator();
        MenuIterator<MenuItem> launchMenuIterator = launchMenu.createIterator();

        System.out.println("\n早餐菜单:");
        printMenu(breakfastMenuIterator);

        System.out.println("午餐菜单");
        printMenu(launchMenuIterator);

    }
    public void printMenu(MenuIterator<MenuItem> iterator) {
        while (iterator.hasNext()) {
            MenuItem item = iterator.next();
            System.out.println(item);
        }
    }
}

8. 测试代码
public class IteratorPatternDemo {
    public static void main(String[] args) {
        LunchMenu lunchMenu = new LunchMenu();
        BreakfastMenu breakfastMenu = new BreakfastMenu();

        Waitress waitress = new Waitress(breakfastMenu, lunchMenu);
        waitress.printMenu();
    }
}

输出结果

早餐菜单:
胡辣汤: 素胡辣汤 -- ¥3.0
油条: 按斤称 -- ¥2.0
包子:  莲藕馅 -- ¥1.5
午餐菜单
茄汁面: 番茄鸡蛋汤面 -- ¥8.0
蒜汁面: 凉拌面 -- ¥7.0
臊子面: 羊肉臊子面 -- ¥15.0


代码解释

  1. MenuIterator 接口:自定义的迭代器接口,包含 hasNext()next() 方法,用于遍历菜单项。
  2. LunchMenuBreakfastMenu:分别使用 ArrayList 和数组实现菜单。两者都有一个 createIterator() 方法,返回相应的迭代器。
  3. LunchMenuIteratorBreakfastMenuIterator:分别实现了 MenuIterator 接口,用于遍历午餐和早餐菜单。
  4. Waitress:负责遍历午餐和早餐菜单并打印它们。
  5. IteratorPatternDemo:测试程序,创建午餐和晚、早餐菜单并打印出所有的菜单项。

总结

通过使用迭代器设计模式,我们可以轻松地遍历不同的菜单实现,并将它们合并输出,保持了代码的扩展性和灵活性,便于后续添加更多类型的菜单。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值