JAVA design pattern Iterator design pattern

First, the iterator pattern Introduction

Iterator mode is also called iteration mode is one of the patterns of behavior, it is the internal object container that is included in the Commission give access outside the class, use Iterator (traverse) design pattern traversal visits in order.

Second, the roles and responsibilities of the iterative mode

Iterator (iterator interface): This interface must be defined to achieve iterative functions such as providing a minimum set of methods defined hasNext () and next () method.

ConcreteIterator (iterator implementation class): iterator interface Iterator implementation class. It can be implemented depending on the circumstances.

Aggregate (trap port): definition of basic functions provide similar Iterator iterator () method.

concreteAggregate (container implementation class): container interface implementation class. We must implement Iterator iterator () method.

Third, the specific iterator pattern implementation

There is a restaurant and a pancake house you want to merge, but each boss want to use their own menu mode, the restaurant using an array of menus, and pancake house using ArrayList form.

Although realization is not the same form, but the content of each menu item is the same, we look at the design of the menu item, more conventional lists the name, description, whether it is vegetarian, price:

// An highlighted block

package design.iterator.gys.noiterator;

public class MenuItem {

 String name;

 String desc;

 boolean vegetarian;

 double price;

 public MenuItem(String name, String desc, boolean vegetarian, double price) {

  super();

  this.name = name;

  this.desc = desc;

  this.vegetarian = vegetarian;

  this.price = price;

 }

 public String getName() {

  return name;

 }

 public String getDesc() {

  return desc;

 }

 public boolean isVegetarian() {

  return vegetarian;

 }

 public double getPrice() {

  return price;

 }

}

1 , do not use an iterative mode

Beginning Bosses different opinions, unwilling to change their menus, then we will see a menu bar two.

restaurant:

// An highlighted block

package design.iterator.gys.noiterator;

public class DinnerMenu {

 static final int Max=6;

 int numberOfItem=0;

 MenuItem[] menu;

 public DinnerMenu() {

  menu=new MenuItem[Max];

  addItem("a2","b2",true,6.99);

  addItem("c2","d2",false,7.99);

  addItem("e2","f2",true,8.99);

  addItem("g2","h2",false,9.99);

 }

 public void addItem(String name,String desc,boolean vegetarian,double price){

  MenuItem mt=new MenuItem(name, desc, vegetarian, price);

  if(numberOfItem>=Max)

   System.out.println("menu is full");

  else

  {

   menu[numberOfItem]=mt;

   numberOfItem++;

  }

 }

 public MenuItem[] getMenu() {

  return menu;

 }

}

Pancake House:

// An highlighted block

package design.iterator.gys.noiterator;

import java.util.ArrayList;

public class PancakeHouseMenu {

 ArrayList menu;

 public PancakeHouseMenu() {

  menu=new ArrayList();

  addItem("a1","b1",true,1.99);

  addItem("c1","d1",false,2.99);

  addItem("e1","f1",true,3.99);

  addItem("g1","h1",false,4.99);

 }

 public void addItem(String name,String desc,boolean vegetarian,double price){

  MenuItem mt=new MenuItem(name, desc, vegetarian, price);

  menu.add(mt);

 }

 public ArrayList getMenu() {

  return menu;

 }

}

Sure enough different styles ah, Oh too busy waiter, was to distinguish the two menus, or accidentally may be necessary to make a fool of. To get the first form of a menu, but also not the same, the waiter both need to iterate respectively:

// An highlighted block

package design.iterator.gys.noiterator;

import java.util.ArrayList;

public class Waitress {

 private PancakeHouseMenu menu1;

 private DinnerMenu menu2;

 public Waitress(PancakeHouseMenu menu1, DinnerMenu menu2) {

  super();

  this.menu1 = menu1;

  this.menu2 = menu2;

 }

 public void printMenu()

 {

  ArrayList breakfastMenu=menu1.getMenu();

  MenuItem[] lunchMenu=menu2.getMenu();

  System.out.println("***PancakeHouseMenu***");

  for(int i=0;i<breakfastMenu.size();i++)

   {

    MenuItem mt=(MenuItem)breakfastMenu.get(i);

    System.out.println(mt.getName()+" "+mt.getDesc()+" "+mt.getPrice());

   }

  System.out.println("***DinnerMenu***");

  for(int i=0;i<lunchMenu.length && lunchMenu[i]!=null;i++)

  {

   MenuItem mt=lunchMenu[i];

   System.out.println(mt.getName()+" "+mt.getDesc()+" "+mt.getPrice());

  }

 }

}

Take a look at the operation of the restaurant:

// An highlighted block

package design.iterator.gys.noiterator;

public class Test {

 public static void main(String[] args) {

  // TODO Auto-generated method stub

  PancakeHouseMenu m=new PancakeHouseMenu();

  DinnerMenu d=new DinnerMenu();

  Waitress w=new Waitress(m, d);

  w.printMenu();

 }

}

// An highlighted block

***PancakeHouseMenu***

a1 b1 1.99

c1 d1 2.99

e1 f1 3.99

g1 h1 4.99

***DinnerMenu***

a2 b2 6.99

c2 d2 7.99

e2 f2 8.99

g2 h2 9.99

Well, not out of any major problems, waiters can be considered for dedicated a.

So the question is, the waiter takes two cycles to traverse forex rebate menu items, menu and staff, and specific bundled with more features can not be expanded, and two internal menus fully exposed to the waiter, though we do not want to think he is a bad guy, but it just in case.

2 , using an iterative mode

