From 8b810b3c8247cfd048664243abb5a4c2b02c6477 Mon Sep 17 00:00:00 2001 From: Alex Hyett Date: Mon, 4 Sep 2023 09:55:08 +0100 Subject: [PATCH] Add decorator --- BadCakeProgram/Program.cs | 11 +---------- Decorator/DecoratedComponent.cs | 16 ++++++++++++++++ Decorator/Decorator.csproj | 10 ++++++++++ Decorator/IComponent.cs | 4 ++++ Decorator/OriginalComponent.cs | 7 +++++++ Decorator/Program.cs | 4 ++++ Strategy/Program.cs | 7 ------- 7 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 Decorator/DecoratedComponent.cs create mode 100644 Decorator/Decorator.csproj create mode 100644 Decorator/IComponent.cs create mode 100644 Decorator/OriginalComponent.cs create mode 100644 Decorator/Program.cs diff --git a/BadCakeProgram/Program.cs b/BadCakeProgram/Program.cs index 0334d7d..9f1e5b4 100644 --- a/BadCakeProgram/Program.cs +++ b/BadCakeProgram/Program.cs @@ -1,6 +1,4 @@ -using System; - -public class BadCakeProgram +public class BadCakeProgram { public static void Main(string[] args) { @@ -124,10 +122,3 @@ public class BadCakeProgram } } - - - - - - - diff --git a/Decorator/DecoratedComponent.cs b/Decorator/DecoratedComponent.cs new file mode 100644 index 0000000..86b60f6 --- /dev/null +++ b/Decorator/DecoratedComponent.cs @@ -0,0 +1,16 @@ +public class DecoratedComponent : IComponent +{ + private readonly IComponent _component; + + public DecoratedComponent(IComponent component) + { + _component = component ?? throw new ArgumentNullException(nameof(component)); + } + + public void DoSomething() + { + Console.WriteLine("I can add functionality before"); + _component.DoSomething(); + Console.WriteLine("I can add functionality after"); + } +} \ No newline at end of file diff --git a/Decorator/Decorator.csproj b/Decorator/Decorator.csproj new file mode 100644 index 0000000..d439800 --- /dev/null +++ b/Decorator/Decorator.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/Decorator/IComponent.cs b/Decorator/IComponent.cs new file mode 100644 index 0000000..24457fc --- /dev/null +++ b/Decorator/IComponent.cs @@ -0,0 +1,4 @@ +public interface IComponent +{ + void DoSomething(); +} \ No newline at end of file diff --git a/Decorator/OriginalComponent.cs b/Decorator/OriginalComponent.cs new file mode 100644 index 0000000..f293bbc --- /dev/null +++ b/Decorator/OriginalComponent.cs @@ -0,0 +1,7 @@ +public class OriginalComponent : IComponent +{ + public void DoSomething() + { + Console.WriteLine("I am the original component doing something important"); + } +} \ No newline at end of file diff --git a/Decorator/Program.cs b/Decorator/Program.cs new file mode 100644 index 0000000..63216d1 --- /dev/null +++ b/Decorator/Program.cs @@ -0,0 +1,4 @@ +var component = new OriginalComponent(); +var decorated = new DecoratedComponent(component); + +decorated.DoSomething(); diff --git a/Strategy/Program.cs b/Strategy/Program.cs index 8ac5312..9381e47 100644 --- a/Strategy/Program.cs +++ b/Strategy/Program.cs @@ -102,10 +102,3 @@ public class StrategyCakeProgram } } } - - - - - - -