设计模式:18、组合模式

目录

0、定义

1、组合模式的三种角色

2、组合模式的UML类图

3、示例代码


0、定义

将对象组合成树形结构以表示"部分-整体"的层次结构。Composite使用户对单个对象和组合对象的使用具有一致性。

1、组合模式的三种角色

  • 抽象组件(Component):是一个接口(抽象类),该接口(抽象类)定义了个体对象 和组合对象 需要实现的关于操作其子节点的方法,比如add()、remove()以及getChild()等方法。抽象组件 也可以定义个体对象 和组合对象用于操作其自身的方法。
  • Composite节点(Composite Node):实现Component接口类的实例,Composite节点 不仅实现Component接口 ,而且可以含有其他Composite节点 或Leaf节点的引用。
  • Leaf节点(Leaf Node):实现Component接口 类的实例,Leaf节点 实现Component接口,不可以含有其他Composite节点 或Leaf节点 的引用,因此,叶节点 在实现Component接口有关操作子节点的方法时,比如add()、remove()和getChild()方法,可让方法抛出一个异常,也可以实现为空操作。

2、组合模式的UML类图

3、示例代码

抽象组件

java 复制代码
package xyz.jangle.design.composite;
/**
 * 抽象组件
 * @author Administrator
 *
 */
public interface Component {
	public void add(Component component);
	public void remove(Component component);
	public Component getChild(int i);
	public void opration();

}

Composite节点(组合节点)

java 复制代码
package xyz.jangle.design.composite;

import java.util.Iterator;
import java.util.LinkedList;
/**
 * Composite节点
 * @author Administrator
 *
 */
public class Composite implements Component {
	
	private LinkedList<Component> child;

	public Composite() {
		super();
		child = new LinkedList<Component>();
	}

	@Override
	public void add(Component component) {
		child.add(component);
	}

	@Override
	public void remove(Component component) {
		child.remove(component);
	}

	@Override
	public Component getChild(int i) {
		return child.get(i);
	}

	@Override
	public void opration() {
		System.out.println(this+"  Composite to do something");
		Iterator<Component> iterator = child.iterator();
		while(iterator.hasNext()) {
			Component component = iterator.next();
			component.opration();
		}

	}

}

Leaf节点(叶子节点)

java 复制代码
package xyz.jangle.design.composite;

public class Leaf implements Component {

	@Override
	public void add(Component component) {

	}

	@Override
	public void remove(Component component) {

	}

	@Override
	public Component getChild(int i) {
		return null;
	}

	@Override
	public void opration() {
		System.out.println(this + "  Leaf to do something");

	}

}

客户端(使用)

java 复制代码
package xyz.jangle.design.composite;

public class AppMain18 {

	public static void main(String[] args) {
		
		Composite firstNode = new Composite();
		Leaf leaf1 = new Leaf();
		Leaf leaf2 = new Leaf();
		Composite childNode = new Composite();
		Leaf childLeaf1 = new Leaf();
		Leaf childLeaf2 = new Leaf();
		
		firstNode.add(leaf1);
		firstNode.add(leaf2);
		firstNode.add(childNode);
		childNode.add(childLeaf1);
		childNode.add(childLeaf2);
		
		firstNode.opration();
		System.out.println("-------------");
		childNode.opration();
	}

}

输出结果

bash 复制代码
xyz.jangle.design.composite.Composite@659e0bfd  Composite to do something
xyz.jangle.design.composite.Leaf@2a139a55  Leaf to do something
xyz.jangle.design.composite.Leaf@15db9742  Leaf to do something
xyz.jangle.design.composite.Composite@6d06d69c  Composite to do something
xyz.jangle.design.composite.Leaf@7852e922  Leaf to do something
xyz.jangle.design.composite.Leaf@4e25154f  Leaf to do something
-------------
xyz.jangle.design.composite.Composite@6d06d69c  Composite to do something
xyz.jangle.design.composite.Leaf@7852e922  Leaf to do something
xyz.jangle.design.composite.Leaf@4e25154f  Leaf to do something
相关推荐
拆房老料8 小时前
DeepSeek Super-agent 架构解析:从单任务 Agent 到持续运行的任务系统
人工智能·设计模式·系统架构
工一木子13 小时前
设计模式与生活⑩:地铁没变,你换了个状态
设计模式
是2的10次方啊15 小时前
设计模式与生活⑪:地铁里被打扰,别总靠自己硬扛
设计模式
高晶1 天前
SPMI 控制器 RTL 设计与验证新手入门
设计模式·程序员·架构
geovindu2 天前
rust:N-Barrier Pattern
开发语言·后端·设计模式·rust·屏障模式·并发型模式
顧棟2 天前
OpenAPI 网关:鉴权 + 流量治理 一体化设计方案与实现
java·spring boot·设计模式
拆房老料2 天前
【第四部分:Agentic 智能体设计模式】17. Agent 路由模式详解:从意图识别到动态模型选择
java·设计模式·ai编程·策略模式
是2的10次方啊2 天前
设计模式与生活⑩:地铁没变,你换了个状态
设计模式
步行cgn2 天前
适配器设计模式:一个“翻译官”的故事
设计模式
扶尔魔ocy3 天前
【QT C++】常用设计模式
c++·设计模式