在这个例子中,我们将实现两个菜单,一个午餐菜单使用 ArrayList
实现,另一个早餐菜单使用数组实现。然后,使用迭代器模式遍历两个菜单,并将它们合并到一起,最后输出所有菜单项。
步骤:
- 创建迭代器接口:定义用于遍历集合的标准接口。
- 实现午餐菜单和早餐菜单类 :分别使用
ArrayList
和数组实现菜单。 - 创建迭代器实现类:实现菜单各自的迭代器。
- 实现菜单的遍历与合并。
代码实现
1. 定义 Iterator
接口
java
import java.util.Iterator;
public interface MenuIterator<T> {
boolean hasNext();
T next();
}
2. 创建 MenuItem
类表示菜单项
java
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
存储菜单项
java
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
类
java
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
类,使用数组存储菜单项
java
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
类
java
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
类用于遍历并打印合并后的菜单
java
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. 测试代码
java
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();
}
}
输出结果
plaintext
早餐菜单:
胡辣汤: 素胡辣汤 -- ¥3.0
油条: 按斤称 -- ¥2.0
包子: 莲藕馅 -- ¥1.5
午餐菜单
茄汁面: 番茄鸡蛋汤面 -- ¥8.0
蒜汁面: 凉拌面 -- ¥7.0
臊子面: 羊肉臊子面 -- ¥15.0
代码解释
MenuIterator
接口 :自定义的迭代器接口,包含hasNext()
和next()
方法,用于遍历菜单项。LunchMenu
和BreakfastMenu
类 :分别使用ArrayList
和数组实现菜单。两者都有一个createIterator()
方法,返回相应的迭代器。LunchMenuIterator
和BreakfastMenuIterator
:分别实现了MenuIterator
接口,用于遍历午餐和早餐菜单。Waitress
类:负责遍历午餐和早餐菜单并打印它们。IteratorPatternDemo
:测试程序,创建午餐和晚、早餐菜单并打印出所有的菜单项。
总结
通过使用迭代器设计模式,我们可以轻松地遍历不同的菜单实现,并将它们合并输出,保持了代码的扩展性和灵活性,便于后续添加更多类型的菜单。