首页 > 其他分享 >HeadFirst设计模式-迭代器模式

HeadFirst设计模式-迭代器模式

时间:2022-12-01 18:23:35浏览次数:44  
标签:HeadFirst name 迭代 vegetarian price String 设计模式 public description

 

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

 

 

 

Java例子

package i_IteratorPattern_CompositePattern;

import java.util.Iterator;

public interface Menu {
    
    Iterator createIterator();

}

//-------------------------------------
package i_IteratorPattern_CompositePattern;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 煎饼屋菜单
 * 使用的是ArrayList存储
 */
public class PancakeHouseMenu implements Menu {
    
    ArrayList menuItems;
    
    public PancakeHouseMenu() {
        menuItems = new ArrayList();
        addItenm("Item1","Description1",true,2.99);
        addItenm("Item2","Description2",true,2.19);
        addItenm("Item3","Description3",false,3.49);
    }

    private void addItenm(String name, String description, boolean vegetarian, double price) {
        MenuItem item = new MenuItem(name,description,vegetarian,price);
        menuItems.add(item);
        
    }

    @Override
    public Iterator createIterator() {
        //return new PancakeHouseMenuIterator(menuItems);
        //或者 
        return menuItems.iterator();
    }

}


//-------------------------------------
package i_IteratorPattern_CompositePattern;

import java.util.Iterator;

/**
 * 午餐
 * 使用 数组 保存数据
 */
public class DinerMemu implements Menu {
    static final int MAX_ITEMS = 6;
    int numberOfItems = 0;
    MenuItem[] menuItems;
    
    public DinerMemu() {
        menuItems = new MenuItem[MAX_ITEMS];
        addItenm("Item11","Description11",true,2.99);
        addItenm("Item21","Description21",true,2.19);
        addItenm("Item31","Description31",false,3.49);
    }

    private void addItenm(String name, String description, boolean vegetarian, double price) {
        MenuItem item = new MenuItem(name,description,vegetarian,price);
        if(numberOfItems >= MAX_ITEMS) {
            System.out.println("Sorry, menu is full! Can not add item to menu.");
        }else {
            menuItems[numberOfItems] = item;
            numberOfItems++;
        }
        
    }
    
    @Override
    public Iterator createIterator() {
        return new DinerMenuIterator(menuItems);
    }

}

//-------------------------------------
package i_IteratorPattern_CompositePattern;

public class MenuItem {
    private String name;
    private String description;
    private boolean vegetarian;
    private double price;
    public MenuItem(String name, String description, boolean vegetarian, double price) {
        super();
        this.name = name;
        this.description = description;
        this.vegetarian = vegetarian;
        this.price = price;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public boolean isVegetarian() {
        return vegetarian;
    }
    public void setVegetarian(boolean vegetarian) {
        this.vegetarian = vegetarian;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "MenuItem [name=" + name + ", description=" + description + ", vegetarian=" + vegetarian + ", price="
                + price + "]";
    }
    
    
}

//-------------------------------------
package i_IteratorPattern_CompositePattern;

import java.util.Iterator;

public class DinerMenuIterator implements Iterator {
    
    MenuItem[] items;
    int position = 0;
    
    public DinerMenuIterator(MenuItem[] items) {
        this.items = items;
    }

    @Override
    public boolean hasNext() {
        if(position <0 || position >= items.length || null == items[position])
            return false;
        return true;
    }

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

    @Override
    public void remove() {
        if(position <=0) {
            throw new IllegalStateException("You can't remove an item until you've done at least one next().");
        }
        //position-1 是当前位置。因为执行了一次next()
        if(items[position-1] != null) {
            for (int i = position-1; i < items.length-1; i++) {
                items[i] = items[i+1];
            }
            items[items.length-1] = null;
        }
    }
}

//-------------------------------------
package i_IteratorPattern_CompositePattern;

import java.util.ArrayList;
import java.util.Iterator;

public class PancakeHouseMenuIterator implements Iterator {
    
    ArrayList items;
    Iterator it;
    
    public PancakeHouseMenuIterator(ArrayList items) {
        this.items = items;
        it = items.iterator();
    }

    @Override
    public boolean hasNext() {
        return it.hasNext();
    }

    @Override
    public Object next() {
        return it.next();
    }
    
    @Override
    public void remove() {
        it.remove();
    }

}

//-------------------------------------
package i_IteratorPattern_CompositePattern;

import java.util.Iterator;

public class Waitress {
    
