From c54a987db8f4d13450e4a77605849fd7a7293683 Mon Sep 17 00:00:00 2001 From: syeffort Date: Mon, 3 Apr 2023 17:12:26 +0900 Subject: [PATCH] chain of responsibility --- .../COR/AbstractHandler.cs | 27 +++++++++++++++ .../ChainOfResponsibility/COR/DogHandler.cs | 19 +++++++++++ .../ChainOfResponsibility/COR/IHandler.cs | 15 +++++++++ .../COR/MonkeyHandler.cs | 19 +++++++++++ .../COR/SquirrelHandler.cs | 19 +++++++++++ .../ChainOfResponsibility.csproj | 10 ++++++ .../ChainOfResponsibility/Client.cs | 33 +++++++++++++++++++ .../ChainOfResponsibility/Program.cs | 24 ++++++++++++++ DesignPatternGuru/DesignPatternGuru.sln | 11 ++++++- 9 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 DesignPatternGuru/ChainOfResponsibility/COR/AbstractHandler.cs create mode 100644 DesignPatternGuru/ChainOfResponsibility/COR/DogHandler.cs create mode 100644 DesignPatternGuru/ChainOfResponsibility/COR/IHandler.cs create mode 100644 DesignPatternGuru/ChainOfResponsibility/COR/MonkeyHandler.cs create mode 100644 DesignPatternGuru/ChainOfResponsibility/COR/SquirrelHandler.cs create mode 100644 DesignPatternGuru/ChainOfResponsibility/ChainOfResponsibility.csproj create mode 100644 DesignPatternGuru/ChainOfResponsibility/Client.cs create mode 100644 DesignPatternGuru/ChainOfResponsibility/Program.cs diff --git a/DesignPatternGuru/ChainOfResponsibility/COR/AbstractHandler.cs b/DesignPatternGuru/ChainOfResponsibility/COR/AbstractHandler.cs new file mode 100644 index 0000000..3fcde62 --- /dev/null +++ b/DesignPatternGuru/ChainOfResponsibility/COR/AbstractHandler.cs @@ -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); + } + } +} diff --git a/DesignPatternGuru/ChainOfResponsibility/COR/DogHandler.cs b/DesignPatternGuru/ChainOfResponsibility/COR/DogHandler.cs new file mode 100644 index 0000000..097661f --- /dev/null +++ b/DesignPatternGuru/ChainOfResponsibility/COR/DogHandler.cs @@ -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); + } + } +} diff --git a/DesignPatternGuru/ChainOfResponsibility/COR/IHandler.cs b/DesignPatternGuru/ChainOfResponsibility/COR/IHandler.cs new file mode 100644 index 0000000..fa2cf0b --- /dev/null +++ b/DesignPatternGuru/ChainOfResponsibility/COR/IHandler.cs @@ -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); + } +} diff --git a/DesignPatternGuru/ChainOfResponsibility/COR/MonkeyHandler.cs b/DesignPatternGuru/ChainOfResponsibility/COR/MonkeyHandler.cs new file mode 100644 index 0000000..5992da8 --- /dev/null +++ b/DesignPatternGuru/ChainOfResponsibility/COR/MonkeyHandler.cs @@ -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); + } + } +} diff --git a/DesignPatternGuru/ChainOfResponsibility/COR/SquirrelHandler.cs b/DesignPatternGuru/ChainOfResponsibility/COR/SquirrelHandler.cs new file mode 100644 index 0000000..5746446 --- /dev/null +++ b/DesignPatternGuru/ChainOfResponsibility/COR/SquirrelHandler.cs @@ -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) ; + } + } +} diff --git a/DesignPatternGuru/ChainOfResponsibility/ChainOfResponsibility.csproj b/DesignPatternGuru/ChainOfResponsibility/ChainOfResponsibility.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/DesignPatternGuru/ChainOfResponsibility/ChainOfResponsibility.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/DesignPatternGuru/ChainOfResponsibility/Client.cs b/DesignPatternGuru/ChainOfResponsibility/Client.cs new file mode 100644 index 0000000..ea9a30c --- /dev/null +++ b/DesignPatternGuru/ChainOfResponsibility/Client.cs @@ -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 foods = new List() + { + "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}"); + } + } + } +} diff --git a/DesignPatternGuru/ChainOfResponsibility/Program.cs b/DesignPatternGuru/ChainOfResponsibility/Program.cs new file mode 100644 index 0000000..e970efc --- /dev/null +++ b/DesignPatternGuru/ChainOfResponsibility/Program.cs @@ -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); + + } + } +} \ No newline at end of file diff --git a/DesignPatternGuru/DesignPatternGuru.sln b/DesignPatternGuru/DesignPatternGuru.sln index fa55c50..4e31fad 100644 --- a/DesignPatternGuru/DesignPatternGuru.sln +++ b/DesignPatternGuru/DesignPatternGuru.sln @@ -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}