1. 程式人生 > >C#設計模式-組合模式

C#設計模式-組合模式

 透明式組合模式:

using System;
using System.Collections.Generic;

namespace TestCS
{
    class Program
    {
        static void Main(string[] args)
        {
            Folder folder = new Folder();
            folder.Add(new Folder());
            Console.WriteLine("the number of sub-folders in this folder: " + folder.Open().Count);
            var secondFolder = new Folder();
            folder.Add(secondFolder);
            Console.WriteLine("the number of sub-folders in this folder: " + folder.Open().Count);
            folder.Remove(secondFolder);
            Console.WriteLine("the number of sub-folders in this folder: " + folder.Open().Count);
            List<Node> list = folder.Open();
            Console.WriteLine("the number of elements in this list: " + list.Count);
            if (list.Count > 0)
            {
                list[0].Add(new File());
                Console.WriteLine("the number of sub-folders in this folder: " + list[0].Open().Count);
                List<Node> files = list[0].Open();
                Console.WriteLine("the number of elements in this list: " + files.Count);
                if (files.Count > 0)
                {
                    files[0].Open();
                    try
                    {
                        files[0].Add(new File());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("error message: " + ex.Message);
                    }
                }
            }
            Console.ReadKey();
        }

        abstract class Node
        {
            public abstract void Add(Node node);
            public abstract void Remove(Node node);
            public abstract List<Node> Open();
        }

        class Folder : Node
        {
            List<Node> m_Nodes = new List<Node>();

            public override void Add(Node node)
            {
                if (!m_Nodes.Contains(node))
                {
                    m_Nodes.Add(node);
                }
            }

            public override void Remove(Node node)
            {
                if (m_Nodes.Contains(node))
                {
                    m_Nodes.Remove(node);
                }
            }

            public override List<Node> Open()
            {
                return m_Nodes;
            }
        }


        class File : Node
        {
            public override void Add(Node node)
            {
                throw new Exception("Cannot add node for file! ");
            }

            public override void Remove(Node node)
            {
                throw new Exception("Cannot remove node in file! ");
            }

            public override List<Node> Open()
            {
                Console.WriteLine("Open File");
                return null;
            }
        }
    }
}

 

安全式組合模式:

using System;
using System.Collections.Generic;

namespace TestCS
{
    class Program
    {
        static void Main(string[] args)
        {
            Folder folder = new Folder();
            folder.Add(new Folder());
            Console.WriteLine("the number of sub-folders in this folder: " + folder.Open().Count);
            var secondFolder = new Folder();
            folder.Add(secondFolder);
            Console.WriteLine("the number of sub-folders in this folder: " + folder.Open().Count);
            folder.Remove(secondFolder);
            Console.WriteLine("the number of sub-folders in this folder: " + folder.Open().Count);
            List<Node> list = folder.Open();
            Console.WriteLine("the number of elements in this list: " + list.Count);
            if (list.Count > 0)
            {
                Console.WriteLine("the number of sub-folders in this folder: " + list[0].Open().Count);
                Folder tempFolder = list[0] as Folder;
                if (tempFolder != null)
                {
                    tempFolder.Add(new File());
                    Console.WriteLine("the number of sub-folders in this folder: " + tempFolder.Open().Count);
                }
                List<Node> files = list[0].Open();
                Console.WriteLine("the number of elements in this list: " + files.Count);
                if (files.Count > 0)
                {
                    files[0].Open();
                }
            }
            Console.ReadKey();
        }

        abstract class Node
        {
            public abstract List<Node> Open();
        }

        class Folder : Node
        {
            List<Node> m_Nodes = new List<Node>();

            public void Add(Node node)
            {
                if (!m_Nodes.Contains(node))
                {
                    m_Nodes.Add(node);
                }
            }

            public void Remove(Node node)
            {
                if (m_Nodes.Contains(node))
                {
                    m_Nodes.Remove(node);
                }
            }

            public override List<Node> Open()
            {
                return m_Nodes;
            }
        }

        class File : Node
        {
            public override List<Node> Open()
            {
                Console.WriteLine("Open File");
                return null;
            }
        }
    }
}