From 1cfa3c397126d9dc97ec62706527f2b9ee947ca9 Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 4 Aug 2022 14:02:28 +0900 Subject: [PATCH] composite pattern --- DesignPattern/Composite/App.config | 6 ++ DesignPattern/Composite/Client.cs | 35 ++++++++++++ DesignPattern/Composite/Composite.csproj | 57 +++++++++++++++++++ DesignPattern/Composite/Directory.cs | 33 +++++++++++ DesignPattern/Composite/File.cs | 21 +++++++ DesignPattern/Composite/Node.cs | 14 +++++ DesignPattern/Composite/Program.cs | 24 ++++++++ .../Composite/Properties/AssemblyInfo.cs | 36 ++++++++++++ DesignPattern/DesignPattern.sln | 6 ++ 9 files changed, 232 insertions(+) create mode 100644 DesignPattern/Composite/App.config create mode 100644 DesignPattern/Composite/Client.cs create mode 100644 DesignPattern/Composite/Composite.csproj create mode 100644 DesignPattern/Composite/Directory.cs create mode 100644 DesignPattern/Composite/File.cs create mode 100644 DesignPattern/Composite/Node.cs create mode 100644 DesignPattern/Composite/Program.cs create mode 100644 DesignPattern/Composite/Properties/AssemblyInfo.cs diff --git a/DesignPattern/Composite/App.config b/DesignPattern/Composite/App.config new file mode 100644 index 0000000..193aecc --- /dev/null +++ b/DesignPattern/Composite/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DesignPattern/Composite/Client.cs b/DesignPattern/Composite/Client.cs new file mode 100644 index 0000000..a4cacb0 --- /dev/null +++ b/DesignPattern/Composite/Client.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Composite +{ + class Client + { + public static void HowToTest() + { + Directory dir = new Directory("Folder"); + File file1 = new File("a1.doc"); + File file2 = new File("a2.doc"); + File file3 = new File("a3.doc"); + Directory sub = new Directory("SubFolder"); + + dir.AddChildren(file1); + dir.AddChildren(file2); + dir.AddChildren(file3); + dir.AddChildren(sub); + + DisplayNode(file2, dir); + } + + static void DisplayNode(params Node[] component) + { + foreach (Node item in component) + { + item.Display(); + } + } + } +} diff --git a/DesignPattern/Composite/Composite.csproj b/DesignPattern/Composite/Composite.csproj new file mode 100644 index 0000000..2082ed1 --- /dev/null +++ b/DesignPattern/Composite/Composite.csproj @@ -0,0 +1,57 @@ + + + + + Debug + AnyCPU + {52E6E6B5-8A12-4A92-8A38-8697247F4CA2} + Exe + Composite + Composite + v4.8 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/DesignPattern/Composite/Directory.cs b/DesignPattern/Composite/Directory.cs new file mode 100644 index 0000000..ab49e31 --- /dev/null +++ b/DesignPattern/Composite/Directory.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Composite +{ + class Directory : Node + { + private List children = new List(); + + public Directory(string name) + { + Name = name; + } + + public override void Display() + { + Console.WriteLine($"DIR: {Name}"); + foreach (Node comp in children) + { + comp.Display(); + } + } + + public void AddChildren(Node child) + { + if (child != null) + children.Add(child); + } + } +} diff --git a/DesignPattern/Composite/File.cs b/DesignPattern/Composite/File.cs new file mode 100644 index 0000000..d7389de --- /dev/null +++ b/DesignPattern/Composite/File.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Composite +{ + class File : Node + { + public File(string name) + { + Name = name; + } + + public override void Display() + { + Console.WriteLine($"FILE: {Name}"); + } + } +} diff --git a/DesignPattern/Composite/Node.cs b/DesignPattern/Composite/Node.cs new file mode 100644 index 0000000..6a4c4c4 --- /dev/null +++ b/DesignPattern/Composite/Node.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Composite +{ + abstract class Node + { + public string Name { get; protected set; } + public abstract void Display(); + } +} diff --git a/DesignPattern/Composite/Program.cs b/DesignPattern/Composite/Program.cs new file mode 100644 index 0000000..f19d7cd --- /dev/null +++ b/DesignPattern/Composite/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Composite +{ + /* + * 카테고리: 구조 패턴 + * 개요: 객체나 객체들의 그룹을 클라이언트 입장에서 동일하게 취급하고 사용할 수 있게 함. + * 개별 객체(Leaf)나 객체 그룹(Composite)을 트리 구조로 만들어 계층을 표현함. + * 개별 객체나 객체 그룹은 Component 인터페이스를 통해 leaf든 composite이든 구분 없이 클라이언트에 사용됨. + */ + class Program + { + static void Main(string[] args) + { + Client.HowToTest(); + + Console.ReadKey(); + } + } +} diff --git a/DesignPattern/Composite/Properties/AssemblyInfo.cs b/DesignPattern/Composite/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..c5d0b05 --- /dev/null +++ b/DesignPattern/Composite/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("Composite")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Composite")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("52e6e6b5-8a12-4a92-8a38-8697247f4ca2")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/DesignPattern/DesignPattern.sln b/DesignPattern/DesignPattern.sln index c66bcb0..11fbf5f 100644 --- a/DesignPattern/DesignPattern.sln +++ b/DesignPattern/DesignPattern.sln @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Adapter", "Adapter\Adapter. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bridge", "Bridge\Bridge.csproj", "{ABFD30C8-C2C5-4142-9E64-2CC65FEBBF7A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Composite", "Composite\Composite.csproj", "{52E6E6B5-8A12-4A92-8A38-8697247F4CA2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,6 +53,10 @@ Global {ABFD30C8-C2C5-4142-9E64-2CC65FEBBF7A}.Debug|Any CPU.Build.0 = Debug|Any CPU {ABFD30C8-C2C5-4142-9E64-2CC65FEBBF7A}.Release|Any CPU.ActiveCfg = Release|Any CPU {ABFD30C8-C2C5-4142-9E64-2CC65FEBBF7A}.Release|Any CPU.Build.0 = Release|Any CPU + {52E6E6B5-8A12-4A92-8A38-8697247F4CA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {52E6E6B5-8A12-4A92-8A38-8697247F4CA2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {52E6E6B5-8A12-4A92-8A38-8697247F4CA2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {52E6E6B5-8A12-4A92-8A38-8697247F4CA2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE