设计模式: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
相关推荐
杨充11 小时前
11.DDD与战术建模
设计模式·开源·代码规范
杨充11 小时前
12.综合实战图片框架
设计模式·开源·代码规范
芝士熊爱编程15 小时前
创建型模式-单例模式
java·单例模式·设计模式
咖啡八杯18 小时前
GoF设计模式——访问者模式
设计模式·访问者模式
谢栋_20 小时前
设计模式从入门到精通之(七)责任链模式
java·设计模式·责任链模式
葬送的代码人生1 天前
别再让 AI 瞎写代码了!Vibe Coding 三步法教你写出靠谱代码
前端·设计模式·架构
无风听海2 天前
Claude Agent Skills 的四种设计模式;从渐进式披露到最小权限
java·算法·设计模式
富贵冼中求2 天前
从单向流到双向 RPC:Agent 通信协议的范式分叉与 ACP 协议实战拆解
设计模式·架构
电子科技圈2 天前
先进封装、芯粒架构和3D集成——先进异构集成亟需兼具标准化与定制化能力的互联及总线IP解决方案
tcp/ip·设计模式·架构·软件构建·代码规范·设计规范
zjun10012 天前
C++:2.工厂模式
设计模式