chain of responsibility

main
syeffort 2 years ago
parent ddfbee48f0
commit c54a987db8
  1. 27
      DesignPatternGuru/ChainOfResponsibility/COR/AbstractHandler.cs
  2. 19
      DesignPatternGuru/ChainOfResponsibility/COR/DogHandler.cs
  3. 15
      DesignPatternGuru/ChainOfResponsibility/COR/IHandler.cs
  4. 19
      DesignPatternGuru/ChainOfResponsibility/COR/MonkeyHandler.cs
  5. 19
      DesignPatternGuru/ChainOfResponsibility/COR/SquirrelHandler.cs
  6. 10
      DesignPatternGuru/ChainOfResponsibility/ChainOfResponsibility.csproj
  7. 33
      DesignPatternGuru/ChainOfResponsibility/Client.cs
  8. 24
      DesignPatternGuru/ChainOfResponsibility/Program.cs
  9. 11
      DesignPatternGuru/DesignPatternGuru.sln

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChainOfResponsibility.COR
{
internal abstract class AbstractHandler : IHandler
{
private IHandler _nextHandler;
public IHandler SetNext(IHandler handler)
{
_nextHandler = handler;
return _nextHandler;
}
public virtual object Handle(object request)
{
if (_nextHandler == null)
return null;
return _nextHandler.Handle(request);
}
}
}

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChainOfResponsibility.COR
{
internal class DogHandler : AbstractHandler
{
public override object Handle(object request)
{
if ((request as string) == "MeatBall")
return $"Dog: I'll eat the {request.ToString()}";
else
return base.Handle(request);
}
}
}

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChainOfResponsibility.COR
{
internal interface IHandler
{
IHandler SetNext(IHandler handler);
object Handle(object request);
}
}

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChainOfResponsibility.COR
{
internal class MonkeyHandler : AbstractHandler
{
public override object Handle(object request)
{
if ((request as string) == "Banana")
return $"Monkey: I'll eat the {request.ToString()}";
else
return base.Handle(request);
}
}
}

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChainOfResponsibility.COR
{
internal class SquirrelHandler : AbstractHandler
{
public override object Handle(object request)
{
if ((request as string) == "Nut")
return $"Squirrel: I'll eat the {request.ToString()}";
else
return base.Handle(request) ;
}
}
}

@ -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,33 @@
using ChainOfResponsibility.COR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChainOfResponsibility
{
internal class Client
{
public static void ClientCode(AbstractHandler handler)
{
List<string> foods = new List<string>()
{
"Nut",
"Banana",
"Cup of coffee",
"MeatBall"
};
foreach (string food in foods)
{
Console.WriteLine($"Client: Who wants a {food}");
var result = handler.Handle(food);
if (result == null)
Console.WriteLine($" {food} was left untouched");
else
Console.WriteLine($" {result}");
}
}
}
}

@ -0,0 +1,24 @@
using ChainOfResponsibility.COR;
namespace ChainOfResponsibility
{
internal class Program
{
static void Main(string[] args)
{
var monkey = new MonkeyHandler();
var squirrel = new SquirrelHandler();
var dog = new DogHandler();
monkey.SetNext(squirrel).SetNext(dog);
Console.WriteLine("Chain: Monkey → Squirrel → Dog");
Client.ClientCode(monkey);
Console.WriteLine();
Console.WriteLine("Subchain: Squirrel → Dog");
Client.ClientCode(squirrel);
}
}
}

@ -29,7 +29,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Facade", "Facade\Facade.csp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Flyweight", "Flyweight\Flyweight.csproj", "{D414F5DF-8FB2-4D77-A0C8-77B623A902DD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Proxy", "Proxy\Proxy.csproj", "{F0E63700-B550-4D57-B912-18C28B3FEAC6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Proxy", "Proxy\Proxy.csproj", "{F0E63700-B550-4D57-B912-18C28B3FEAC6}"
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -85,6 +89,10 @@ Global
{F0E63700-B550-4D57-B912-18C28B3FEAC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0E63700-B550-4D57-B912-18C28B3FEAC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0E63700-B550-4D57-B912-18C28B3FEAC6}.Release|Any CPU.Build.0 = Release|Any CPU
{9626EF73-1F68-46C8-B7C1-434F02768452}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -102,6 +110,7 @@ Global
{7D6D306D-BC44-407A-9A39-104C90A3FA39} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1}
{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}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC}

Loading…
Cancel
Save