.NET/설계 패턴(C#)

[설계 패턴 C#] 8. 복합체 패턴

언제나휴일 2016. 6. 9. 22:50
반응형

8. 복합체 패턴


Composite.zip


"본문 내용"은 언제나 휴일 본 사이트에 있습니다.

Tree.cs

namespace Composite

{

    abstract class Tree

    {

        public string Name

        {

             get;

             private set;

        }

        public Tree Parent

        {

            get;

            set;

        }

 

        public abstract void View();

        public virtual void AddChild(Tree child) //복합 개체에서만 필요한 기능

        {

        }

        public virtual void RemoveChild(Tree child) //복합 개체에서만 필요한 기능

        {

        }

 

        public int Size

        {

            get

            {

                return Name.Length + Level*2;

            }

        }

 

        public int Level

        {

            get

            {

                int level = 0;

                Tree ancestor = Parent;

                while(ancestor!=null)

                {

                    level++;

                    ancestor = ancestor.Parent;

                }

                return level;

            }

        }

       

        public Tree(string name)

        {

            Name = name;

            Parent = null;

        }

    }

}

 

Category.cs

using System;

using System.Collections.Generic;

 

namespace Composite

{

    class Category:Tree

    {

        List<Tree> children = new List<Tree>();

        public Category(string name):base(name)

        { 

        }

        public override void View()

        {

            Console.WriteLine("{0," + Size.ToString() + "}-C", Name);

            foreach(Tree child in children)

            {

                child.View();

            }

        }

        public override void AddChild(Tree child) //Category에만 필요한 기능

        {

            child.Parent = this;

            children.Add(child);

        }

        public override void RemoveChild(Tree child) //Category에만 필요한 기능

        {

            child.Parent = null;

            children.Remove(child);

        }

    }

}

 

Path.cs

using System;

namespace Composite

{

    class Path:Tree

    {

        public Path(string path):base(path)

        {

        }

        public override void View()

        {

            Console.WriteLine("{0," + Size.ToString() + "}", Name);

        }

    }

}

 

Program.cs

namespace Composite

{

    class Program

    {

        static void Main(string[] args)

        {

            Tree tree = new Category("사진트리");

            Tree date_tree = MakeDemoDateTree();

            Tree location_tree = MakeDemoLocationTree();

 

            tree.AddChild(date_tree);

            tree.AddChild(location_tree);

            tree.View();

        }

 

        static Tree MakeDemoLocationTree()

        {

            Tree tree = new Category("지역");

            Tree sub1 = new Category("제주");

            Tree sub2 = new Category("천안");

            Tree sub3 = new Category("서울");

            Tree sub4 = new Path("C:\\사진\\외도");

            Tree sub2_1 = new Path("C:\\사진\\현충사의봄");

           

 

            sub2.AddChild(sub2_1);

            tree.AddChild(sub1);

            tree.AddChild(sub2);

            tree.AddChild(sub3);

            tree.AddChild(sub4);

            return tree;

        }

 

        static Tree MakeDemoDateTree()

        {

            Tree tree = new Category("날짜");

            Tree sub1 = new Category("20120429");

            Tree sub2 = new Category("20120407");

            Tree sub3 = new Category("20120507");

 

            Tree sub1_1 = new Path("C:\\사진\\외도");

            sub1.AddChild(sub1_1);

            Tree sub2_1 = new Path("C:\\사진\\현충사의봄");

            sub2.AddChild(sub2_1);

            Tree sub3_1 = new Path("C:\\사진\\일산고구마모종");

            sub3.AddChild(sub3_1);

 

            tree.AddChild(sub1);

            tree.AddChild(sub2);

            tree.AddChild(sub3);

 

            return tree;

        }

    }

}


반응형