Definition
Composite objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly.
GoF 定义
将对象以树状结构组合,用以表现部分-全体的层次关系。组合模式让客户端在操作各个对象或组合对象时是一致的。
Participants
The classes and objects participating in this pattern are:
Component(组件界面)
- declares the interface for objects in the composition.
- implements default behavior for the interface common to all classes, as appropriate.
- declares an interface for accessing and managing its child components.
- (optional) defines an interface for accessing a component’s parent in the recursive structure, and implements it if that’s appropriate.
- 定义树状结构中,每一个节点可以使用的操作方法
Leaf
- represents leaf objects in the composition. A leaf has no children.
- defines behavior for primitive objects in the composition.
- 不再包含任何子节点的最终节点
- 实现Component中基本的行为,对于与子节点操作有关的方法可以不实现、也可以提示警告或者弹出例外(Exception)
Composite(组合节点)
- defines behavior for components having children.
- stores child components.
- implements child-related operations in the Component interface.
- 即根节点的概念
- 会包含叶节点的对象
- 会实现Component中与子节点操作有关的方法,如Add,Remove,GetChild等
Client
- manipulates objects in the composition through the Component interface.
Structure
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace CompositeStructure
{
public class CompositeStructure : MonoBehaviour {
void Start () {
// Create a tree structure
Composite root = new Composite("root");
root.Add(new Leaf("Leaf A"));
root.Add(new Leaf("Leaf B"));
Composite comp = new Composite("Composite X");
comp.Add(new Leaf("Leaf XA"));
comp.Add(new Leaf("Leaf XB"));
root.Add(comp);
root.Add(new Leaf("Leaf C"));
Composite comp1 = new Composite("Composite Y");
comp1.Add(new Leaf("Leaf YA"));
comp1.Add(new Leaf("Leaf YB"));
comp.Add(comp1);
// Add and remove a leaf
Leaf leaf = new Leaf("Leaf D");
root.Add(leaf);
root.Remove(leaf);
// Recursively display tree
root.Display(2);
}
}
public abstract class Component
{
protected string m_name;
public Component(string name)
{
m_name = name;
}
public abstract void Add(Component c);
public abstract void Remove(Component c);
public abstract void Display(int depth);
}
public class Composite : Component
{
private List<Component> _children = new List<Component>();
public Composite(string name) : base(name)
{
}
public override void Add(Component c)
{
_children.Add(c);
}
// Recursively display child notes
public override void Display(int depth)
{
Debug.Log(new String('-', depth) + m_name);
foreach (Component component in _children)
{
component.Display(depth + 2);
}
}
public override void Remove(Component c)
{
_children.Remove(c);
}
}
public class Leaf : Component
{
public Leaf(string name) : base(name)
{
}
public override void Add(Component c)
{
Debug.Log("Cann't add to a leaf");
}
public override void Display(int depth)
{
Debug.Log(new String('-', depth) + m_name);
}
public override void Remove(Component c)
{
Debug.Log("Cann't remove from a leaf");
}
}
}