From 3d8d1058edbdfb472ac51e09c01da1b08df7fc8b Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 29 Feb 2024 14:06:48 +0900 Subject: [PATCH] command --- DesignPattern/DesignPattern.sln | 22 +++++ .../DesignPattern/DesignPattern.csproj | 10 +++ .../DesignPattern/Patterns/CommandPattern.cs | 84 +++++++++++++++++++ .../DesignPattern/Patterns/FactoryPattern.cs | 75 +++++++++++++++++ DesignPattern/DesignPattern/Program.cs | 13 +++ 5 files changed, 204 insertions(+) create mode 100644 DesignPattern/DesignPattern.sln create mode 100644 DesignPattern/DesignPattern/DesignPattern.csproj create mode 100644 DesignPattern/DesignPattern/Patterns/CommandPattern.cs create mode 100644 DesignPattern/DesignPattern/Patterns/FactoryPattern.cs create mode 100644 DesignPattern/DesignPattern/Program.cs diff --git a/DesignPattern/DesignPattern.sln b/DesignPattern/DesignPattern.sln new file mode 100644 index 0000000..83c458b --- /dev/null +++ b/DesignPattern/DesignPattern.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPattern", "DesignPattern\DesignPattern.csproj", "{E7881A12-006C-4464-8ADE-BC952AE99FEF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E7881A12-006C-4464-8ADE-BC952AE99FEF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E7881A12-006C-4464-8ADE-BC952AE99FEF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E7881A12-006C-4464-8ADE-BC952AE99FEF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E7881A12-006C-4464-8ADE-BC952AE99FEF}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/DesignPattern/DesignPattern/DesignPattern.csproj b/DesignPattern/DesignPattern/DesignPattern.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/DesignPattern/DesignPattern/DesignPattern.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/DesignPattern/DesignPattern/Patterns/CommandPattern.cs b/DesignPattern/DesignPattern/Patterns/CommandPattern.cs new file mode 100644 index 0000000..4f75a06 --- /dev/null +++ b/DesignPattern/DesignPattern/Patterns/CommandPattern.cs @@ -0,0 +1,84 @@ +namespace DesignPattern.Patterns +{ + interface ICommand + { + void Execute(); + } + + 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 cmd = new StockCommand(broker, symbol, txType, qty); + _commands.Add(cmd); + } + + public void ExecuteAll() + { + foreach (var cmd in _commands) + { + cmd.Execute(); + } + } + } + + 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; + _symbol = symbol; + _txType = txType; + _qty = qty; + } + + public void Execute() + { + _broker.Process(_symbol, _txType, _qty); + } + } + + enum TxType + { + Buy, + Sell + } + + class StockBroker + { + public void Process(string stockSymbol, TxType txType, int qty) + { + Console.WriteLine($"{txType}, {stockSymbol}, {qty}"); + } + } + + class CommandClient + { + public static void Work() + { + var broker = new StockBroker(); + + User user = new User(); + + ICommand cmd = new StockCommand(broker, "MSFT", TxType.Buy, 150); + user.AddCommand(cmd); + user.AddCommand(broker, "AMZN", TxType.Sell, 2000); + user.AddCommand(broker, "BRK.B", TxType.Buy, 5000); + user.AddCommand(broker, "APPL", TxType.Sell, 1000); + + user.ExecuteAll(); + } + } +} \ No newline at end of file diff --git a/DesignPattern/DesignPattern/Patterns/FactoryPattern.cs b/DesignPattern/DesignPattern/Patterns/FactoryPattern.cs new file mode 100644 index 0000000..dcec24f --- /dev/null +++ b/DesignPattern/DesignPattern/Patterns/FactoryPattern.cs @@ -0,0 +1,75 @@ +using System.Runtime.CompilerServices; + +namespace DesignPattern.Patterns +{ + public interface IProduct + { + string Operation(); + } + + abstract class Creator + { + public abstract IProduct FactoryMethod(); + + public string SomeOperation() + { + var product = FactoryMethod(); + + var result = $"Creator: The same creator's code has just worked with {product.Operation()}"; + + return result; + } + } + + class ContreteProduct1 : IProduct + { + public string Operation() + { + return $"[Result of {this.GetType().Name}]"; + } + } + + class ContreteProduct2 : IProduct + { + public string Operation() + { + return $"[Result of {this.GetType().Name}]"; + } + } + + class ConcreteCreator1 : Creator + { + public override IProduct FactoryMethod() + { + return new ContreteProduct1(); + } + } + + class ConcreteCreator2 : Creator + { + public override IProduct FactoryMethod() + { + return new ContreteProduct2(); + } + } + + class FactoryClient + { + public static void Work() + { + Console.WriteLine("App: Launch with the Creator1"); + ClientCode(new ConcreteCreator1()); + + Console.WriteLine(); + + Console.WriteLine("App: Launch with the Creator2"); + ClientCode(new ConcreteCreator2()); + } + + public static void ClientCode(Creator creator) + { + Console.WriteLine($"Client: I'm not aware of the creator's class, but it still works."); + Console.WriteLine(creator.SomeOperation()); + } + } +} \ No newline at end of file diff --git a/DesignPattern/DesignPattern/Program.cs b/DesignPattern/DesignPattern/Program.cs new file mode 100644 index 0000000..3ae68b7 --- /dev/null +++ b/DesignPattern/DesignPattern/Program.cs @@ -0,0 +1,13 @@ +using DesignPattern.Patterns; + +namespace DesignPattern; + +class Program +{ + static void Main(string[] args) + { + // FactoryClient.Work(); + + CommandClient.Work(); + } +}