23种设计模式-java-组合模式

本文介绍了如何使用组合模式实现树状结构,以模拟水果店中复杂商品组合(如大苹果、小苹果、水果拼盘等)的定价和操作。通过抽象产品组件和简单/复杂元素(LeafProduct和CompositeProduct),展示了如何保持客户端代码的一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模式概述:

你可以使用它将对象组合成树状结构, 并且能像使用独立对象一样使用它们。

使用场景:

1.如果你需要实现树状对象结构, 可以使用组合模式。
2.如果你希望客户端代码以相同方式处理简单和复杂元素, 可以使用该模式。

代码样例:

代码背景: 组合模式主要是用于树状结构;以下代码是模拟买水果的场景,每种水果是叶子节点(简单元素);水果需要袋子包装,使用袋子包装好水果的组合是复杂元素,一些包装好水果的袋子可能和其他水果或者袋子又被大的袋子再次包装。

/**
 * 抽象产品元素
 */
public abstract class ProductComponent {
    public String name;
    public long price;

    public ProductComponent(String name, long price){
        this.name=name;
        this.price = price;
    }

    public abstract boolean add(ProductComponent productComponent);//添加元素
    public abstract boolean remove(ProductComponent productComponent);//移除元素
    public abstract List<ProductComponent> getChlid();//获取子元素

    public abstract long excute();
}

/**
 * 简单元素
 */
public class LeafProduct extends ProductComponent{

    public LeafProduct(String name, long price) {
        super(name, price);
    }

    @Override
    public boolean add(ProductComponent productComponent) {
        return false;
    }

    @Override
    public boolean remove(ProductComponent productComponent) {
        return false;
    }

    @Override
    public List<ProductComponent> getChlid() {
        return null;
    }

    @Override
    public long excute() {
        System.out.println("==简单元素:name="+name+",price="+price);
        return price;
    }
}
/**
 * 复杂元素
 */
public class CompositeProduct extends ProductComponent{
    List<ProductComponent> childs;
    public CompositeProduct(String name, long price) {
        super(name, price);
        childs = new ArrayList<ProductComponent>();
    }

    @Override
    public boolean add(ProductComponent productComponent) {
        return childs.add(productComponent);
    }

    @Override
    public boolean remove(ProductComponent productComponent) {
        return childs.remove(productComponent);
    }

    @Override
    public List<ProductComponent> getChlid() {
        return childs;
    }

    @Override
    public long excute() {
        System.out.println("--复杂元素:name="+name+",price="+price);
        if(childs.size()!=0){
            for (ProductComponent productComponent:childs){
                price+=productComponent.excute();
            }
        }
        return price;
    }
}
public class TestComposite {
    public static void main(String[] args) {
        System.out.println("水果店买水果。。");
        ProductComponent apple1 = new LeafProduct("大苹果",5l);
        ProductComponent apple2 = new LeafProduct("小苹果",3l);
        ProductComponent appleComposite = new CompositeProduct("苹果方便带",0l);
        appleComposite.add(apple1);
        appleComposite.add(apple2);


        ProductComponent banana = new LeafProduct("香蕉-拼盘",2l);
        ProductComponent pineapple = new LeafProduct("菠萝-拼盘",3l);
        ProductComponent dragoFruit = new LeafProduct("火龙果-拼盘",4l);
        ProductComponent fruitPlatter = new CompositeProduct("水果拼盘包装",1l);
        fruitPlatter.add(banana);
        fruitPlatter.add(pineapple);
        fruitPlatter.add(dragoFruit);

        ProductComponent fruitComposite = new CompositeProduct("大号方便袋",0l);
        fruitComposite.add(appleComposite);
        fruitComposite.add(fruitPlatter);

        fruitComposite.excute();

        System.out.println("水果总价:"+fruitComposite.price);

    }
}

测试结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值