diff --git a/DesignPatternGuru/Decorator/Client.cs b/DesignPatternGuru/Decorator/Client.cs new file mode 100644 index 0000000..110d71d --- /dev/null +++ b/DesignPatternGuru/Decorator/Client.cs @@ -0,0 +1,17 @@ +using Decorator.Components; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Decorator +{ + internal class Client + { + public void ClientCode(Component component) + { + Console.WriteLine($"RESULT: {component.Operation()}"); + } + } +} diff --git a/DesignPatternGuru/Decorator/Components/Component.cs b/DesignPatternGuru/Decorator/Components/Component.cs new file mode 100644 index 0000000..4a88d9b --- /dev/null +++ b/DesignPatternGuru/Decorator/Components/Component.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Decorator.Components +{ + public abstract class Component + { + public abstract string Operation(); + } +} diff --git a/DesignPatternGuru/Decorator/Components/ConcreteComponent.cs b/DesignPatternGuru/Decorator/Components/ConcreteComponent.cs new file mode 100644 index 0000000..e3ea1e8 --- /dev/null +++ b/DesignPatternGuru/Decorator/Components/ConcreteComponent.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Decorator.Components +{ + internal class ConcreteComponent : Component + { + public override string Operation() + { + return "ConcreteComponent"; + } + } +} diff --git a/DesignPatternGuru/Decorator/Decorator.csproj b/DesignPatternGuru/Decorator/Decorator.csproj new file mode 100644 index 0000000..9ad2a07 --- /dev/null +++ b/DesignPatternGuru/Decorator/Decorator.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/DesignPatternGuru/Decorator/Decorators/ConcreteDecoratorA.cs b/DesignPatternGuru/Decorator/Decorators/ConcreteDecoratorA.cs new file mode 100644 index 0000000..6d2052a --- /dev/null +++ b/DesignPatternGuru/Decorator/Decorators/ConcreteDecoratorA.cs @@ -0,0 +1,21 @@ +using Decorator.Components; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Decorator.Decorators +{ + internal class ConcreteDecoratorA : Decorator + { + public ConcreteDecoratorA(Component component) : base(component) + { + } + + public override string Operation() + { + return $"ConcreateDecoratorA({base.Operation()})"; + } + } +} diff --git a/DesignPatternGuru/Decorator/Decorators/ConcreteDecoratorB.cs b/DesignPatternGuru/Decorator/Decorators/ConcreteDecoratorB.cs new file mode 100644 index 0000000..13efaed --- /dev/null +++ b/DesignPatternGuru/Decorator/Decorators/ConcreteDecoratorB.cs @@ -0,0 +1,21 @@ +using Decorator.Components; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Decorator.Decorators +{ + internal class ConcreteDecoratorB : Decorator + { + public ConcreteDecoratorB(Component component) : base(component) + { + } + + public override string Operation() + { + return $"ConcreteDecoratorB({base.Operation()})"; + } + } +} diff --git a/DesignPatternGuru/Decorator/Decorators/Decorator.cs b/DesignPatternGuru/Decorator/Decorators/Decorator.cs new file mode 100644 index 0000000..2432284 --- /dev/null +++ b/DesignPatternGuru/Decorator/Decorators/Decorator.cs @@ -0,0 +1,32 @@ +using Decorator.Components; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Decorator.Decorators +{ + abstract class Decorator : Component + { + protected Component _component; + + public Decorator(Component component) + { + _component = component; + } + + public void SetComponent(Component component) + { + _component = component; + } + + public override string Operation() + { + if (_component == null) + return String.Empty; + + return _component.Operation(); + } + } +} diff --git a/DesignPatternGuru/Decorator/Program.cs b/DesignPatternGuru/Decorator/Program.cs new file mode 100644 index 0000000..7514a90 --- /dev/null +++ b/DesignPatternGuru/Decorator/Program.cs @@ -0,0 +1,24 @@ +using Decorator.Components; +using Decorator.Decorators; +using System.ComponentModel; + +namespace Decorator +{ + internal class Program + { + static void Main(string[] args) + { + Client client = new Client(); + + ConcreteComponent simple = new ConcreteComponent(); + Console.WriteLine("Client: I get a simple component:"); + client.ClientCode(simple); + Console.WriteLine(); + + ConcreteDecoratorA decorator1 = new ConcreteDecoratorA(simple); + ConcreteDecoratorB decorator2 = new ConcreteDecoratorB(decorator1); + Console.WriteLine("Client: Now I've got a decorated component:"); + client.ClientCode(decorator2); + } + } +} \ No newline at end of file diff --git a/DesignPatternGuru/DesignPatternGuru.sln b/DesignPatternGuru/DesignPatternGuru.sln index c7b9a4d..113485f 100644 --- a/DesignPatternGuru/DesignPatternGuru.sln +++ b/DesignPatternGuru/DesignPatternGuru.sln @@ -21,7 +21,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Adapter", "Adapter\Adapter. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bridge", "Bridge\Bridge.csproj", "{60D1EE6E-282D-4613-8B1D-DE355768DCBA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Composite", "Composite\Composite.csproj", "{8EAAC7FD-ED5E-4567-B1C1-F79AC79C9820}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Composite", "Composite\Composite.csproj", "{8EAAC7FD-ED5E-4567-B1C1-F79AC79C9820}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Decorator", "Decorator\Decorator.csproj", "{8AA3ECFD-4F9A-433D-884C-E76E5050AFBF}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -61,6 +63,10 @@ Global {8EAAC7FD-ED5E-4567-B1C1-F79AC79C9820}.Debug|Any CPU.Build.0 = Debug|Any CPU {8EAAC7FD-ED5E-4567-B1C1-F79AC79C9820}.Release|Any CPU.ActiveCfg = Release|Any CPU {8EAAC7FD-ED5E-4567-B1C1-F79AC79C9820}.Release|Any CPU.Build.0 = Release|Any CPU + {8AA3ECFD-4F9A-433D-884C-E76E5050AFBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8AA3ECFD-4F9A-433D-884C-E76E5050AFBF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8AA3ECFD-4F9A-433D-884C-E76E5050AFBF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8AA3ECFD-4F9A-433D-884C-E76E5050AFBF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -74,6 +80,7 @@ Global {4E8B1160-0BD2-42FD-9DB8-991CAA88578A} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} {60D1EE6E-282D-4613-8B1D-DE355768DCBA} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} {8EAAC7FD-ED5E-4567-B1C1-F79AC79C9820} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} + {8AA3ECFD-4F9A-433D-884C-E76E5050AFBF} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC}