main
syeffort 2 years ago
parent c54a987db8
commit b3b784113f
  1. 10
      DesignPatternGuru/Command/Command.csproj
  2. 30
      DesignPatternGuru/Command/Commands/ComplexCommand.cs
  3. 13
      DesignPatternGuru/Command/Commands/ICommand.cs
  4. 41
      DesignPatternGuru/Command/Commands/Invoker.cs
  5. 21
      DesignPatternGuru/Command/Commands/Receiver.cs
  6. 23
      DesignPatternGuru/Command/Commands/SimpleCommand.cs
  7. 17
      DesignPatternGuru/Command/Program.cs
  8. 9
      DesignPatternGuru/DesignPatternGuru.sln

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -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);
}
}
}

@ -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();
}
}

@ -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();
}
}
}
}

@ -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})");
}
}
}

@ -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})");
}
}
}

@ -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();
}
}
}

@ -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}

Loading…
Cancel
Save