Java 组合模式

本文主要用作学习记录,参考headfirst设计模式
设计原则:找出应用中可能需要变化之外,把它们独立出来,不要和那些不需要变化的代码混在一起
设计原则:针对接口编程,而不是针对实现编程
设计原则:多用组合,少用继承
设计原则:类应该对扩展开放,对修改关闭
设计原则:一个类应该只有一个引起变化的原因
组合模式允许你将对象组合成树结构来表现“整体/部分”层次结构。组合能让客户以一致的方式处理个别对象以及对象组合

//定义数据结构
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();
    }
}
//多个menu集合
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;
    }

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

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

    @Override
    public MenuComponent getChild(int i) {
        return (MenuComponent) 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("--------------------");
        Iterator iterator = menuComponents.iterator();
        while(iterator.hasNext()){
            ((MenuComponent)iterator.next()).print();
        }
    }
}

//单个item相关信息
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());
    }
}


//测试
public class TestCompoent {
    public static void main(String[] args) {
        Menu totalMenuItem = new Menu("total","total menu item");
        MenuItem breakfast  = new MenuItem("breakfast","this is breakfast",true,0.89);
        MenuItem lunch  = new MenuItem("lunch","this is lunch",true,1.50);
        MenuItem dinner  = new MenuItem("dinner","this is dinner",true,2.78);
        totalMenuItem.add(breakfast);
        totalMenuItem.add(lunch);
        totalMenuItem.add(dinner);

        totalMenuItem.print();
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值