diff --git a/DesignPattern/DesignPattern.sln b/DesignPattern/DesignPattern.sln index c2d4aa2..93d7f88 100644 --- a/DesignPattern/DesignPattern.sln +++ b/DesignPattern/DesignPattern.sln @@ -39,6 +39,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Command", "Command\Command. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Template", "Template\Template.csproj", "{8C1CA345-A2D4-46E2-9FD0-34E8BE4CE7C4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "State", "State\State.csproj", "{008D8C56-8546-4EB1-83C6-68FAF2051895}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -117,6 +119,10 @@ Global {8C1CA345-A2D4-46E2-9FD0-34E8BE4CE7C4}.Debug|Any CPU.Build.0 = Debug|Any CPU {8C1CA345-A2D4-46E2-9FD0-34E8BE4CE7C4}.Release|Any CPU.ActiveCfg = Release|Any CPU {8C1CA345-A2D4-46E2-9FD0-34E8BE4CE7C4}.Release|Any CPU.Build.0 = Release|Any CPU + {008D8C56-8546-4EB1-83C6-68FAF2051895}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {008D8C56-8546-4EB1-83C6-68FAF2051895}.Debug|Any CPU.Build.0 = Debug|Any CPU + {008D8C56-8546-4EB1-83C6-68FAF2051895}.Release|Any CPU.ActiveCfg = Release|Any CPU + {008D8C56-8546-4EB1-83C6-68FAF2051895}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/DesignPattern/State/App.config b/DesignPattern/State/App.config new file mode 100644 index 0000000..aee9adf --- /dev/null +++ b/DesignPattern/State/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DesignPattern/State/BadState.cs b/DesignPattern/State/BadState.cs new file mode 100644 index 0000000..87c092d --- /dev/null +++ b/DesignPattern/State/BadState.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace State +{ + internal class BadState : State + { + public BadState() + { + this.InterestRate = 35.00M; + } + + public override void ChangeScore(Context context, int score) + { + if (score >= 600) + { + context.CurrentState = new NormalState(); + + context.CurrentState.ChangeScore(context, score); + } + } + } +} diff --git a/DesignPattern/State/Client.cs b/DesignPattern/State/Client.cs new file mode 100644 index 0000000..351e159 --- /dev/null +++ b/DesignPattern/State/Client.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace State +{ + internal class Client + { + public static void HowToTest() + { + Context ctx = new Context(); + ctx.SetCreditScore(550); + Console.WriteLine($"{ctx.CurrentState.InterestRate}%"); + + ctx.SetCreditScore(760); + Console.WriteLine($"{ctx.CurrentState.InterestRate}%"); + } + } +} diff --git a/DesignPattern/State/Context.cs b/DesignPattern/State/Context.cs new file mode 100644 index 0000000..bc97c58 --- /dev/null +++ b/DesignPattern/State/Context.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace State +{ + internal class Context + { + public State CurrentState { get; set; } + + public Context() + { + this.CurrentState = new NormalState(); + } + + public void SetCreditScore(int creditScore) + { + Console.WriteLine($"{creditScore}"); + this.CurrentState.ChangeScore(this, creditScore); + } + } +} diff --git a/DesignPattern/State/NormalState.cs b/DesignPattern/State/NormalState.cs new file mode 100644 index 0000000..430fd8d --- /dev/null +++ b/DesignPattern/State/NormalState.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace State +{ + internal class NormalState : State + { + public NormalState() + { + this.InterestRate = 25.00M; + } + + public override void ChangeScore(Context context, int score) + { + if (score < 600) + { + context.CurrentState = new BadState(); + + context.CurrentState.ChangeScore(context, score); + } + else if (score >= 750) + { + context.CurrentState = new VIPState(); + + context.CurrentState.ChangeScore(context, score); + } + } + } +} diff --git a/DesignPattern/State/Program.cs b/DesignPattern/State/Program.cs new file mode 100644 index 0000000..0e11b43 --- /dev/null +++ b/DesignPattern/State/Program.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace State +{ + /* + * 카테고리: 행위 패턴 + * 개요: 상태별 행위를 별도의 클래스에 구현하여 + * 복잡한 상태별 행위를 효율적으로 분리하여 구현하고 + * 새로운 상태에 따른 행위를 유연하게 추가/확장함 + * + * Context: 현재 상태를 기억 + * State: 인터페이스 + * Concrete State: 각 상태별 행위 구현 + */ + internal class Program + { + static void Main(string[] args) + { + Client.HowToTest(); + + Console.ReadKey(); + } + } +} diff --git a/DesignPattern/State/Properties/AssemblyInfo.cs b/DesignPattern/State/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..fed6ad1 --- /dev/null +++ b/DesignPattern/State/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("State")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("State")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("008d8c56-8546-4eb1-83c6-68faf2051895")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/DesignPattern/State/State.cs b/DesignPattern/State/State.cs new file mode 100644 index 0000000..2f8caca --- /dev/null +++ b/DesignPattern/State/State.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace State +{ + internal abstract class State + { + public decimal InterestRate { get; protected set; } + + public abstract void ChangeScore(Context context, int score); + } +} diff --git a/DesignPattern/State/State.csproj b/DesignPattern/State/State.csproj new file mode 100644 index 0000000..06d3836 --- /dev/null +++ b/DesignPattern/State/State.csproj @@ -0,0 +1,59 @@ + + + + + Debug + AnyCPU + {008D8C56-8546-4EB1-83C6-68FAF2051895} + Exe + State + State + 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/State/VIPState.cs b/DesignPattern/State/VIPState.cs new file mode 100644 index 0000000..48fd93d --- /dev/null +++ b/DesignPattern/State/VIPState.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace State +{ + internal class VIPState : State + { + public VIPState() + { + this.InterestRate = 15.00M; + } + + public override void ChangeScore(Context context, int score) + { + if (score < 750) + { + context.CurrentState = new NormalState(); + + context.CurrentState.ChangeScore(context, score); + } + } + } +}