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

本文主要分为三部分: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();

        }

    }

}

参考文献:

《大话设计模式》

你可能感兴趣的:(设计模式)