OO餐廳和OO煎餅屋合併了,但是餐廳菜單實現採用數組,煎餅屋採用arraylist。
總的菜單類是這樣的:
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 String getName(){
return name;
}
public String getDescription(){
return description;
}
public double getPrice(){
return price;
}
public boolean isVegetarian(){
return vegetarian;
}
}
兩家店的菜單實現:
import java.util.ArrayList;
public class PancakeHouseMenu {
ArrayList menuItems;
public PancakeHouseMenu(){
menuItems=new ArrayList<>();
addItem("KB'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 made with fresh blueberry", true, 3.49);
addItem("Waffles", "Waffles with your choice of blueberry or strawberry", 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 ArrayList getMenuItems(){
return menuItems;
}
}
public class DinerMenu {
static final int MAX_ITEMS=6;
int numOfItems=0;
MenuItem[] menuItems;
public DinerMenu(){
menuItems=new MenuItem[MAX_ITEMS];
addItem("Vegetarian BLT", "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 hotdog with saurkraut ,relesh,onions,topped with cheese",false,3.05);
}
public void addItem(String name,String description,boolean vegetarian,double price){
MenuItem menuItem=new MenuItem(name, description, vegetarian, price);
if(numOfItems>=MAX_ITEMS){
System.err.println("Sorry,menu is full!Can't add item to menu.");
}else {
menuItems[numOfItems]=menuItem;
numOfItems++;
}
}
public MenuItem[] getMenuItem(){
return menuItems;
}
}
兩種不同的菜單表現方式會帶來很多問題,比如女招待就遇到問題,printMenu()的話得調用兩個menu的getMenuItem()方法,但是返回類型不一樣,就得循壞一一列出item。
現在我們創建一個對象,將它稱爲迭代器Iterator,來封裝“遍歷集合內的每個對象的過程”。
Iterator iterator=breakfastMenu.createIterator();
while(iterator.hasNext())
MenuItem menuItem=(MenuItem)iterator.next();
Iterator iterator=lunchMenu.createIterator();
while(iterator.hasNext())
MenuItem menuItem=(MenuItem)iterator.next();
這時候女招待問題就可以解決了:
void printMenu(Iterator iterator){
while(iterator.hasNext()){
MenuItem menuItem=(MenuItem)iterator.next();
//其他操作
}
}
Java自帶Iterator接口,ArrayList支持Iterator()方法,數組不支持,要自己定義,和之前不一樣的是要實現remove()方法。
如果菜單又有子菜單怎麼辦?
定義組合模式,允許你將對象組合成樹形結構來表現整體/部分層次結構。
首先實現菜單組件:
public abstract class MenuComponent {
public void add(MenuComponent menuComponent){
throw new UnsupportedOperationException();
}
public void remove(MenuComponent menuComponent){
throw new UnsupportedOperationException();
}
public MenuComponent getCHild(int i){
throw new UnsupportedOperationException();
}
public String getName(){
throw new UnsupportedOperationException();
}
public String getDescription(){
throw new UnsupportedOperationException();
}
public double getPrice(){
throw new UnsupportedOperationException();
}
public boolean isVegetarian(){
throw new UnsupportedOperationException();
}
public void print(){
throw new UnsupportedOperationException();
}
}
接下來是菜單項類:
public class MenuItem extends MenuComponent{
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 String getName(){
return name;
}
public String getDescription(){
return description;
}
public double getPrice(){
return price;
}
public boolean isVegetarian(){
return vegetarian;
}
public void print(){
System.out.println(" "+getName());
if(isVegetarian())
System.out.println("(v)");
System.out.println(","+getPrice());
System.out.println(" --"+getDescription());
}
}
實現組合菜單:
import java.util.ArrayList;
public class Menu extends MenuComponent{
ArrayList menuComponents=new ArrayList<>();
String name;
String description;
public Menu(String name,String description){
this.name=name;
this.description=description;
}
public void add(MenuComponent menuComponent){
menuComponent.add(menuComponent);
}
public void remove(MenuComponent menuComponent){
menuComponent.remove(menuComponent);
}
public MenuComponent getChild(int i){
return (MenuComponent)menuComponents.get(i);
}
public String getName(){
return name;
}
public String getDescription(){
return description;
}
//不覆蓋getPrice()和isVegetarian()方法
public void print(){
System.out.println("\n"+getName());
System.out.println(","+getDescription());
System.out.println("------------------------------");
}
}
修正print()方法:
public void print(){
System.out.println("\n"+getName());
System.out.println(","+getDescription());
System.out.println("------------------------------");
Iterator iterator=menuComponents.iterator();
while(iterator.hasNext()){
MenuComponent menuComponent=(MenuComponent)iterator.next();
menuComponent.print();
}
}
接下來是一種組合迭代器:
我們要爲每個組件都加上createIterator()方法:
public class Menu extends MenuComponents{
//其他不需要改變
public Iterator createIterator(){
return new CompositeIterator(menuComponents.iterator());
}
}
public class MenuItem extends MenuComponent{
//其他不需要改變
public Iterator createIterator(){
return new NullIerator();
}
}
迭代器:
import java.util.Iterator;
import java.util.Stack;
public class CompositeIterator implements Iterator{
Stack stack=new Stack<>();
public CompositeIterator(Iterator iterator){
stack.push(iterator);
}
public Object next(){
if(hasNext()){
Iterator iterator=(Iterator)stack.peek();
MenuComponent component=(MenuComponent)iterator.next();
if(component instanceof Menu){
stack.push(component.createIterator());
}
return component;
}else {
return null;
}
}
public boolean hasNext(){
if(stack.empty())
return false;
else {
Iterator iterator=(Iterator)stack.peek();
if(!iterator.hasNext()){
stack.pop();
return hasNext();
}else {
return true;
}
}
}
public void remove(){
throw new UnsupportedOperationException();
}
}
空迭代器:
import java.util.Iterator;
public class NullIterator implements Iterator{
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public Object next() {
// TODO Auto-generated method stub
return null;
}
@Override
public void remove() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException();
}
}
組合模式解耦了客戶程序與複雜元素內部結構,從而使客戶程序可以像處理簡單元素一樣來處理複雜元素。