head first---------composite design pattern
组合模式:允许你将整体对象组装成树形结构来表现“整体/部分”的层次结构。组合能让客户以一致的方式处理个别的对象和对象组合。
package com.clark.compositpattern;
import java.util.ArrayList;
import java.util.Iterator;
/**
* Menu extends Component
* @author Administrator
*
*/
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){
menuComponents.add(menuComponent);
}
public void remove(MenuComponent menuComponent){
menuComponents.remove(menuComponent);
}
public MenuComponent getChild(int i){
return (MenuComponent)menuComponents.get(i);
}
public String getName(){
return name;
}
public String getDescription(){
return description;
}
public void print(){
System.out.print("\n"+getName());
System.out.println(", "+getDescription());
System.out.println("--------------------------");
//if menu contains menuItem,iterator print menuItem
Iterator iterator=menuComponents.iterator();
while(iterator.hasNext()){
MenuComponent menuComponent=(MenuComponent)iterator.next();
menuComponent.print();
}
}
@Override
public Iterator createIterator() {
return new CompositeIterator(menuComponents.iterator());
}
}
import java.util.ArrayList;
import java.util.Iterator;
/**
* Menu extends Component
* @author Administrator
*
*/
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){
menuComponents.add(menuComponent);
}
public void remove(MenuComponent menuComponent){
menuComponents.remove(menuComponent);
}
public MenuComponent getChild(int i){
return (MenuComponent)menuComponents.get(i);
}
public String getName(){
return name;
}
public String getDescription(){
return description;
}
public void print(){
System.out.print("\n"+getName());
System.out.println(", "+getDescription());
System.out.println("--------------------------");
//if menu contains menuItem,iterator print menuItem
Iterator iterator=menuComponents.iterator();
while(iterator.hasNext()){
MenuComponent menuComponent=(MenuComponent)iterator.next();
menuComponent.print();
}
}
@Override
public Iterator createIterator() {
return new CompositeIterator(menuComponents.iterator());
}
}
package com.clark.compositpattern;
import java.util.Iterator;
import java.util.Stack;
public class CompositeIterator implements Iterator {
Stack stack=new Stack();
public CompositeIterator(Iterator iterator){
stack.push(iterator);
}
@Override
public boolean hasNext() {
if (stack.empty()) {
return false;
} else {
Iterator iterator = (Iterator) stack.peek();
if (!iterator.hasNext()) {
stack.pop();
return hasNext();
} else {
return true;
}
}
}
@Override
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;
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
import java.util.Iterator;
import java.util.Stack;
public class CompositeIterator implements Iterator {
Stack stack=new Stack();
public CompositeIterator(Iterator iterator){
stack.push(iterator);
}
@Override
public boolean hasNext() {
if (stack.empty()) {
return false;
} else {
Iterator iterator = (Iterator) stack.peek();
if (!iterator.hasNext()) {
stack.pop();
return hasNext();
} else {
return true;
}
}
}
@Override
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;
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
package com.clark.compositpattern;
import java.util.Iterator;
/**
* Menu Component ,Every Menu and MenuItem will implements It
* @author Administrator
* we called it conmponent design pattern
*/
public abstract class MenuComponent {
//add method
public void add(MenuComponent menuComponent){
throw new UnsupportedOperationException();
}
public void remove(MenuComponent menuComponent){
throw new UnsupportedOperationException();
}
public MenuComponent getChild(int i){
throw new UnsupportedOperationException();
}
//below this method is use by menuItem
public String getName(){
throw new UnsupportedOperationException();
}
public String getDescription(){
throw new UnsupportedOperationException();
}
public double getPrice(){
throw new UnsupportedOperationException();
}
public boolean isVegetarian(){
throw new UnsupportedOperationException();
}
//all the time to use by menu and menuItem
public void print(){
throw new UnsupportedOperationException();
}
public abstract Iterator createIterator();
}
import java.util.Iterator;
/**
* Menu Component ,Every Menu and MenuItem will implements It
* @author Administrator
* we called it conmponent design pattern
*/
public abstract class MenuComponent {
//add method
public void add(MenuComponent menuComponent){
throw new UnsupportedOperationException();
}
public void remove(MenuComponent menuComponent){
throw new UnsupportedOperationException();
}
public MenuComponent getChild(int i){
throw new UnsupportedOperationException();
}
//below this method is use by menuItem
public String getName(){
throw new UnsupportedOperationException();
}
public String getDescription(){
throw new UnsupportedOperationException();
}
public double getPrice(){
throw new UnsupportedOperationException();
}
public boolean isVegetarian(){
throw new UnsupportedOperationException();
}
//all the time to use by menu and menuItem
public void print(){
throw new UnsupportedOperationException();
}
public abstract Iterator createIterator();
}
package com.clark.compositpattern;
import java.util.Iterator;
/**
* MenuItem extends MenuComponent
* @author Administrator
*
*/
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 boolean isVegetarian() {
return vegetarian;
}
public double getPrice() {
return price;
}
//print method
public void print(){
System.out.println(" "+getName());
if(isVegetarian()){
System.out.print(" "+vegetarian);
}
System.out.println(","+getPrice());
System.out.println(" -----"+getDescription());
}
@Override
public Iterator createIterator() {
return new NullIterator();
}
}
import java.util.Iterator;
/**
* MenuItem extends MenuComponent
* @author Administrator
*
*/
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 boolean isVegetarian() {
return vegetarian;
}
public double getPrice() {
return price;
}
//print method
public void print(){
System.out.println(" "+getName());
if(isVegetarian()){
System.out.print(" "+vegetarian);
}
System.out.println(","+getPrice());
System.out.println(" -----"+getDescription());
}
@Override
public Iterator createIterator() {
return new NullIterator();
}
}
package com.clark.compositpattern;
/**
* Test class
*
* @author Administrator
*
*/
public class MenuTestDrive {
public static void main(String[] args) {
// first make up MenuComponent component
MenuComponent pancakeHouseMenu = new Menu("Pancake House Menu",
"Breakfast");
MenuComponent dinerMenu = new Menu("Diner Menu", "Lunch");
MenuComponent cafeMenu = new Menu("Cafe Menu", "Diner");
MenuComponent dessertMenu = new Menu("Dessert Menu",
"Dessert of Course!");
MenuComponent coffeeMenu = new Menu("COFFEE MENU",
"Stuff to go with your afternoon coffee");
// create a new all Menu
MenuComponent allMenus = new Menu("All Menus", "All Menus combined");
allMenus.add(pancakeHouseMenu);
allMenus.add(dinerMenu);
allMenus.add(cafeMenu);
pancakeHouseMenu.add(new MenuItem("K&B's Pancake Breakfast",
"Pancakes with scrambled eggs, and toast", true, 2.99));
pancakeHouseMenu.add(new MenuItem("Regular Pancake Breakfast",
"Pancakes with fried eggs, sausage", false, 2.99));
pancakeHouseMenu.add(new MenuItem("Blueberry Pancakes",
"Pancakes made with fresh blueberries, and blueberry syrup",
true, 3.49));
pancakeHouseMenu.add(new MenuItem("Waffles",
"Waffles, with your choice of blueberries or strawberries",
true, 3.59));
dinerMenu.add(new MenuItem("Vegetarian BLT",
"(Fakin') Bacon with lettuce & tomato on whole wheat", true,
2.99));
dinerMenu.add(new MenuItem("BLT",
"Bacon with lettuce & tomato on whole wheat", false, 2.99));
dinerMenu.add(new MenuItem("Soup of the day",
"A bowl of the soup of the day, with a side of potato salad",
false, 3.29));
dinerMenu
.add(new MenuItem(
"Hotdog",
"A hot dog, with saurkraut, relish, onions, topped with cheese",
false, 3.05));
dinerMenu.add(new MenuItem("Steamed Veggies and Brown Rice",
"Steamed vegetables over brown rice", true, 3.99));
dinerMenu
.add(new MenuItem(
"Pasta",
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
true, 3.89));
// add menuItem to dinerMenu
dinerMenu
.add(new MenuItem("Pasta", "Spaghetti with Marinara", true, 33));
// add menu to menu
dinerMenu.add(dessertMenu);
// add menuItem to dessertMenu
dessertMenu.add(new MenuItem("Apple Pue",
"Apple pie with a flakey crust", true, 13));
//add MenuItem to cafeMenu
cafeMenu.add(new MenuItem(
"Veggie Burger and Air Fries",
"Veggie burger on a whole wheat bun, lettuce, tomato, and fries",
true, 3.99));
cafeMenu.add(new MenuItem("Soup of the day",
"A cup of the soup of the day, with a side salad", false, 3.69));
cafeMenu.add(new MenuItem("Burrito",
"A large burrito, with whole pinto beans, salsa, guacamole",
true, 4.29));
//add coffeeMenu to cafeMenu
cafeMenu.add(coffeeMenu);
//add MenuItem to cafeMenu
coffeeMenu.add(new MenuItem("Coffee Cake",
"Crumbly cake topped with cinnamon and walnuts", true, 1.59));
coffeeMenu.add(new MenuItem("Bagel",
"Flavors include sesame, poppyseed, cinnamon raisin, pumpkin",
false, 0.69));
// add all menu to allMenus
Waitress waitress = new Waitress(allMenus);
// waitress.print();
waitress.printVegetarianMenu();
}
}
/**
* Test class
*
* @author Administrator
*
*/
public class MenuTestDrive {
public static void main(String[] args) {
// first make up MenuComponent component
MenuComponent pancakeHouseMenu = new Menu("Pancake House Menu",
"Breakfast");
MenuComponent dinerMenu = new Menu("Diner Menu", "Lunch");
MenuComponent cafeMenu = new Menu("Cafe Menu", "Diner");
MenuComponent dessertMenu = new Menu("Dessert Menu",
"Dessert of Course!");
MenuComponent coffeeMenu = new Menu("COFFEE MENU",
"Stuff to go with your afternoon coffee");
// create a new all Menu
MenuComponent allMenus = new Menu("All Menus", "All Menus combined");
allMenus.add(pancakeHouseMenu);
allMenus.add(dinerMenu);
allMenus.add(cafeMenu);
pancakeHouseMenu.add(new MenuItem("K&B's Pancake Breakfast",
"Pancakes with scrambled eggs, and toast", true, 2.99));
pancakeHouseMenu.add(new MenuItem("Regular Pancake Breakfast",
"Pancakes with fried eggs, sausage", false, 2.99));
pancakeHouseMenu.add(new MenuItem("Blueberry Pancakes",
"Pancakes made with fresh blueberries, and blueberry syrup",
true, 3.49));
pancakeHouseMenu.add(new MenuItem("Waffles",
"Waffles, with your choice of blueberries or strawberries",
true, 3.59));
dinerMenu.add(new MenuItem("Vegetarian BLT",
"(Fakin') Bacon with lettuce & tomato on whole wheat", true,
2.99));
dinerMenu.add(new MenuItem("BLT",
"Bacon with lettuce & tomato on whole wheat", false, 2.99));
dinerMenu.add(new MenuItem("Soup of the day",
"A bowl of the soup of the day, with a side of potato salad",
false, 3.29));
dinerMenu
.add(new MenuItem(
"Hotdog",
"A hot dog, with saurkraut, relish, onions, topped with cheese",
false, 3.05));
dinerMenu.add(new MenuItem("Steamed Veggies and Brown Rice",
"Steamed vegetables over brown rice", true, 3.99));
dinerMenu
.add(new MenuItem(
"Pasta",
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
true, 3.89));
// add menuItem to dinerMenu
dinerMenu
.add(new MenuItem("Pasta", "Spaghetti with Marinara", true, 33));
// add menu to menu
dinerMenu.add(dessertMenu);
// add menuItem to dessertMenu
dessertMenu.add(new MenuItem("Apple Pue",
"Apple pie with a flakey crust", true, 13));
//add MenuItem to cafeMenu
cafeMenu.add(new MenuItem(
"Veggie Burger and Air Fries",
"Veggie burger on a whole wheat bun, lettuce, tomato, and fries",
true, 3.99));
cafeMenu.add(new MenuItem("Soup of the day",
"A cup of the soup of the day, with a side salad", false, 3.69));
cafeMenu.add(new MenuItem("Burrito",
"A large burrito, with whole pinto beans, salsa, guacamole",
true, 4.29));
//add coffeeMenu to cafeMenu
cafeMenu.add(coffeeMenu);
//add MenuItem to cafeMenu
coffeeMenu.add(new MenuItem("Coffee Cake",
"Crumbly cake topped with cinnamon and walnuts", true, 1.59));
coffeeMenu.add(new MenuItem("Bagel",
"Flavors include sesame, poppyseed, cinnamon raisin, pumpkin",
false, 0.69));
// add all menu to allMenus
Waitress waitress = new Waitress(allMenus);
// waitress.print();
waitress.printVegetarianMenu();
}
}
package com.clark.compositpattern;
import java.util.Iterator;
public class NullIterator implements Iterator {
public Object next() {
return null;
}
public boolean hasNext() {
return false;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
import java.util.Iterator;
public class NullIterator implements Iterator {
public Object next() {
return null;
}
public boolean hasNext() {
return false;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
package com.clark.compositpattern;
import java.util.Iterator;
/**
* Waitress class
* @author Administrator
*
*/
public class Waitress {
//Waitress only know all menu
MenuComponent allMenus;
public Waitress(MenuComponent allMenus){
this.allMenus=allMenus;
}
public void print(){
allMenus.print();
}
public void printVegetarianMenu() {
Iterator iterator = allMenus.createIterator();
System.out.println("\nVEGETARIAN MENU\n----");
while (iterator.hasNext()) {
MenuComponent menuComponent =
(MenuComponent)iterator.next();
try {
if (menuComponent.isVegetarian()) {
menuComponent.print();
}
} catch (UnsupportedOperationException e) {}
}
}
}
import java.util.Iterator;
/**
* Waitress class
* @author Administrator
*
*/
public class Waitress {
//Waitress only know all menu
MenuComponent allMenus;
public Waitress(MenuComponent allMenus){
this.allMenus=allMenus;
}
public void print(){
allMenus.print();
}
public void printVegetarianMenu() {
Iterator iterator = allMenus.createIterator();
System.out.println("\nVEGETARIAN MENU\n----");
while (iterator.hasNext()) {
MenuComponent menuComponent =
(MenuComponent)iterator.next();
try {
if (menuComponent.isVegetarian()) {
menuComponent.print();
}
} catch (UnsupportedOperationException e) {}
}
}
}