From bd9e2206634c426f8b421a9ddb85867c58ec1f61 Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 4 Aug 2022 10:42:39 +0900 Subject: [PATCH] object pool pattern --- DesignPattern/DesignPattern.sln | 6 ++ DesignPattern/ObjectPool/App.config | 6 ++ DesignPattern/ObjectPool/Client.cs | 25 +++++++++ DesignPattern/ObjectPool/MyConnection.cs | 13 +++++ DesignPattern/ObjectPool/MyConnectionPool.cs | 29 ++++++++++ DesignPattern/ObjectPool/ObjectPool.csproj | 56 +++++++++++++++++++ DesignPattern/ObjectPool/Program.cs | 22 ++++++++ .../ObjectPool/Properties/AssemblyInfo.cs | 36 ++++++++++++ 8 files changed, 193 insertions(+) create mode 100644 DesignPattern/ObjectPool/App.config create mode 100644 DesignPattern/ObjectPool/Client.cs create mode 100644 DesignPattern/ObjectPool/MyConnection.cs create mode 100644 DesignPattern/ObjectPool/MyConnectionPool.cs create mode 100644 DesignPattern/ObjectPool/ObjectPool.csproj create mode 100644 DesignPattern/ObjectPool/Program.cs create mode 100644 DesignPattern/ObjectPool/Properties/AssemblyInfo.cs diff --git a/DesignPattern/DesignPattern.sln b/DesignPattern/DesignPattern.sln index 9bf7a0f..42e621a 100644 --- a/DesignPattern/DesignPattern.sln +++ b/DesignPattern/DesignPattern.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Factory", "Factory\Factory. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prototype", "Prototype\Prototype.csproj", "{C5EFDAF1-61D2-4B66-BCE2-2A54632F3F7A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObjectPool", "ObjectPool\ObjectPool.csproj", "{64993D9F-0D9E-42B6-B032-800DA2DD453B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {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 + {64993D9F-0D9E-42B6-B032-800DA2DD453B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {64993D9F-0D9E-42B6-B032-800DA2DD453B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {64993D9F-0D9E-42B6-B032-800DA2DD453B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {64993D9F-0D9E-42B6-B032-800DA2DD453B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/DesignPattern/ObjectPool/App.config b/DesignPattern/ObjectPool/App.config new file mode 100644 index 0000000..193aecc --- /dev/null +++ b/DesignPattern/ObjectPool/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DesignPattern/ObjectPool/Client.cs b/DesignPattern/ObjectPool/Client.cs new file mode 100644 index 0000000..5dd066d --- /dev/null +++ b/DesignPattern/ObjectPool/Client.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ObjectPool +{ + class Client + { + public static void HowToUse() + { + MyConnectionPool myPool = new MyConnectionPool(); + MyConnection conn = myPool.GetObject(); + // use conn object + + myPool.ReleaseObject(conn); + + MyConnection conn2 = myPool.GetObject(); + // use conn2 object + + myPool.ReleaseObject(conn2); + } + } +} diff --git a/DesignPattern/ObjectPool/MyConnection.cs b/DesignPattern/ObjectPool/MyConnection.cs new file mode 100644 index 0000000..9d4833a --- /dev/null +++ b/DesignPattern/ObjectPool/MyConnection.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ObjectPool +{ + class MyConnection + { + + } +} diff --git a/DesignPattern/ObjectPool/MyConnectionPool.cs b/DesignPattern/ObjectPool/MyConnectionPool.cs new file mode 100644 index 0000000..85a1ead --- /dev/null +++ b/DesignPattern/ObjectPool/MyConnectionPool.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ObjectPool +{ + class MyConnectionPool + { + private readonly ConcurrentBag pool = new ConcurrentBag(); + + public MyConnection GetObject() + { + MyConnection obj; + if (pool.TryTake(out obj)) + return obj; + else + return new MyConnection(); + } + + public void ReleaseObject(MyConnection conn) + { + pool.Add(conn); + Console.WriteLine($"Release: {conn.GetHashCode()}"); + } + } +} diff --git a/DesignPattern/ObjectPool/ObjectPool.csproj b/DesignPattern/ObjectPool/ObjectPool.csproj new file mode 100644 index 0000000..a967891 --- /dev/null +++ b/DesignPattern/ObjectPool/ObjectPool.csproj @@ -0,0 +1,56 @@ + + + + + Debug + AnyCPU + {64993D9F-0D9E-42B6-B032-800DA2DD453B} + Exe + ObjectPool + ObjectPool + 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/ObjectPool/Program.cs b/DesignPattern/ObjectPool/Program.cs new file mode 100644 index 0000000..63fe798 --- /dev/null +++ b/DesignPattern/ObjectPool/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ObjectPool +{ + /* + * 카테고리: 생성패턴 + * 개요: 객체 생성에 리소스 소모가 커 그 자체가 프로그램의 성능에 영향을 미치는 경우, 미리 만들어두고 사용 + * 객체 요청 시 Pool에 사용 가능한 객체가 있는지 확인하고 없는 경우에만 새로 객체를 생성 + */ + class Program + { + static void Main(string[] args) + { + Client.HowToUse(); + Console.ReadKey(); + } + } +} diff --git a/DesignPattern/ObjectPool/Properties/AssemblyInfo.cs b/DesignPattern/ObjectPool/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..543c468 --- /dev/null +++ b/DesignPattern/ObjectPool/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("ObjectPool")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ObjectPool")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("64993d9f-0d9e-42b6-b032-800da2dd453b")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]