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

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

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

解决方案:

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

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

实现:


设计模式与实例代码:Composite模式_第1张图片


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

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


你可能感兴趣的:(设计模式与实例代码:Composite模式)