组合模式(结构型)

一:相关定义

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

二:相关类图的示例

组合模式类图:
组合模式类图
使用组合结构,我们能把相同的操作应用在组合和个别对象上。换句话说,大多数情况下,我们可以忽略对象组合和个别对象之间的差距。

组合模式示例:
菜单组合
这是一个菜单的组合模式示例,在一个菜单中,可能包含有一个子菜单,也可能包含一个菜品;使用组合模式后,waitress在遍历菜单时不用关心菜单中是子菜单还是菜品,只需要使用统一的接口就可以了,大大便利了客户端的操作。下面是具体代码:
菜单组件MenuComponent

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 String getPrice(){
        throw new UnsupportedOperationException();
    }
    public boolean isVegetarian(){
        throw new UnsupportedOperationException();
    }
    public void print(){
        throw new UnsupportedOperationException();
    }
}

菜单组件包含了菜品和子菜单的所有方法,这里默认实现抛出异常,叶子节点和组合节点都需要继承菜单组件。
菜单项MenuItem:

public class MenuItem extends MenuComponent{
    String name;
    String description;
    boolean vegetarain;
    double price;

    public MenuItem(String name,
                    String description,
                    boolean vegetarian,
                    double price){
        this.name = name;
        this.description = description;
        this.vegetarian = vegatarian;
        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.print(" "+getName() );
        if(isVegetarian()){
            System.out.print("(V)");
        }
        System.out.print(", "+getPrice());
        System.out.print(" -- "+getDescription());
    }
}

组合菜单:

public class Menu extends MenuComponent{
    ArrayList menuComponents = new ArrayList();
    String name;
    String description;

    public Menu(ArrayList menuComponents, String name, String description) {
        this.menuComponents = menuComponents;
        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.println("\n"+getName());
        System.out.println(", "+getDescription());
        System.out.println("-------------------");

        Iterator it = menuComponents.iterator();
        while (it.hasNext()){
            MenuComponent menuComponent = (MenuComponent) it.next();
            menuComponent.print();
        }
    }
}

客户端waitress:

public class Waitress{
    MenuComponent allMenus;

    public Waitress(MenuComponent allMenus){
        this.allMenus = allMenus;
    }

    public void printMenu(){
        allMenus.print();
    }
}

这里,主要的代码就完成了。女招待只要调用顶层的print()方法,就可以完成对整个菜单的遍历了。

参考书籍:《head first 设计模式》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值