From ebe6726e2f9d6d8ba4a7018fc23f7fed49a064bd Mon Sep 17 00:00:00 2001 From: syneffort Date: Wed, 3 Aug 2022 17:47:11 +0900 Subject: [PATCH] Prototype pattern --- DesignPattern/DesignPattern.sln | 6 ++ DesignPattern/Prototype/App.config | 6 ++ DesignPattern/Prototype/BasicWebPrototype.cs | 22 +++++++ DesignPattern/Prototype/Client.cs | 29 +++++++++ .../Prototype/PremiumWebPrototype.cs | 22 +++++++ DesignPattern/Prototype/Program.cs | 23 +++++++ .../Prototype/Properties/AssemblyInfo.cs | 36 +++++++++++ DesignPattern/Prototype/Prototype.cs | 21 +++++++ DesignPattern/Prototype/Prototype.csproj | 63 +++++++++++++++++++ DesignPattern/Prototype/basic.template | 1 + DesignPattern/Prototype/premium.template | 1 + 11 files changed, 230 insertions(+) create mode 100644 DesignPattern/Prototype/App.config create mode 100644 DesignPattern/Prototype/BasicWebPrototype.cs create mode 100644 DesignPattern/Prototype/Client.cs create mode 100644 DesignPattern/Prototype/PremiumWebPrototype.cs create mode 100644 DesignPattern/Prototype/Program.cs create mode 100644 DesignPattern/Prototype/Properties/AssemblyInfo.cs create mode 100644 DesignPattern/Prototype/Prototype.cs create mode 100644 DesignPattern/Prototype/Prototype.csproj create mode 100644 DesignPattern/Prototype/basic.template create mode 100644 DesignPattern/Prototype/premium.template diff --git a/DesignPattern/DesignPattern.sln b/DesignPattern/DesignPattern.sln index 0eca858..9bf7a0f 100644 --- a/DesignPattern/DesignPattern.sln +++ b/DesignPattern/DesignPattern.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Builder", "Builder\Builder. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Factory", "Factory\Factory.csproj", "{26FB406E-7EC9-4E6B-B115-68013563C935}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prototype", "Prototype\Prototype.csproj", "{C5EFDAF1-61D2-4B66-BCE2-2A54632F3F7A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {26FB406E-7EC9-4E6B-B115-68013563C935}.Debug|Any CPU.Build.0 = Debug|Any CPU {26FB406E-7EC9-4E6B-B115-68013563C935}.Release|Any CPU.ActiveCfg = Release|Any CPU {26FB406E-7EC9-4E6B-B115-68013563C935}.Release|Any CPU.Build.0 = Release|Any CPU + {C5EFDAF1-61D2-4B66-BCE2-2A54632F3F7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C5EFDAF1-61D2-4B66-BCE2-2A54632F3F7A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C5EFDAF1-61D2-4B66-BCE2-2A54632F3F7A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C5EFDAF1-61D2-4B66-BCE2-2A54632F3F7A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/DesignPattern/Prototype/App.config b/DesignPattern/Prototype/App.config new file mode 100644 index 0000000..193aecc --- /dev/null +++ b/DesignPattern/Prototype/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DesignPattern/Prototype/BasicWebPrototype.cs b/DesignPattern/Prototype/BasicWebPrototype.cs new file mode 100644 index 0000000..54ef5de --- /dev/null +++ b/DesignPattern/Prototype/BasicWebPrototype.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Prototype +{ + class BasicWebPrototype : Prototype + { + public BasicWebPrototype() + { + WebTemplate = File.ReadAllText("basic.template"); + } + + public override Prototype Clone() + { + return (Prototype)this.MemberwiseClone(); + } + } +} diff --git a/DesignPattern/Prototype/Client.cs b/DesignPattern/Prototype/Client.cs new file mode 100644 index 0000000..87b64e9 --- /dev/null +++ b/DesignPattern/Prototype/Client.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Prototype +{ + class Client + { + public static void HowToTest() + { + Prototype basicTemplate = new BasicWebPrototype(); + Prototype premiumTemplate = new PremiumWebPrototype(); + + Prototype t1 = basicTemplate.Clone(); + t1.SetTitle("Basic Web"); + Console.WriteLine(t1.WebTemplate); + + Prototype t2 = basicTemplate.Clone(); + t2.SetTitle("Basic Web2"); + Console.WriteLine(t2.WebTemplate); + + Prototype t3 = premiumTemplate.Clone(); + t3.SetTitle("Premium Web"); + Console.WriteLine(t3.WebTemplate); + } + } +} diff --git a/DesignPattern/Prototype/PremiumWebPrototype.cs b/DesignPattern/Prototype/PremiumWebPrototype.cs new file mode 100644 index 0000000..eb0f03f --- /dev/null +++ b/DesignPattern/Prototype/PremiumWebPrototype.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Prototype +{ + class PremiumWebPrototype : Prototype + { + public PremiumWebPrototype() + { + WebTemplate = File.ReadAllText("premium.template"); + } + + public override Prototype Clone() + { + return (Prototype)this.MemberwiseClone(); + } + } +} diff --git a/DesignPattern/Prototype/Program.cs b/DesignPattern/Prototype/Program.cs new file mode 100644 index 0000000..d641684 --- /dev/null +++ b/DesignPattern/Prototype/Program.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Prototype +{ + /* + * 카테고리: 생성패턴 + * 개요: 복잡한 객체를 생성하는 비용이 클 때 사용. + * 미리 원형 객체를 만들어두고 이를 복제하여 새 객체를 생성함. + */ + class Program + { + static void Main(string[] args) + { + Client.HowToTest(); + + Console.ReadKey(); + } + } +} diff --git a/DesignPattern/Prototype/Properties/AssemblyInfo.cs b/DesignPattern/Prototype/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ca24d0b --- /dev/null +++ b/DesignPattern/Prototype/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("Prototype")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Prototype")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("c5efdaf1-61d2-4b66-bce2-2a54632f3f7a")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/DesignPattern/Prototype/Prototype.cs b/DesignPattern/Prototype/Prototype.cs new file mode 100644 index 0000000..b9c14a7 --- /dev/null +++ b/DesignPattern/Prototype/Prototype.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Prototype +{ + public abstract class Prototype + { + // 필요에 따라 deep, shallow copy 선택하여 구현 + public abstract Prototype Clone(); + + public string WebTemplate { get; protected set; } + + public void SetTitle(string title) + { + this.WebTemplate = this.WebTemplate.Replace("{{title}}", title); + } + } +} diff --git a/DesignPattern/Prototype/Prototype.csproj b/DesignPattern/Prototype/Prototype.csproj new file mode 100644 index 0000000..3f34e97 --- /dev/null +++ b/DesignPattern/Prototype/Prototype.csproj @@ -0,0 +1,63 @@ + + + + + Debug + AnyCPU + {C5EFDAF1-61D2-4B66-BCE2-2A54632F3F7A} + Exe + Prototype + Prototype + v4.8 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + copy "$(ProjectDir)\basic.template" "$(targetDir)" +copy "$(ProjectDir)\premium.template" "$(targetDir)" + + \ No newline at end of file diff --git a/DesignPattern/Prototype/basic.template b/DesignPattern/Prototype/basic.template new file mode 100644 index 0000000..88490ce --- /dev/null +++ b/DesignPattern/Prototype/basic.template @@ -0,0 +1 @@ +{{title}} \ No newline at end of file diff --git a/DesignPattern/Prototype/premium.template b/DesignPattern/Prototype/premium.template new file mode 100644 index 0000000..88490ce --- /dev/null +++ b/DesignPattern/Prototype/premium.template @@ -0,0 +1 @@ +{{title}} \ No newline at end of file