From b3b784113f2a63c25787c21a35daeaea2b3e117d Mon Sep 17 00:00:00 2001 From: syeffort Date: Fri, 7 Apr 2023 15:37:38 +0900 Subject: [PATCH] command --- DesignPatternGuru/Command/Command.csproj | 10 +++++ .../Command/Commands/ComplexCommand.cs | 30 ++++++++++++++ .../Command/Commands/ICommand.cs | 13 ++++++ DesignPatternGuru/Command/Commands/Invoker.cs | 41 +++++++++++++++++++ .../Command/Commands/Receiver.cs | 21 ++++++++++ .../Command/Commands/SimpleCommand.cs | 23 +++++++++++ DesignPatternGuru/Command/Program.cs | 17 ++++++++ DesignPatternGuru/DesignPatternGuru.sln | 9 +++- 8 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 DesignPatternGuru/Command/Command.csproj create mode 100644 DesignPatternGuru/Command/Commands/ComplexCommand.cs create mode 100644 DesignPatternGuru/Command/Commands/ICommand.cs create mode 100644 DesignPatternGuru/Command/Commands/Invoker.cs create mode 100644 DesignPatternGuru/Command/Commands/Receiver.cs create mode 100644 DesignPatternGuru/Command/Commands/SimpleCommand.cs create mode 100644 DesignPatternGuru/Command/Program.cs diff --git a/DesignPatternGuru/Command/Command.csproj b/DesignPatternGuru/Command/Command.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/DesignPatternGuru/Command/Command.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/DesignPatternGuru/Command/Commands/ComplexCommand.cs b/DesignPatternGuru/Command/Commands/ComplexCommand.cs new file mode 100644 index 0000000..96a53ab --- /dev/null +++ b/DesignPatternGuru/Command/Commands/ComplexCommand.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Command.Commands +{ + internal class ComplexCommand : ICommand + { + private Receiver _receiver; + + private string _a; + private string _b; + + public ComplexCommand(Receiver receiver, string a, string b) + { + _receiver = receiver; + _a = a; + _b = b; + } + + public void Execute() + { + Console.WriteLine("ComplexCommand: Complex stuff should be done by a receiver object."); + _receiver.DoSomething(_a); + _receiver.DoSomething(_b); + } + } +} diff --git a/DesignPatternGuru/Command/Commands/ICommand.cs b/DesignPatternGuru/Command/Commands/ICommand.cs new file mode 100644 index 0000000..c405c30 --- /dev/null +++ b/DesignPatternGuru/Command/Commands/ICommand.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Command.Commands +{ + internal interface ICommand + { + void Execute(); + } +} diff --git a/DesignPatternGuru/Command/Commands/Invoker.cs b/DesignPatternGuru/Command/Commands/Invoker.cs new file mode 100644 index 0000000..5c39e86 --- /dev/null +++ b/DesignPatternGuru/Command/Commands/Invoker.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Command.Commands +{ + internal class Invoker + { + private ICommand _onStart; + private ICommand _onFinish; + + public void SetOnStart(ICommand command) + { + _onStart = command; + } + + public void SetOnFinish(ICommand command) + { + _onFinish = command; + } + + public void DoSomethingImportant() + { + Console.WriteLine("Invoker: Does anybody want something done before I begin?"); + if (_onStart != null) + { + _onStart.Execute(); + } + + Console.WriteLine("Invoker: ...doing someting really important..."); + + Console.WriteLine("Invoker: Does anybody want something done after I finish?"); + if (_onFinish != null) + { + _onFinish.Execute(); + } + } + } +} diff --git a/DesignPatternGuru/Command/Commands/Receiver.cs b/DesignPatternGuru/Command/Commands/Receiver.cs new file mode 100644 index 0000000..5cf1113 --- /dev/null +++ b/DesignPatternGuru/Command/Commands/Receiver.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Command.Commands +{ + internal class Receiver + { + public void DoSomething(string a) + { + Console.WriteLine( $"Receiver: Working on ({a})"); + } + + public void DoSometingElse(string b) + { + Console.WriteLine($"Receiver: Also working on ({b})"); + } + } +} diff --git a/DesignPatternGuru/Command/Commands/SimpleCommand.cs b/DesignPatternGuru/Command/Commands/SimpleCommand.cs new file mode 100644 index 0000000..6dd139d --- /dev/null +++ b/DesignPatternGuru/Command/Commands/SimpleCommand.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Command.Commands +{ + internal class SimpleCommand : ICommand + { + private string _payload = string.Empty; + + public SimpleCommand(string payload) + { + _payload = payload; + } + + public void Execute() + { + Console.WriteLine( $"SimpleCommand: See, I can do simple things like printing ({_payload})"); + } + } +} diff --git a/DesignPatternGuru/Command/Program.cs b/DesignPatternGuru/Command/Program.cs new file mode 100644 index 0000000..29653d4 --- /dev/null +++ b/DesignPatternGuru/Command/Program.cs @@ -0,0 +1,17 @@ +using Command.Commands; + +namespace Command +{ + internal class Program + { + static void Main(string[] args) + { + Invoker invoker = new Invoker(); + invoker.SetOnStart(new SimpleCommand("Say Hi!")); + Receiver receiver = new Receiver(); + invoker.SetOnFinish(new ComplexCommand(receiver, "Send email", "Save report")); + + invoker.DoSomethingImportant(); + } + } +} \ No newline at end of file diff --git a/DesignPatternGuru/DesignPatternGuru.sln b/DesignPatternGuru/DesignPatternGuru.sln index 4e31fad..32be10d 100644 --- a/DesignPatternGuru/DesignPatternGuru.sln +++ b/DesignPatternGuru/DesignPatternGuru.sln @@ -33,7 +33,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Proxy", "Proxy\Proxy.csproj EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BehavioralPatterns", "BehavioralPatterns", "{E3B8A482-1A9A-4B6E-8310-050A44C8E379}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfResponsibility", "ChainOfResponsibility\ChainOfResponsibility.csproj", "{9626EF73-1F68-46C8-B7C1-434F02768452}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChainOfResponsibility", "ChainOfResponsibility\ChainOfResponsibility.csproj", "{9626EF73-1F68-46C8-B7C1-434F02768452}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Command", "Command\Command.csproj", "{629A51B1-04D9-4636-8E21-1712DCFF21DF}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -93,6 +95,10 @@ Global {9626EF73-1F68-46C8-B7C1-434F02768452}.Debug|Any CPU.Build.0 = Debug|Any CPU {9626EF73-1F68-46C8-B7C1-434F02768452}.Release|Any CPU.ActiveCfg = Release|Any CPU {9626EF73-1F68-46C8-B7C1-434F02768452}.Release|Any CPU.Build.0 = Release|Any CPU + {629A51B1-04D9-4636-8E21-1712DCFF21DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {629A51B1-04D9-4636-8E21-1712DCFF21DF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {629A51B1-04D9-4636-8E21-1712DCFF21DF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {629A51B1-04D9-4636-8E21-1712DCFF21DF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -111,6 +117,7 @@ Global {D414F5DF-8FB2-4D77-A0C8-77B623A902DD} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} {F0E63700-B550-4D57-B912-18C28B3FEAC6} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} {9626EF73-1F68-46C8-B7C1-434F02768452} = {E3B8A482-1A9A-4B6E-8310-050A44C8E379} + {629A51B1-04D9-4636-8E21-1712DCFF21DF} = {E3B8A482-1A9A-4B6E-8310-050A44C8E379} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC}