当前位置: 首页 > news >正文

迭代器设计模式

在这个例子中,我们将实现两个菜单,一个午餐菜单使用 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;}@Overridepublic 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;}@Overridepublic boolean hasNext() {return position < items.size();}@Overridepublic 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;}@Overridepublic boolean hasNext() {return position < items.length && items[position] != null;}@Overridepublic 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:测试程序,创建午餐和晚、早餐菜单并打印出所有的菜单项。

总结

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


http://www.mrgr.cn/news/20242.html

相关文章:

  • 淘宝订单 API 接口:获取淘宝平台数据的 api 接口(电商 ERP 订单对接方案)
  • docker 启动ElasticSearch
  • Spring Boot 2.0 解决跨域问题:WebMvcConfiguration implements WebMvcConfigurer
  • c++ string之字符替换、string的swap交换
  • Nacos配置的优先级
  • Mysql梳理1——数据库概述(上)
  • C++ 原子变量atomic variable
  • webCppCluster
  • vs2022 如何去掉 错误|警告的波形曲线 绿色波浪线
  • Python知识点:Python研发中,如何使用JIRA进行项目管理
  • uniapp壁纸项目笔记
  • QT:动态库与静态库的生成和使用
  • HTML 文本标签
  • 财税系统集成Java发票查验接口代码示例
  • 零基础国产GD32单片机编程入门(十四)内部RTC实时时钟及实战含源码
  • 计算机大专生没有出路了吗?听听过来人怎么讲!
  • 面对市场变革,企业数字化转型的必要性与挑战何在?
  • 竟然有50万个使用Flutter开发的应用了,这也太牛了!
  • 如何缩放C#中的img
  • 司法军警行业ITSM案例分析报告