设计模式与实例代码:Composite模式

本文介绍了一种软件设计模式——组合模式,该模式通过将对象组织成树形结构来表示整体与部分的关系,允许用户以统一的方式处理单个对象及对象组合。文章通过一个公司组织结构的例子展示了如何使用组合模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

意图/定义:将对象组合成树形结构以表现“整体/部分”层次结构,组合可以让客户以一致的方式处理个别对象以及对象组合。

问题:你想表示对象的部分/整体层次结构或者需要忽略组合对象与单个对象的不同

解决方案:

参与者和协作者:在Composite中用来存储部件实现Component接口中实现的有关操作

效果:组合模式解耦了客户程序与复杂元素内部结构

实现:




示例代码,以公司的结构为例,一个公司可能由多个单个人以及一些部门组成。现在我们需要了解公司的人员组成情况,可我们又不希望去了解公司各个部门内部或这些个人的组织情况,我们为个人、部门到公司都提供一个输出本单元内详情的接口以达到这人目的。

using System;
using System.Collections.Generic;

namespace Composite
{
    /// <summary>
    /// MainApp startup class for Structural
    /// Composite Design Pattern.
    /// </summary>
    class MainApp
    {
        /// <summary>
        /// Entry point into console application.
        /// </summary>
        static void Main()
        {
            // Create a tree structure
            Composite company = new Composite("MicroSoft");
            company.Add(new Leaf("CEO"));
            company.Add(new Leaf("CTO"));

            Composite department = new Composite("Windows 8 Development Dep");
            department.Add(new Leaf("Department Leader"));
            department.Add(new Leaf("Department Member"));

            company.Add(department);
            company.Add(new Leaf("CFO"));

            // Add and remove a leaf
            Leaf temp_worker = new Leaf("UFO");
            company.Add(temp_worker);
            company.Remove(temp_worker);

            // Recursively display tree
            company.Display(1);

            // Wait for user
            Console.ReadKey();
        }
    }

    /// <summary>
    /// The 'Component' abstract class
    /// </summary>
    abstract class Component
    {
        protected string name;

        // Constructor
        public Component(string name)
        {
            this.name = name;
        }

        public abstract void Add(Component c);
        public abstract void Remove(Component c);
        public abstract void Display(int depth);
    }

    /// <summary>
    /// The 'Composite' class
    /// </summary>
    class Composite : Component
    {
        private List<Component> _children = new List<Component>();

        // Constructor
        public Composite(string name) : base(name)
        {
        }

        public override void Add(Component component)
        {
            _children.Add(component);
        }

        public override void Remove(Component component)
        {
            _children.Remove(component);
        }

        public override void Display(int depth)
        {
            Console.WriteLine(new String('-', depth) + name);

            // Recursively display child nodes
            foreach (Component component in _children)
            {
                component.Display(depth + 2);
            }
        }
    }

    /// <summary>
    /// The 'Leaf' class
    /// </summary>
    class Leaf : Component
    {
        // Constructor
        public Leaf(string name) : base(name)
        {
        }

        public override void Add(Component c)
        {
            Console.WriteLine("Cannot add to a leaf");
        }

        public override void Remove(Component c)
        {
            Console.WriteLine("Cannot remove from a leaf");
        }

        public override void Display(int depth)
        {
            Console.WriteLine(new String('-', depth) + name);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值