DP-详解组合模式代码实现

组合模式(composite pattern):将对象组合成树形结构来表现部分-整体层次结构。组合让客户可以统一处理个别对象和对象组合。

java 复制代码
package com.designpatterns.composite;

/**
 * 1.实现MenuComponent
 */
public abstract class MenuComponent {
    public void add(MenuComponent menuComponent){
        throw new UnsupportedOperationException();
    }
    public void remove(MenuComponent menuComponent){
        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();
    }
}
java 复制代码
package com.designpatterns.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * 2.实现菜单
 */
public class Menu extends  MenuComponent{
    List<MenuComponent> menuComponents=new ArrayList<>();
    String name;
    String description;

    public  Menu(String name,String description){
        this.name=name;
        this.description=description;
    }

    @Override
    public void add(MenuComponent menuComponent) {
        menuComponents.add(menuComponent);
    }

    @Override
    public void remove(MenuComponent menuComponent) {
        menuComponents.remove(menuComponent);
    }

    public MenuComponent getChild(int i){
        return menuComponents.get(i);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public void print() {
        System.out.println("\n" + getName());
        System.out.println(", "+getDescription());
        System.out.println("---------------------------");
        for (MenuComponent menuComponent : menuComponents) {
            menuComponent.print();
        }
    }
}
java 复制代码
package com.designpatterns.composite;

/**
 * 3.实现MenuItem
 */
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;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public double getPrice() {
        return price;
    }

    @Override
    public boolean isVegetarian() {
        return vegetarian;
    }

    @Override
    public void print() {
        System.out.println(" "+getName());
        if(isVegetarian()){
            System.out.println("(v)");
        }
        System.out.println(", "+getPrice());
        System.out.println("  --"+getDescription());
    }
}
java 复制代码
package com.designpatterns.composite;

/**
 * 4.定义服务员
 */
public class Waitress {
    MenuComponent allMenus;
    public Waitress(MenuComponent allMenus){
        this.allMenus=allMenus;
    }

    public void printMenu(){
        allMenus.print();
    }
}
java 复制代码
package com.designpatterns.composite;

/**
 * 5.测试组合模式
 */
public class MenuTestDrive {
    public static void main(String[] args) {
        MenuComponent pancakeHouseMenu=new Menu("PANCAKE HOUSE MENU","Breakfast");
        MenuComponent dinerMenu=new Menu("DINER MENU","Lunch");
        MenuComponent cafeMenu=new Menu("CAFE MENU","Dinner");
        MenuComponent dessertMenu=new Menu("DESSERT MENU","Dessert of course");

        MenuComponent allMenus=new Menu("ALL Menus","ALL menus combined");

        allMenus.add(pancakeHouseMenu);
        allMenus.add(dinerMenu);
        allMenus.add(cafeMenu);

        //添加菜单项
        dinerMenu.add(new MenuItem(
                "Pasta",
                "Bread",
                true,
                3.89
        ));
        dinerMenu.add(dessertMenu);

        dessertMenu.add(new MenuItem(
                "Apple Pie",
                "ice cream",
                true,
                1.59
        ));

        //添加所有菜单
        Waitress waitress=new Waitress(allMenus);
        waitress.printMenu();

    }
}

最终测试结果如下:

相关推荐
小蜗牛在漫步2 天前
23种设计模式-组合模式
设计模式·组合模式
郝学胜-神的一滴5 天前
Pomian语言处理器研发笔记(二):使用组合模式定义表示程序结构的语法树
开发语言·c++·笔记·程序人生·决策树·设计模式·组合模式
Leo来编程5 天前
设计模式14-组合模式
设计模式·组合模式
Your易元5 天前
模式组合应用-组合模式
组合模式
o0向阳而生0o5 天前
99、23种设计模式之组合模式(8/23)
设计模式·组合模式
郝学胜-神的一滴8 天前
C++组合模式:构建灵活的层次结构
开发语言·c++·程序人生·设计模式·组合模式
TechNomad9 天前
设计模式:组合模式(Composite Pattern)
设计模式·组合模式
快乐的划水a21 天前
组合模式及优化
c++·设计模式·组合模式