main
syneffort 2 years ago
parent ab7df4e5c4
commit 305b348674
  1. 17
      DesignPatternGuru/Decorator/Client.cs
  2. 13
      DesignPatternGuru/Decorator/Components/Component.cs
  3. 16
      DesignPatternGuru/Decorator/Components/ConcreteComponent.cs
  4. 10
      DesignPatternGuru/Decorator/Decorator.csproj
  5. 21
      DesignPatternGuru/Decorator/Decorators/ConcreteDecoratorA.cs
  6. 21
      DesignPatternGuru/Decorator/Decorators/ConcreteDecoratorB.cs
  7. 32
      DesignPatternGuru/Decorator/Decorators/Decorator.cs
  8. 24
      DesignPatternGuru/Decorator/Program.cs
  9. 9
      DesignPatternGuru/DesignPatternGuru.sln

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save