main
syeffort 2 years ago
parent 56857c00d2
commit 1f9ad6fffc
  1. 9
      DesignPatternGuru/DesignPatternGuru.sln
  2. 24
      DesignPatternGuru/Mediator/Components/BaseComponent.cs
  3. 23
      DesignPatternGuru/Mediator/Components/Component1.cs
  4. 24
      DesignPatternGuru/Mediator/Components/Component2.cs
  5. 10
      DesignPatternGuru/Mediator/Mediator.csproj
  6. 40
      DesignPatternGuru/Mediator/Mediators/ConcreteMediator.cs
  7. 13
      DesignPatternGuru/Mediator/Mediators/IMediator.cs
  8. 23
      DesignPatternGuru/Mediator/Program.cs

@ -37,7 +37,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChainOfResponsibility", "Ch
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Command", "Command\Command.csproj", "{629A51B1-04D9-4636-8E21-1712DCFF21DF}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Command", "Command\Command.csproj", "{629A51B1-04D9-4636-8E21-1712DCFF21DF}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Iterator", "Iterator\Iterator.csproj", "{6619433E-476B-404C-94A0-D0C0FE52DE41}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Iterator", "Iterator\Iterator.csproj", "{6619433E-476B-404C-94A0-D0C0FE52DE41}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mediator", "Mediator\Mediator.csproj", "{08D296F6-6698-4FCB-B605-318D9381F975}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -105,6 +107,10 @@ Global
{6619433E-476B-404C-94A0-D0C0FE52DE41}.Debug|Any CPU.Build.0 = Debug|Any CPU {6619433E-476B-404C-94A0-D0C0FE52DE41}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6619433E-476B-404C-94A0-D0C0FE52DE41}.Release|Any CPU.ActiveCfg = Release|Any CPU {6619433E-476B-404C-94A0-D0C0FE52DE41}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6619433E-476B-404C-94A0-D0C0FE52DE41}.Release|Any CPU.Build.0 = Release|Any CPU {6619433E-476B-404C-94A0-D0C0FE52DE41}.Release|Any CPU.Build.0 = Release|Any CPU
{08D296F6-6698-4FCB-B605-318D9381F975}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{08D296F6-6698-4FCB-B605-318D9381F975}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08D296F6-6698-4FCB-B605-318D9381F975}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08D296F6-6698-4FCB-B605-318D9381F975}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -125,6 +131,7 @@ Global
{9626EF73-1F68-46C8-B7C1-434F02768452} = {E3B8A482-1A9A-4B6E-8310-050A44C8E379} {9626EF73-1F68-46C8-B7C1-434F02768452} = {E3B8A482-1A9A-4B6E-8310-050A44C8E379}
{629A51B1-04D9-4636-8E21-1712DCFF21DF} = {E3B8A482-1A9A-4B6E-8310-050A44C8E379} {629A51B1-04D9-4636-8E21-1712DCFF21DF} = {E3B8A482-1A9A-4B6E-8310-050A44C8E379}
{6619433E-476B-404C-94A0-D0C0FE52DE41} = {E3B8A482-1A9A-4B6E-8310-050A44C8E379} {6619433E-476B-404C-94A0-D0C0FE52DE41} = {E3B8A482-1A9A-4B6E-8310-050A44C8E379}
{08D296F6-6698-4FCB-B605-318D9381F975} = {E3B8A482-1A9A-4B6E-8310-050A44C8E379}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC} SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC}

@ -0,0 +1,24 @@
using Mediator.Mediators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mediator.Components
{
internal class BaseComponent
{
protected IMediator _mediator;
public BaseComponent(IMediator mediator = null)
{
_mediator = mediator;
}
public void SetMediator(IMediator mediator)
{
_mediator = mediator;
}
}
}

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mediator.Components
{
internal class Component1 : BaseComponent
{
public void DoA()
{
Console.WriteLine("Component1 does A.");
_mediator.Notify(this, "A");
}
public void DoB()
{
Console.WriteLine("Component1 does B.");
_mediator.Notify(this, "B");
}
}
}

@ -0,0 +1,24 @@
using Mediator.Mediators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mediator.Components
{
internal class Component2 : BaseComponent
{
public void DoC()
{
Console.WriteLine("Component2 does C.");
_mediator.Notify(this, "C");
}
public void DoD()
{
Console.WriteLine("Component2 does D.");
_mediator.Notify(this, "D");
}
}
}

@ -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,40 @@
using Mediator.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mediator.Mediators
{
internal class ConcreteMediator : IMediator
{
private Component1 _component1;
private Component2 _component2;
public ConcreteMediator(Component1 component1, Component2 component2)
{
_component1 = component1;
_component2 = component2;
_component1.SetMediator(this);
_component2.SetMediator(this);
}
public void Notify(object sender, string ev)
{
if (ev == "A")
{
Console.WriteLine("Mediator reacts on A and triggers following operations: ");
_component2.DoC();
}
if (ev == "D")
{
Console.WriteLine("Mediator reacts on D and triggers following operations: ");
_component1.DoB();
_component2.DoC();
}
}
}
}

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mediator.Mediators
{
internal interface IMediator
{
void Notify(object sender, string ev);
}
}

@ -0,0 +1,23 @@
using Mediator.Components;
using Mediator.Mediators;
namespace Mediator
{
internal class Program
{
static void Main(string[] args)
{
Component1 component1 = new Component1();
Component2 component2 = new Component2();
new ConcreteMediator(component1, component2);
Console.WriteLine("Client triggers operation A.");
component1.DoA();
Console.WriteLine();
Console.WriteLine("Client triggers operation D.");
component2.DoD();
}
}
}
Loading…
Cancel
Save