设计模式学习笔记:组合模式

本文主要分为三部分:1、组合模式的基本定义;2、组合模式的类图结构;3、代码示例

一、定义

Composite:将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得客户对单个对象和复合对象的使用具有一致性。

二、类图结构

clip_image002

三、代码示例

例子:一个项目包含多个文件夹、类、接口等,而一个文件夹也同样可以包含多个文件夹、类、接口。

代码:

项目抽象类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace y.CompositePattern
{
    public abstract class ComponentProject
    {
        protected string name = string.Empty;

        public ComponentProject(string name)
        {
            this.name = name;
        }

        public abstract void Add(ComponentProject componentProject);
        public abstract void Remove(ComponentProject componentProject);
        public abstract void ShowTitle(int depth);
    }
}

文件夹实现类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace y.CompositePattern
{
    public class CompositeFolder:ComponentProject
    {
        private List<ComponentProject> subFolder = new List<ComponentProject>();
        public CompositeFolder(string name)
            : base(name)
        { }

        public override void Add(ComponentProject componentProject)
        {
            subFolder.Add(componentProject);
        }
        public override void Remove(ComponentProject componentProject)
        {
            subFolder.Remove(componentProject);
        }
        public override void ShowTitle(int depth)
        {
            Console.WriteLine(new string('-',depth)+name);
            foreach(ComponentProject project in subFolder)
            {
                project.ShowTitle(depth+2);
            }
        }
        
    }
}

”类“实现类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace y.CompositePattern
{
    public class LeafClass:ComponentProject
    {
        public LeafClass(string name)
            : base(name)
        { }

        public override void Add(ComponentProject componentProject)
        {
        }
        public override void Remove(ComponentProject componentProject)
        {
        }

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

“接口”实现类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace y.CompositePattern
{
    public class LeafInterface:ComponentProject
    {
        public LeafInterface(string name)
            : base(name)
        { }

        public override void Add(ComponentProject componentProject)
        {
           
        }

        public override void Remove(ComponentProject componentProject)
        {
         
        }

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

主程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace y.CompositePattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title="组合模式";
            Console.WriteLine("Press any ket to exit program.\r\n");
            CompositeFolder mainFolder = new CompositeFolder("Composite Example");
            mainFolder.Add(new LeafClass("Main Class"));
            mainFolder.Add(new LeafInterface("Main Interface"));

            CompositeFolder bussinessFolder = new CompositeFolder("Bussiness Folder");
            bussinessFolder.Add(new LeafInterface("Bussiness Interface"));
            bussinessFolder.Add(new LeafClass("Bussiness Class"));

            mainFolder.Add(bussinessFolder);

            CompositeFolder databaseFolder = new CompositeFolder("Database Folder");
            databaseFolder.Add(new LeafInterface("Database Interface"));
            databaseFolder.Add(new LeafClass("Database Class"));

            mainFolder.Add(databaseFolder);

            Console.WriteLine("Composite Struct:");
            mainFolder.ShowTitle(1);

            Console.ReadKey();
        }
    }
}

参考文献:

《大话设计模式》

转载于:https://www.cnblogs.com/xyz168/archive/2011/06/26/2090512.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值