设计模式: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***29311 小时前
能懂!基于Springboot的用户增删查改(三层设计模式)
spring boot·后端·设计模式
在未来等你10 小时前
AI Agent设计模式 Day 19:Feedback-Loop模式:反馈循环与自我优化
设计模式·llm·react·ai agent·plan-and-execute
兵bing15 小时前
设计模式-访问者模式
设计模式·访问者模式
python零基础入门小白15 小时前
【万字长文】大模型应用开发:意图路由与查询重写设计模式(从入门到精通)
java·开发语言·设计模式·语言模型·架构·大模型应用开发·大模型学习
MC丶科16 小时前
Java设计模式漫画英雄宇宙-工厂模式 —Factory博士的“超级英雄制造机”!
java·设计模式·漫画
明洞日记16 小时前
【设计模式手册013】命令模式 - 请求封装的优雅之道
java·设计模式·命令模式
ada0_ada117 小时前
行为型模式:②命令模式(Command Pattern)
设计模式
o0向阳而生0o18 小时前
113、23种设计模式之中介者模式(21/23)
设计模式·中介者模式
心语星光18 小时前
23种经典设计模式
设计模式
ACE19852 天前
AI Agent 设计模式深度解析:提示链(Prompt Chaining)模式
人工智能·设计模式·prompt