当前位置:编程学习 > JAVA >>

head first---------composite design pattern

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());
}
}
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();
}
 
 
}
 
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();
}
 
 
 
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();
}
}
 
 
 
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, a
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,