设计模式之组合模式

Definition

Composite objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly.

GoF 定义

将对象以树状结构组合,用以表现部分-全体的层次关系。组合模式让客户端在操作各个对象或组合对象时是一致的。

img

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");            
        }
    }
}

Summarize

Benefit

Defect

Applicable Situations

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值