diff --git a/DesignPatternGuru/DesignPatternGuru.sln b/DesignPatternGuru/DesignPatternGuru.sln index 6ddec7a..88b71c6 100644 --- a/DesignPatternGuru/DesignPatternGuru.sln +++ b/DesignPatternGuru/DesignPatternGuru.sln @@ -37,7 +37,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChainOfResponsibility", "Ch EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Command", "Command\Command.csproj", "{629A51B1-04D9-4636-8E21-1712DCFF21DF}" 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 Global 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}.Release|Any CPU.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -125,6 +131,7 @@ Global {9626EF73-1F68-46C8-B7C1-434F02768452} = {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} + {08D296F6-6698-4FCB-B605-318D9381F975} = {E3B8A482-1A9A-4B6E-8310-050A44C8E379} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC} diff --git a/DesignPatternGuru/Mediator/Components/BaseComponent.cs b/DesignPatternGuru/Mediator/Components/BaseComponent.cs new file mode 100644 index 0000000..7936791 --- /dev/null +++ b/DesignPatternGuru/Mediator/Components/BaseComponent.cs @@ -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; + } + } +} diff --git a/DesignPatternGuru/Mediator/Components/Component1.cs b/DesignPatternGuru/Mediator/Components/Component1.cs new file mode 100644 index 0000000..aba670b --- /dev/null +++ b/DesignPatternGuru/Mediator/Components/Component1.cs @@ -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"); + } + } +} diff --git a/DesignPatternGuru/Mediator/Components/Component2.cs b/DesignPatternGuru/Mediator/Components/Component2.cs new file mode 100644 index 0000000..5d37d61 --- /dev/null +++ b/DesignPatternGuru/Mediator/Components/Component2.cs @@ -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"); + } + } +} diff --git a/DesignPatternGuru/Mediator/Mediator.csproj b/DesignPatternGuru/Mediator/Mediator.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/DesignPatternGuru/Mediator/Mediator.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/DesignPatternGuru/Mediator/Mediators/ConcreteMediator.cs b/DesignPatternGuru/Mediator/Mediators/ConcreteMediator.cs new file mode 100644 index 0000000..9063d23 --- /dev/null +++ b/DesignPatternGuru/Mediator/Mediators/ConcreteMediator.cs @@ -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(); + } + } + } +} diff --git a/DesignPatternGuru/Mediator/Mediators/IMediator.cs b/DesignPatternGuru/Mediator/Mediators/IMediator.cs new file mode 100644 index 0000000..cb3e786 --- /dev/null +++ b/DesignPatternGuru/Mediator/Mediators/IMediator.cs @@ -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); + } +} diff --git a/DesignPatternGuru/Mediator/Program.cs b/DesignPatternGuru/Mediator/Program.cs new file mode 100644 index 0000000..b1426d0 --- /dev/null +++ b/DesignPatternGuru/Mediator/Program.cs @@ -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(); + } + } +} \ No newline at end of file