首页 > 其他分享 >迭代器模式

迭代器模式

时间:2022-12-12 16:34:29浏览次数:33  
标签:String 迭代 menuItems addItem 模式 menuItem MenuItem public


定义迭代器模式

迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。

迭代器模式_java

实例:

三个餐厅要合并,每个餐厅的菜单采用不同和数据结构实现,服务员需要获取所有的菜单信息,但是处理逻辑又不能过于复杂。

/**
* 菜单项
*/
public class MenuItem {
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 double getPrice() {
return price;
}

public boolean isVegetarian() {

return vegetarian;
}

public String getDescription() {

return description;
}

public String getName() {

return name;
}
}
/**
* 菜单接口
*/
public interface Menu {
Iterator<MenuItem> createIterator();
}
/**
* 午餐菜单
*/
public class DinerMenu implements Menu{
@Override
public Iterator<MenuItem> createIterator() {
return new DinerMenuIterator(menuItems);
}
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);
}
public void addItem(String name,String description,boolean vegerarian,double price){
MenuItem menuItem=new MenuItem(name,description,vegerarian,price);
if(numberOfItems>=MAX_ITEMS){
System.out.println("Sorry,menu is full! Can't add item to menu");
}else{
menuItems[numberOfItems]=menuItem;
numberOfItems++;
}
}
}
/**
* 早餐菜单
*/
public class PancakeHouseMenu implements Menu{
@Override
public Iterator<MenuItem> createIterator() {
return menuItems.iterator();
}

ArrayList<MenuItem> menuItems;
public PancakeHouseMenu(){
menuItems=new ArrayList<>();
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 make 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 class CafeMenu implements Menu{
@Override
public Iterator<MenuItem> createIterator() {
return menuItems.values().iterator();
}

Hashtable<String,MenuItem> menuItems=new Hashtable<>();
public CafeMenu(){
addItem("Veggie Burger and Air Fries","Veggie burger on a whole wheat bun, lettuce,tomato,and fries",true,3.99);
addItem("Soup of the day","A cup of the soup of the day, with a side salad",false,3.69);
addItem("Burrito","A large burrito ,with whole pinto beans,salsa,guacamole",true,4.29);
}
public void addItem(String name,String description,boolean vegetarian,double price){
MenuItem menuItem=new MenuItem(name,description,vegetarian,price);
menuItems.put(menuItem.getName(),menuItem);
}

}
/**
* 迭代器具体实现类,用于遍历午餐
*/
public class DinerMenuIterator implements Iterator<MenuItem>{
private MenuItem[] menuItems;
private int position=0;
public DinerMenuIterator(MenuItem[] menuItems){
this.menuItems=menuItems;
}
@Override
public boolean hasNext() {
if(position>=menuItems.length||menuItems[position]==null){
return false;
}else {
return true;
}
}

@Override
public MenuItem next() {
MenuItem menuItem=menuItems[position];
position++;
return menuItem;
}
}
/**
* 服务员类
*/
public class Waitress {
ArrayList<Menu> menus;
public Waitress(ArrayList<Menu> menus){
this.menus=menus;
}
public void printMenu(){
Iterator<Menu> menuItemIterator=menus.iterator();
while(menuItemIterator.hasNext()){
Menu menu=menuItemIterator.next();
printMenu(menu.createIterator());
}
}
private void printMenu(Iterator<MenuItem> iterator){
System.out.println();
while (iterator.hasNext()){
MenuItem menuItem=iterator.next();
System.out.print(menuItem.getName()+", ");
System.out.print(menuItem.getPrice()+" -- " );
System.out.println(menuItem.getDescription());
}
System.out.println();
}
}

测试:

public class Test {
public static void main(String[] args){
Menu dinerMenu=new DinerMenu();
Menu pancakeMenu=new PancakeHouseMenu();
Menu cafeMenu=new CafeMenu();
ArrayList<Menu> menus=new ArrayList<>();
menus.add(dinerMenu);
menus.add(pancakeMenu);
menus.add(cafeMenu);
Waitress waitress=new Waitress(menus);
waitress.printMenu();
}
}

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




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 make with fresh blueberries


Waffles, 3.59 -- Waffles,with your choice of blueberries or strawberries




Soup of the day, 3.69 -- A cup of the soup of the day, with a side salad


Burrito, 4.29 -- A large burrito ,with whole pinto beans,salsa,guacamole


Veggie Burger and Air Fries, 3.99 -- Veggie burger on a whole wheat bun, lettuce,tomato,and fries


标签:String,迭代,menuItems,addItem,模式,menuItem,MenuItem,public
From: https://blog.51cto.com/u_12026373/5930805

相关文章

  • 代理模式
    组成:抽象角色:通过接口或抽象类声明真实角色实现的业务方法。代理角色:实现抽象角色,是真实角色的代理,通过真实角色的业务逻辑方法来实现抽象方法,并可以附加......
  • 观察者模式
    观察者模式的定义定义对象间的一种一对多的依赖关系。当一个对象的状态发生改变时,所有依赖它的对象都得到通知并被自动更新。推模型目标对象主动向观察者推送目标的详细信息......
  • 模板方法模式
    1.概述定义一个操作中的算法的骨架,而将步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义算法的某些特定步骤。2.模式中的角色2.1抽象类(Ab......
  • 适配器模式
    适配器模式定义:适配器模式将一个类的接口,转化成客户期望的另一个接口。使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作。适配器分类1组合采用组合方式的适配......
  • rxjs究竟是观察者模式还是发布订阅模式
    rxjs源代码Subscriber.ts,里面对Subscriber的定义:exportclassSubscriberextendsSubscriptionimplementsObserverrxjs里面观察者模式还是发布订阅模式?观察者模式和发......
  • DataGrid CRUD(MVVM模式)
    引用程序包:MvvmLight实体类:publicclassStudent:ViewModelBase{privateintid;privatestringname;publicintId{......
  • 采用无线网桥做监控有什么优势?无线网桥的组网模式有哪些?
     无线网桥顾名思义就是无线网络的桥接,它利用无线传输方式实现在两个或多个网络之间相互连接,无线网桥从通信机制上分为电路型网桥和数据型网桥。无线网桥通常用来应用在无线......
  • 【秒杀购物商城业务服务】「分布式架构服务」盘点中间件服务的高可用模式及集群技术的
    秒杀购物商城业务服务-分布式架构介绍基于MySQL数据库集群技术实现服务的高可用基于Tomcat的集群负载机制实现Tomcat服务器的高可用基于Nginx负载均衡机制实现负载均衡......
  • 【秒杀购物商城业务服务】「分布式架构服务」盘点中间件服务的高可用模式及集群技术的
    秒杀购物商城业务服务-分布式架构介绍基于MySQL数据库集群技术实现服务的高可用基于Tomcat的集群负载机制实现Tomcat服务器的高可用基于Nginx负载均衡机制实现负载均衡(介绍......
  • 前端_js设计模式
    什么是设计模式1.什么是设计模式设计模式是前人总结出的,解决开发中某类问题的方法;我们在过去的代码编写中已经接触过很多的设计模式了,只不过当时咱们不知道这就......