Add decorator
This commit is contained in:
parent
a02e3390b6
commit
8b810b3c82
7 changed files with 42 additions and 17 deletions
|
@ -1,6 +1,4 @@
|
||||||
using System;
|
public class BadCakeProgram
|
||||||
|
|
||||||
public class BadCakeProgram
|
|
||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
@ -124,10 +122,3 @@ public class BadCakeProgram
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
16
Decorator/DecoratedComponent.cs
Normal file
16
Decorator/DecoratedComponent.cs
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
10
Decorator/Decorator.csproj
Normal file
10
Decorator/Decorator.csproj
Normal 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
4
Decorator/IComponent.cs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
public interface IComponent
|
||||||
|
{
|
||||||
|
void DoSomething();
|
||||||
|
}
|
7
Decorator/OriginalComponent.cs
Normal file
7
Decorator/OriginalComponent.cs
Normal 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
4
Decorator/Program.cs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
var component = new OriginalComponent();
|
||||||
|
var decorated = new DecoratedComponent(component);
|
||||||
|
|
||||||
|
decorated.DoSomething();
|
|
@ -102,10 +102,3 @@ public class StrategyCakeProgram
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue