diff --git a/DesignPattern/ChainOfResponsibility/AddHandler.cs b/DesignPattern/ChainOfResponsibility/AddHandler.cs new file mode 100644 index 0000000..0d73dbe --- /dev/null +++ b/DesignPattern/ChainOfResponsibility/AddHandler.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ChainOfResponsibility +{ + internal class AddHandler : CalcHandler + { + public override int Calculate(CalcType cType, int a, int b) + { + if (cType == CalcType.Add) + return a + b; + else + return Next.Calculate(cType, a, b); + } + } +} diff --git a/DesignPattern/ChainOfResponsibility/App.config b/DesignPattern/ChainOfResponsibility/App.config new file mode 100644 index 0000000..aee9adf --- /dev/null +++ b/DesignPattern/ChainOfResponsibility/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DesignPattern/ChainOfResponsibility/CalcHandler.cs b/DesignPattern/ChainOfResponsibility/CalcHandler.cs new file mode 100644 index 0000000..5fec663 --- /dev/null +++ b/DesignPattern/ChainOfResponsibility/CalcHandler.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ChainOfResponsibility +{ + internal abstract class CalcHandler + { + public CalcHandler Next { get; set; } + public abstract int Calculate(CalcType cType, int a, int b); + } +} diff --git a/DesignPattern/ChainOfResponsibility/CalcType.cs b/DesignPattern/ChainOfResponsibility/CalcType.cs new file mode 100644 index 0000000..d7e22dd --- /dev/null +++ b/DesignPattern/ChainOfResponsibility/CalcType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ChainOfResponsibility +{ + internal enum CalcType + { + Add, + Subtract, + Multiply, + Divide, + } +} diff --git a/DesignPattern/ChainOfResponsibility/ChainOfResponsibility.csproj b/DesignPattern/ChainOfResponsibility/ChainOfResponsibility.csproj new file mode 100644 index 0000000..b6cb52f --- /dev/null +++ b/DesignPattern/ChainOfResponsibility/ChainOfResponsibility.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {808A59EA-EF38-4D24-B4C9-ADD93D21EDDC} + Exe + ChainOfResponsibility + ChainOfResponsibility + v4.8.1 + 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/ChainOfResponsibility/Client.cs b/DesignPattern/ChainOfResponsibility/Client.cs new file mode 100644 index 0000000..9899e85 --- /dev/null +++ b/DesignPattern/ChainOfResponsibility/Client.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ChainOfResponsibility +{ + internal class Client + { + public static void HowToUse() + { + CalcHandler add = new AddHandler(); + CalcHandler sub = new SubtractHandler(); + CalcHandler mul = new MultiplyHandler(); + CalcHandler div = new DivideHandler(); + + add.Next = sub; + sub.Next = mul; + mul.Next = div; + + int result = add.Calculate(CalcType.Multiply, 10, 20); + Console.WriteLine(result); + } + } +} diff --git a/DesignPattern/ChainOfResponsibility/DivideHandler.cs b/DesignPattern/ChainOfResponsibility/DivideHandler.cs new file mode 100644 index 0000000..d0ff9f1 --- /dev/null +++ b/DesignPattern/ChainOfResponsibility/DivideHandler.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ChainOfResponsibility +{ + internal class DivideHandler : CalcHandler + { + public override int Calculate(CalcType cType, int a, int b) + { + if (cType == CalcType.Divide) + return a + b; + else + return Next.Calculate(cType, a, b); + } + } +} diff --git a/DesignPattern/ChainOfResponsibility/MultiplyHandler.cs b/DesignPattern/ChainOfResponsibility/MultiplyHandler.cs new file mode 100644 index 0000000..95cd74c --- /dev/null +++ b/DesignPattern/ChainOfResponsibility/MultiplyHandler.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ChainOfResponsibility +{ + internal class MultiplyHandler : CalcHandler + { + public override int Calculate(CalcType cType, int a, int b) + { + if (cType == CalcType.Multiply) + return a * b; + else + return Next.Calculate(cType, a, b); + } + } +} diff --git a/DesignPattern/ChainOfResponsibility/Program.cs b/DesignPattern/ChainOfResponsibility/Program.cs new file mode 100644 index 0000000..79bbb0b --- /dev/null +++ b/DesignPattern/ChainOfResponsibility/Program.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ChainOfResponsibility +{ + /* + * 카테고리: 행위 패턴 + * 개요: 명령을 처리하기 위해 명령을 체인을 따라 전달하며 + * 그 명령을 수행할 수 있도록 체인으로 연결된 객체에서 처리함 + * 각 체인의 객체는 명령을 수행해보고 처리할 수 없으면 다음 객체로 넘기며 + * 명령은 체인의 처음부터 끝까지 계속 이동하게 됨 + */ + internal class Program + { + static void Main(string[] args) + { + Client.HowToUse(); + + Console.ReadKey(); + } + } +} diff --git a/DesignPattern/ChainOfResponsibility/Properties/AssemblyInfo.cs b/DesignPattern/ChainOfResponsibility/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..34bd36f --- /dev/null +++ b/DesignPattern/ChainOfResponsibility/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("ChainOfResponsibility")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ChainOfResponsibility")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("808a59ea-ef38-4d24-b4c9-add93d21eddc")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/DesignPattern/ChainOfResponsibility/SubtractHandler.cs b/DesignPattern/ChainOfResponsibility/SubtractHandler.cs new file mode 100644 index 0000000..8e819ac --- /dev/null +++ b/DesignPattern/ChainOfResponsibility/SubtractHandler.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ChainOfResponsibility +{ + internal class SubtractHandler : CalcHandler + { + public override int Calculate(CalcType cType, int a, int b) + { + if (cType == CalcType.Subtract) + return a - b; + else + return Next.Calculate(cType, a, b); + } + } +} diff --git a/DesignPattern/DesignPattern.sln b/DesignPattern/DesignPattern.sln index 003116b..abc270a 100644 --- a/DesignPattern/DesignPattern.sln +++ b/DesignPattern/DesignPattern.sln @@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Observer", "Observer\Observ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Strategy", "Strategy\Strategy.csproj", "{D6B55743-F509-4B6C-820A-DB8BC5FA0CE5}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfResponsibility", "ChainOfResponsibility\ChainOfResponsibility.csproj", "{808A59EA-EF38-4D24-B4C9-ADD93D21EDDC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -99,6 +101,10 @@ Global {D6B55743-F509-4B6C-820A-DB8BC5FA0CE5}.Debug|Any CPU.Build.0 = Debug|Any CPU {D6B55743-F509-4B6C-820A-DB8BC5FA0CE5}.Release|Any CPU.ActiveCfg = Release|Any CPU {D6B55743-F509-4B6C-820A-DB8BC5FA0CE5}.Release|Any CPU.Build.0 = Release|Any CPU + {808A59EA-EF38-4D24-B4C9-ADD93D21EDDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {808A59EA-EF38-4D24-B4C9-ADD93D21EDDC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {808A59EA-EF38-4D24-B4C9-ADD93D21EDDC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {808A59EA-EF38-4D24-B4C9-ADD93D21EDDC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE