Java design mode---iterator pattern

Source: Internet
Author: User

1. Definition: Provides a way to sequentially access individual elements in an aggregated object without exposing their internal representations.

2. Example:
1) Requirements:
The menu (Pancake House menu, restaurant menu, and coffee menu) uses different collection accesses (arraylist,string[],hashtable), using the iterator mode to enable the waiter (waitress) to implement the print menu operation without relying on a specific menu.

2) Code implementation:

A) menu interface and entity classes:

Public interface Menu {public    Iterator createiterator ();} public class MenuItem {    private String name;    Private String desc;    Private Boolean vegetarian;    private double price;    Public MenuItem (string name, String desc, Boolean vegetarian, double price) {        this.name = name;        THIS.DESC = desc;        This.vegetarian = vegetarian;        This.price = Price;    }    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }    Public String GetDesc () {        return desc;    }    public void Setdesc (String desc) {        THIS.DESC = desc;    }    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;    }}

b) Menu specific implementation

public class Pancakehousemenu implements Menu {ArrayList MenuItems;        Public Pancakehousemenu () {MenuItems = new ArrayList ();        AddItem ("Kobe ' s Pancake breakfast", "pancakes with eggs", false,2.99);    AddItem ("Lilei ' s Pancake Breakfast", "Pancakes with Toast", false, 3.59);        } public void AddItem (string s, String s1, Boolean B, double v) {MenuItem MenuItem = new MenuItem (S,S1,B,V);    Menuitems.add (MenuItem);    } public Iterator Createiterator () {return menuitems.iterator ();    }}public class Dinnermenu implements Menu {private static final int max_size = 6;    int numofitems = 0;    Menuitem[] MenuItems;        Public Dinnermenu () {MenuItems = new menuitem[max_size];        AddItem ("Vegetarian BLT", "Bacon with Tomato", true,2.99);    AddItem ("Hot Dog", "with onions and cheese", false,3.05);        } private void AddItem (string s, String s1, Boolean B, double v) {MenuItem MenuItem = new MenuItem (S,S1,B,V); if (numofItems >= max_size) {System.err.println ("Sorry,menu is full!");            }else{Menuitems[numofitems]=menuitem;        Numofitems = Numofitems + 1;    }} @Override public Iterator createiterator () {return new dinermenuiterator (MenuItems);    }}public class Coffeemenu implements Menu {Hashtable MenuItems = new Hashtable ();    Public Coffeemenu () {AddItem ("Mocha", "Han Meimei order on couple of Mocha", false,3.01);        } private void AddItem (string s, String s1, Boolean B, double v) {MenuItem MenuItem = new MenuItem (S,S1,B,V);    Menuitems.put (Menuitem.getname (), MenuItem);    } @Override Public Iterator createiterator () {return menuitems.values (). Iterator (); }}

c) The array does not have a. Iterator () method that needs to be specifically implemented

public class Dinermenuiterator implements Iterator {    menuitem[] list;    int position = 0;    Public Dinermenuiterator (menuitem[] list) {        this.list = list;    }    @Override Public    Boolean hasnext () {        if (Position>=list.length | | list[position] = = null) {            return false;        }else{            return true;        }    }    @Override public    Object Next () {        MenuItem MenuItem = list[position];        Position = position + 1;        return menuItem;    }    @Override public    Void Remove () {        if (position <=0) {            throw new illegalstateexception (' Now '  Can not remove an item ");        }        if (list[position]! = null) {for            (int i=position-1;i< (list.length-1); i++) {                list[i] = list[i+1];            }            List[list.length-1]=null;}}}    

D) Waitress implementation

public class Waitress {Menu pancakehousemenu;    Menu Dinnermenu;    Menu Coffeemenu; Public Waitress (menu Pancakehousemenu, menu dinnermenu, menu coffeemenu) {this.pancakehousemenu = Pancakehousemenu        ;        This.dinnermenu = Dinnermenu;    This.coffeemenu = Coffeemenu;        } public void Printmenu () {Iterator pancakehouseiterator = Pancakehousemenu.createiterator ();        Iterator dinneriterator = Dinnermenu.createiterator ();        Iterator coffeeiterator = Coffeemenu.createiterator ();        System.out.println ("menu\n=====breakfast==start======");        Printmenu (Pancakehouseiterator);        System.out.println ("menu\n=====breakfast===end=====");        System.out.println ("menu\n=====lunch==start======");        Printmenu (Dinneriterator);        System.out.println ("menu\n=====lunch===end=====");        System.out.println ("menu\n=====coffee==start======");        Printmenu (Coffeeiterator);    System.out.println ("menu\n=====coffee===end=====");  }  private void Printmenu (Iterator Iterator) {while (Iterator.hasnext ()) {MenuItem MenuItem = (MenuItem) i            Terator.next ();            System.out.println (Menuitem.getname () + ",");            System.out.println (Menuitem.getprice () + ",");        System.out.println (Menuitem.getdesc ()); }    }}

e) Test class

public class Menutest {public    static void Main (string[] args) {        Pancakehousemenu pancakehousemenu = new Pancakeho Usemenu ();        Dinnermenu Dinnermenu = new Dinnermenu ();        Coffeemenu Coffeemenu = new Coffeemenu ();        Waitress waitress = new Waitress (pancakehousemenu,dinnermenu,coffeemenu);        Waitress.printmenu ();    }}

Test results:

Menu
=====breakfast==start======
Kobe ' s pancake breakfast,
2.99,
Pancakes with eggs
Lilei ' s pancake breakfast,
3.59,
Pancakes with Toast
Menu
=====breakfast===end=====
Menu
=====lunch==start======
Vegetarian BLT,
2.99,
Bacon with Tomato
Hot Dog,
3.05,
With onions and cheese
Menu
=====lunch===end=====
Menu
=====coffee==start======
Mocha,
3.01,
Han Meimei Order on couple of Mocha
Menu
=====coffee===end=====

Java design mode---iterator pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.