Add decorator

This commit is contained in:
Alex Hyett 2023-09-04 09:55:08 +01:00
parent a02e3390b6
commit 8b810b3c82
7 changed files with 42 additions and 17 deletions

View file

@ -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
}
}

View file

@ -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");
}
}

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

4
Decorator/IComponent.cs Normal file
View file

@ -0,0 +1,4 @@
public interface IComponent
{
void DoSomething();
}

View file

@ -0,0 +1,7 @@
public class OriginalComponent : IComponent
{
public void DoSomething()
{
Console.WriteLine("I am the original component doing something important");
}
}

4
Decorator/Program.cs Normal file
View file

@ -0,0 +1,4 @@
var component = new OriginalComponent();
var decorated = new DecoratedComponent(component);
decorated.DoSomething();

View file

@ -102,10 +102,3 @@ public class StrategyCakeProgram
}
}
}