设计模式之组合模式(HashMap源码中组合模式的使用)

一、概述

1、定义

        Compose objects into tree structures to represent part-whole hierarches.Composite lets clients treat individual objects and compositions of objects uniformly.(将对象组合成树形结构以表示“部分-整体”的层次结构使得用户对单个对象和组合对象的使用具有一致性)。

2、通用类图

在这里插入图片描述

二、通用源码

Component抽象构件角色:
        定义参加组合对象的共有方法和属性,可以定义一些默认的行为和属性。

public abstract class Component {
   
    //个题和整体都具有的共享
    public void doSomething(){
   
        //业务逻辑处理
    }
}

Composite树枝构件:
        树枝对象,作用是组合树枝节点和叶子节点形成一个树形结构。

public class Composite extends Component {
   
    //构件容器
    private ArrayList<Component> componentArrayList=new ArrayList<>();
    
    //增加一个叶子构件或者树枝构件
    public void add(Component component){
   
        this.componentArrayList.add(component);
    }
    
    //删除一个叶子构件或者树枝构件
    public void remove(Component component){
   
        this.componentArrayList.remove(component)
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值