    PancakeHouseMenu phm;
    DinerMemu dm;
    
    public Waitress(PancakeHouseMenu pancakeHouseMenu,DinerMemu dinerMemu) {
        phm = pancakeHouseMenu;
        dm = dinerMemu;
    }
    
    public void printMenu() {
        Iterator pIter = phm.createIterator();
        Iterator dIter = dm.createIterator();
        System.out.println("Menu\n-----\nBreakfast");
        printMenu(pIter);
        System.out.println("\nLunch");
        printMenu(dIter);
    }

    //使用一种方法遍历俩个不同存储方式的类
    private void printMenu(Iterator it) {
        while(it.hasNext()) {
            MenuItem m = (MenuItem) it.next();
            System.out.println(m);
        }
        
    }

}

//-------------------------------------
package i_IteratorPattern_CompositePattern;

public class TestDemo {

    public static void main(String[] args) {
        PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
        DinerMemu dinerMemu = new DinerMemu();
        Waitress waitress = new Waitress(pancakeHouseMenu, dinerMemu);
        waitress.printMenu();

    }

}
/*
Menu
-----
Breakfast
MenuItem [name=Item1, description=Description1, vegetarian=true, price=2.99]
MenuItem [name=Item2, description=Description2, vegetarian=true, price=2.19]
MenuItem [name=Item3, description=Description3, vegetarian=false, price=3.49]

Lunch
MenuItem [name=Item11, description=Description11, vegetarian=true, price=2.99]
MenuItem [name=Item21, description=Description21, vegetarian=true, price=2.19]
MenuItem [name=Item31, description=Description31, vegetarian=false, price=3.49]

 */
//-------------------------------------
View Code

 

 

 

 

**

标签:HeadFirst,name,迭代,vegetarian,price,String,设计模式,public,description
From: https://www.cnblogs.com/htj10/p/16942269.html

相关文章

  • 设计模式-适配器模式
    简单介绍适配器模式(AdapterPattern)将某个类的接口转换成客户端期望的另一个接口表示,主的目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同工作。其别名为......
  • 设计模式-桥接模式
    前言当我们要实现不同类型手机(全面屏、水滴屏、刘海屏)的不同品牌(华为、小米、Vivo)的各种操作时(开机、关机、上网,打电话)那么通过编程的实现如下!通过类图的形式如下!这种普......
  • Java设计模式面试题及答案(2023)
    Java面试题及答案(2023版),每道都是认真筛选出的高频面试题,助力大家能找到满意的工作!Java设计模式面试题及答案下载链接:全部面试题及答案PDF其他互联网大厂面试题1:阿里......
  • 设计模式
    设计模式的七大原则单一原则对类来说,即一个类应该只负责一个职责。如果类A负责两个不同的职责:职责1、职责2,当职责1需求变更而改变类A时,可能造成职责2执行错误,所以需要将......
  • IOS的delegate 设计模式,用法及利于其编写列表 UITableView(具体编写)
    delegate设计模式,用法及利于其编写列表UITableView(具体编写)在app中必须用到的设计模式,也是最常用的UITanView视图展示,协助管理,不管数据。简单列表编写self.view.backg......
  • 关于迭代Iterator
    List<ReplenishmentReportVO>missingStockProducts=NewArrayLIst<>;Iterator<ReplenishmentReportVO>itreator=missingStockProducts.iterator();while(itreator.hasNe......
  • 一篇文章让你彻底搞懂单例设计模式
    转自:​​单例设计模式-全网最牛文章​​下文是笔者编写的单例模式实现的八种方式,如下所示:单例模式的简介我们将一个类在当前进程中只有一个实例的这种模式,称之为“单例模式......
  • 设计模式-六大设计原则
    1单一职责原则(SingleResponsibilityPrinciple)2开闭原则(Open-ClosedPrinciple)3里氏代换原则(LiskovSubstitutionPrinciple)4依赖倒置原则(DependenceInversio......
  • 设计模式-23种设计模式
    1创建型1单例模式(SingletonPattern)2工厂模式(FactoryPattern)3抽象工厂模式(AbstractFactoryPattern)4建造者模式(BuilderPattern)5原型模式(PrototypePattern)......
  • 设计模式面试点汇总
    设计模式面试点汇总我们会在这里介绍我所涉及到的设计模式相关的面试点,本篇内容持续更新我们会介绍下述设计模式的相关面试点:单例模式单例模式下面我们来介绍单例模......