组合模式

来源:互联网 发布:华硕fx50j优化 编辑:程序博客网 时间:2024/04/28 05:11

组合模式:允许你将对象组合成树形结构来表现“整体/部分”层次结构。组合能让客户以一致的方式处理个别对象以及对象组合。

这里写图片描述

我们以菜单为例,一个菜单中可能包含多个菜单项,或者包含多个子菜单。其中子菜单又可能包含菜单项以及菜单。组合模式能够创建一个树形结构,来统一处理嵌套的菜单或者菜单项。

1. 菜单组件

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();    }}

2. 菜单项

public class MenuItem extends MenuComponent{    private String name;    private String description;    private boolean vegetarian;    private double price;    public MenuItem(String name, String description, boolean vegetarian,            double price) {        super();        this.name = name;        this.description = description;        this.vegetarian = vegetarian;        this.price = price;    }    @Override    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;    }    @Override    public boolean isVegetarian() {        return vegetarian;    }    public void setVegetarian(boolean vegetarian) {        this.vegetarian = vegetarian;    }    @Override    public double getPrice() {        return price;    }    public void setPrice(double price) {        this.price = price;    }    @Override    public String toString() {        return "MenuItem [name=" + name + ", description=" + description                + ", vegetarian=" + vegetarian + ", price=" + price + "]";    }    @Override    public void print() {        System.out.println(toString());    }}

3. 子菜单

public class Menu extends MenuComponent {    private List<MenuComponent> menuComponents = new ArrayList<MenuComponent>();    private String name;    private String description;    public Menu(String name, String description) {        super();        this.name = name;        this.description = description;    }    @Override    public void add(MenuComponent menuComponent){        menuComponents.add(menuComponent);    }    @Override    public void remove(MenuComponent menuComponent){        menuComponents.remove(menuComponent);    }    @Override    public MenuComponent getChild(int i){        return menuComponents.get(i);    }    @Override    public String getName(){        return name;    }    public String getDescription(){        return description;    }    @Override    public void print() {        Iterator<MenuComponent> iterator = menuComponents.iterator();        while(iterator.hasNext()){            MenuComponent menuComponent = iterator.next();            menuComponent.print();        }       }}

4. 服务员

public class Waitress {    MenuComponent allMenus;    public Waitress(MenuComponent allMenus) {        super();        this.allMenus = allMenus;    }    public void printMenu(){        allMenus.print();    }}

5. 测试

public class CombinationTest {    public static void main(String[] args) {        Menu pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU", "Breakfast");        Menu dinerMenu = new Menu("DINER MENU", "Lunch");        Menu dessertMenu = new Menu("DESSERT MENU", " Dessert if course");        Menu allMenus = new Menu("ALL MENUS", "All menus of combined");        dinerMenu.add(dessertMenu);        allMenus.add(pancakeHouseMenu);        allMenus.add(dinerMenu);        MenuItem soup = new MenuItem("Soup of the day", "Soup of the day, with a side of potato salad", false, 3.29);        MenuItem hotDog = new MenuItem("Hotdog", "A hot dog with saurkraut, relish, onions, topped with cheese", false, 3.55);        dinerMenu.add(soup);        dinerMenu.add(hotDog);        MenuItem kb = new MenuItem("K&B's Pancake Breakfast", "Pancakes with acrambled eggds, and toast", true, 2.99);        MenuItem regular = new MenuItem("Regular pancake Breakfast", "Pancake with fried eggds, saisage", false, 2.99);        pancakeHouseMenu.add(kb);        pancakeHouseMenu.add(regular);        MenuItem applyPie = new MenuItem("APPLIE PIE", "Apply Pie WITH a flake crust, topped with wanilla ice cream", true, 1.59);        dinerMenu.add(applyPie);        Waitress waitress = new Waitress(allMenus);        waitress.printMenu();    }}

6. 运行结果

MenuItem [name=K&B's Pancake Breakfast, description=Pancakes with acrambled eggds, and toast, vegetarian=true, price=2.99]MenuItem [name=Regular pancake Breakfast, description=Pancake with fried eggds, saisage, vegetarian=false, price=2.99]MenuItem [name=Soup of the day, description=Soup of the day, with a side of potato salad, vegetarian=false, price=3.29]MenuItem [name=Hotdog, description=A hot dog with saurkraut, relish, onions, topped with cheese, vegetarian=false, price=3.55]MenuItem [name=APPLIE PIE, description=Apply Pie WITH a flake crust, topped with wanilla ice cream, vegetarian=true, price=1.59]