Design

Class Design

In order to reduce the work attendant, so she only familiar with one interface will be able to print out all the menu.

First, we abstract the iterator class, but still left to cook the contents of the menu change, and here we use the iterator form of self-definition.

// An highlighted block

package design.iterator.gys.iterator;

public interface Iterator {

 public boolean hasNext();

 public Object next();

}

Here is the restaurant iterator, because the menu is an array type, we use the array index is designed, as is the array of fixed-length, we must not only determine whether the index should exceed determine whether the contents of the array is empty:

// An highlighted block

package design.iterator.gys.iterator;

import design.iterator.gys.noiterator.MenuItem;

public class DinnerMenuIterator implements Iterator{

 private MenuItem[] menu;

 private int start=0;

 public DinnerMenuIterator(MenuItem[] menu) {

  super();

  this.menu = menu;

 }

 @Override

 public boolean hasNext() {

  // TODO Auto-generated method stub

  if(start<menu.length&&menu[start]!=null)

   return true;

  else

   return false;

 }

 @Override

 public Object next() {

  // TODO Auto-generated method stub

  return menu[start++];

 }

}

Pancake House menu using ArrlyList implemented, variable length belongs:

// An highlighted block

package design.iterator.gys.iterator;

import java.util.ArrayList;

public class PancakeHouseMenuIterator implements Iterator{

 private ArrayList menu;

 private int start=0;

 public PancakeHouseMenuIterator(ArrayList menu) {

  super();

  this.menu = menu;

 }

 @Override

 public boolean hasNext() {

  // TODO Auto-generated method stub

  if(start<menu.size())

   return true;

  else

   return false;

 }

 @Override

 public Object next() {

  // TODO Auto-generated method stub

  return menu.get(start++);

 }

}

At this point, we realized the iterator two menus. So then we have to be processed within the menu to allow the waiter to get the iterator.

restaurant:

// An highlighted block

package design.iterator.gys.iterator;

import design.iterator.gys.noiterator.MenuItem;

public class DinnerMenu {

 static final int Max=6;

 int numberOfItem=0;

 MenuItem[] menu;

 public DinnerMenu() {

  menu=new MenuItem[Max];

  addItem("a2","b2",true,6.99);

  addItem("c2","d2",false,7.99);

  addItem("e2","f2",true,8.99);

  addItem("g2","h2",false,9.99);

 }

 public void addItem(String name,String desc,boolean vegetarian,double price){

  MenuItem mt=new MenuItem(name, desc, vegetarian, price);

  if(numberOfItem>=Max)

   System.out.println("menu is full");

  else

  {

   menu[numberOfItem]=mt;

   numberOfItem++;

  }

 }

 public Iterator createIterator() {

  return new DinnerMenuIterator(menu);

 }

}

Pancake House:

// An highlighted block

package design.iterator.gys.iterator;

import java.util.ArrayList;

import design.iterator.gys.noiterator.MenuItem;

public class PancakeHouseMenu {

 ArrayList menu;

 public PancakeHouseMenu() {

  menu=new ArrayList();

  addItem("a1","b1",true,1.99);

  addItem("c1","d1",false,2.99);

  addItem("e1","f1",true,3.99);

  addItem("g1","h1",false,4.99);

 }

 public void addItem(String name,String desc,boolean vegetarian,double price){

  MenuItem mt=new MenuItem(name, desc, vegetarian, price);

  menu.add(mt);

 }

 public Iterator createIterator() {

  return new PancakeHouseMenuIterator(menu);

 }

}

Look at how to adapt to the new staff :

// An highlighted block

package design.iterator.gys.iterator;

import design.iterator.gys.noiterator.MenuItem;

public class Waitress {

 PancakeHouseMenu menu1;

 DinnerMenu memu2;

 public Waitress(PancakeHouseMenu menu1, DinnerMenu memu2) {

  super();

  this.menu1 = menu1;

  this.memu2 = memu2;

 }

 public void printMenu() {

  Iterator i1=menu1.createIterator();

  printMenu(i1);

  System.out.println("----------");

  Iterator i2=memu2.createIterator();

  printMenu(i2);

 }

 public void printMenu(Iterator iterator)

 {

  while(iterator.hasNext()) {

   MenuItem m=(MenuItem)iterator.next();

   System.out.println(m.getName()+" "+m.getDesc()+" "+m.getPrice());

  }

 }

}

So easy! Now the waiter just need to call the iterator each menu, you can achieve traversal of the menu, you did not have to know the implementation of the internal mechanism.

// An highlighted block

package design.iterator.gys.iterator;

public class Tset {

 public static void main(String[] args) {

  // TODO Auto-generated method stub

  PancakeHouseMenu p=new PancakeHouseMenu();

  DinnerMenu d=new DinnerMenu();

  Waitress w=new Waitress(p, d);

  w.printMenu();

  System.out.println("**********");

  Iterator i=d.createIterator();

  w.printMenu(i);

 }

}

Test Results:

// An highlighted block

a1 b1 1.99

c1 d1 2.99

e1 f1 3.99

g1 h1 4.99

----------

a2 b2 6.99

c2 d2 7.99

e2 f2 8.99

g2 h2 9.99

**********

a2 b2 6.99

c2 d2 7.99

e2 f2 8.99

g2 h2 9.99

Good results, and can be used alone to a menu output.

————————————————

Original link: https://blog.csdn.net/qq_22118991/article/details/84968047

Guess you like

Origin www.cnblogs.com/benming/p/11725502.html