## 1. 什么是组合设计模式
## 2. 怎么使用组合设计模式
## 3. 代码
public interface Iterator {
public boolean hasNext();
public Object next();
}
public class MenuItem {
private String name;
private int price;
private boolean vegetarian;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public boolean isVegetarian() {
return vegetarian;
}
public void setVegetarian(boolean vegetarian) {
this.vegetarian = vegetarian;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public MenuItem(String name, int price, boolean vegetarian, String description) {
this.name = name;
this.price = price;
this.vegetarian = vegetarian;
this.description = description;
}
}
public class DinerMenuIterator implements Iterator{
MenuItem[] items;
int position= 0;
public DinerMenuIterator( MenuItem[] items){
this.items = items;
}
@Override
public boolean hasNext() {
if(position>items.length ||items[position]==null) {
return false;
}else{
return true;
}
}
@Override
public Object next() {
MenuItem menuItem = items[position];
position+=1;
return menuItem;
}
}
public class DinerMenu implements Iterator{
static final int MAX_VALUE = 6;
int numberOfItems = 0;
MenuItem[] menuItems;
public DinerMenu(){
menuItems = new MenuItem[MAX_VALUE];
}
public void addItem(String name,String description,int price,boolean vegetarica){
MenuItem item = new MenuItem(name,price,vegetarica,description);
if(numberOfItems >= MAX_VALUE){
System.out.println("Sorry menu is full");
}else{
menuItems[numberOfItems] = item;
numberOfItems = numberOfItems+1;
}
}
public MenuItem[] getMenuItems() {
return menuItems;
}
@Override
public boolean hasNext() {
return false;
}
@Override
public Object next() {
return null;
}
}
public class PancakeHouseMemuIterator implements Iterator{
List<MenuItem> list ;
PancakeHouseMemuIterator( List<MenuItem> list){
this.list = list;
}
int position = 0;
@Override
public boolean hasNext() {
if(position>list.size()-1 ||list.get(position)==null) {
return false;
}else{
return true;
}
}
@Override
public Object next() {
MenuItem menuItem = list.get(position);
position+=1;
return menuItem;
}
}
public class PancakeHouseMenu{
ArrayList menuList;
public PancakeHouseMenu(){
menuList = new ArrayList();
addItem("西红柿","炒鸡蛋",5,false);
addItem("西红柿1","炒鸡蛋1",5,false);
addItem("西红柿2","炒鸡蛋2",5,false);
addItem("西红柿3","炒鸡蛋3",5,false);
addItem("西红柿4","炒鸡蛋4",5,false);
}
public void addItem(String name,String description,int price,boolean vegetarica){
MenuItem item = new MenuItem(name,price,vegetarica,description);
menuList.add(item);
}
public ArrayList getMenuList(){
return menuList;
}
}