diff --git a/DesignPattern/Command/App.config b/DesignPattern/Command/App.config new file mode 100644 index 0000000..aee9adf --- /dev/null +++ b/DesignPattern/Command/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DesignPattern/Command/Client.cs b/DesignPattern/Command/Client.cs new file mode 100644 index 0000000..da91383 --- /dev/null +++ b/DesignPattern/Command/Client.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Command +{ + internal class Client + { + public static void HowToUse() + { + // Receiver 객체 + StockBroker broker = new StockBroker(); + + // Invoker 객체 + User user = new User(); + + // Command 객체 + ICommand command = new StockCommand(broker, "MSFT", TxType.Buy, 150); + + user.AddCommand(command); + user.AddCommand(broker, "AMZN", TxType.Sell, 2000); + + // Command 실행 요청 + user.ExecuteAll(); + } + } +} diff --git a/DesignPattern/Command/Command.csproj b/DesignPattern/Command/Command.csproj new file mode 100644 index 0000000..2adb7ad --- /dev/null +++ b/DesignPattern/Command/Command.csproj @@ -0,0 +1,59 @@ + + + + + Debug + AnyCPU + {F34D75D4-7025-4CE3-AF7F-11117FA32F8B} + Exe + Command + Command + 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/Command/ICommand.cs b/DesignPattern/Command/ICommand.cs new file mode 100644 index 0000000..6d62c9a --- /dev/null +++ b/DesignPattern/Command/ICommand.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Command +{ + internal interface ICommand + { + void Execute(); + } +} diff --git a/DesignPattern/Command/Program.cs b/DesignPattern/Command/Program.cs new file mode 100644 index 0000000..a4281eb --- /dev/null +++ b/DesignPattern/Command/Program.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Command +{ + /* + * 카테고리: 행위 패턴 + * 개요: 어떤 행위를 실행하는 요청을 명령 객체로 캡슐화하여 전달함 + * 명령 요청을 즉시 요청할 수도 있지만, 명령 객체를 큐에 쌓아 배치로 한번에 실행할 수도 있고, + * 반대 명령을 내려 Undo 하는 기능을 제공할 수 있음 + * + * Invoker: 명령을 실행할 때를 결정 + * Command: 명령을 표현 + * Receiver: 명령을 받아 일련의 작업을 수행 + */ + internal class Program + { + static void Main(string[] args) + { + Client.HowToUse(); + + Console.ReadKey(); + } + } +} diff --git a/DesignPattern/Command/Properties/AssemblyInfo.cs b/DesignPattern/Command/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..0b26603 --- /dev/null +++ b/DesignPattern/Command/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("Command")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Command")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("f34d75d4-7025-4ce3-af7f-11117fa32f8b")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/DesignPattern/Command/StockBroker.cs b/DesignPattern/Command/StockBroker.cs new file mode 100644 index 0000000..593620a --- /dev/null +++ b/DesignPattern/Command/StockBroker.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Command +{ + internal class StockBroker + { + public void Process(string stockSymbol, TxType txType, int qty) + { + Console.WriteLine($"{txType.ToString()}, {stockSymbol}, {qty}"); + } + } +} diff --git a/DesignPattern/Command/StockCommand.cs b/DesignPattern/Command/StockCommand.cs new file mode 100644 index 0000000..7f61266 --- /dev/null +++ b/DesignPattern/Command/StockCommand.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Command +{ + internal class StockCommand : ICommand + { + private StockBroker _broker; + private string symbol; + private TxType txType; + private int qty; + + public StockCommand(StockBroker broker, string symbol, TxType txType, int qty) + { + _broker = broker; + this.symbol = symbol; + this.txType = txType; + this.qty = qty; + } + + public void Execute() + { + _broker.Process(symbol, txType, qty); + } + } +} diff --git a/DesignPattern/Command/TxType.cs b/DesignPattern/Command/TxType.cs new file mode 100644 index 0000000..b11336e --- /dev/null +++ b/DesignPattern/Command/TxType.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Command +{ + internal enum TxType + { + Buy, + Sell, + } +} diff --git a/DesignPattern/Command/User.cs b/DesignPattern/Command/User.cs new file mode 100644 index 0000000..dfcac2d --- /dev/null +++ b/DesignPattern/Command/User.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Command +{ + internal class User + { + private List _commands = new List(); + + public void AddCommand(ICommand command) + { + _commands.Add(command); + } + + public void AddCommand(StockBroker broker, string symbol, TxType txType, int qty) + { + ICommand command = new StockCommand(broker, symbol, txType, qty); + _commands.Add(command); + } + + public void ExecuteAll() + { + foreach (ICommand cmd in _commands) + { + cmd.Execute(); + } + } + } +} diff --git a/DesignPattern/DesignPattern.sln b/DesignPattern/DesignPattern.sln index abc270a..53ac36d 100644 --- a/DesignPattern/DesignPattern.sln +++ b/DesignPattern/DesignPattern.sln @@ -35,6 +35,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Strategy", "Strategy\Strate EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfResponsibility", "ChainOfResponsibility\ChainOfResponsibility.csproj", "{808A59EA-EF38-4D24-B4C9-ADD93D21EDDC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Command", "Command\Command.csproj", "{F34D75D4-7025-4CE3-AF7F-11117FA32F8B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -105,6 +107,10 @@ Global {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 + {F34D75D4-7025-4CE3-AF7F-11117FA32F8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F34D75D4-7025-4CE3-AF7F-11117FA32F8B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F34D75D4-7025-4CE3-AF7F-11117FA32F8B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F34D75D4-7025-4CE3-AF7F-11117FA32F8B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE