From 538e777dae04ebb38034d66dde3daa1f30b4417e Mon Sep 17 00:00:00 2001 From: eminencegrs Date: Wed, 31 Jan 2024 20:52:13 +0100 Subject: [PATCH] Re-work the Visitor pattern. Add models for KB, MB, GB, TB, PT. Add converters. Update the solution structure. --- Behavioral/DesignPatterns.Visitor/Context.cs | 30 ++++++++++++++ .../Converters/GigaBytesToBytesConverter.cs | 24 +++++++++++ .../Converters/KiloBytesToBytesConverter.cs | 24 +++++++++++ .../Converters/MegaBytesToBytesConverter.cs | 24 +++++++++++ .../Converters/PetaBytesToBytesConverter.cs | 24 +++++++++++ .../Converters/TeraBytesToBytesConverter.cs | 24 +++++++++++ .../DesignPatterns.Visitor.csproj | 6 --- Behavioral/DesignPatterns.Visitor/Handler.cs | 15 +++++++ .../DesignPatterns.Visitor}/ISize.cs | 2 +- .../DesignPatterns.Visitor/ISizeConverter.cs | 7 ++++ Behavioral/DesignPatterns.Visitor/IVisitor.cs | 6 +++ .../DesignPatterns.Visitor/Models/Size.cs | 12 ++++++ .../Models/SizeInBytes.cs | 6 +++ .../Models/SizeInGigaBytes.cs | 6 +++ .../Models/SizeInKiloBytes.cs | 6 +++ .../Models/SizeInMegaBytes.cs | 6 +++ .../Models/SizeInPetaBytes.cs | 6 +++ .../Models/SizeInTeraBytes.cs | 6 +++ Behavioral/DesignPatterns.Visitor/Program.cs | 41 +++++++++++++++++++ .../DesignPatterns.Visitor/SizeVisitor.cs | 19 +++++++++ .../StringExtensions.cs | 9 ++++ .../DesignPatterns.Visitor/StringVisitor.cs | 36 ++++++++++++++++ DesignPatterns.Visitor/Context.cs | 30 -------------- DesignPatterns.Visitor/Dockerfile | 21 ---------- DesignPatterns.Visitor/Handler.cs | 15 ------- DesignPatterns.Visitor/IVisitor.cs | 6 --- DesignPatterns.Visitor/Program.cs | 10 ----- DesignPatterns.Visitor/SizeInBytes.cs | 12 ------ DesignPatterns.Visitor/SizeInGB.cs | 12 ------ DesignPatterns.Visitor/SizeVisitor.cs | 34 --------------- DesignPatterns.sln | 2 +- 31 files changed, 333 insertions(+), 148 deletions(-) create mode 100644 Behavioral/DesignPatterns.Visitor/Context.cs create mode 100644 Behavioral/DesignPatterns.Visitor/Converters/GigaBytesToBytesConverter.cs create mode 100644 Behavioral/DesignPatterns.Visitor/Converters/KiloBytesToBytesConverter.cs create mode 100644 Behavioral/DesignPatterns.Visitor/Converters/MegaBytesToBytesConverter.cs create mode 100644 Behavioral/DesignPatterns.Visitor/Converters/PetaBytesToBytesConverter.cs create mode 100644 Behavioral/DesignPatterns.Visitor/Converters/TeraBytesToBytesConverter.cs rename {DesignPatterns.Visitor => Behavioral/DesignPatterns.Visitor}/DesignPatterns.Visitor.csproj (78%) create mode 100644 Behavioral/DesignPatterns.Visitor/Handler.cs rename {DesignPatterns.Visitor => Behavioral/DesignPatterns.Visitor}/ISize.cs (69%) create mode 100644 Behavioral/DesignPatterns.Visitor/ISizeConverter.cs create mode 100644 Behavioral/DesignPatterns.Visitor/IVisitor.cs create mode 100644 Behavioral/DesignPatterns.Visitor/Models/Size.cs create mode 100644 Behavioral/DesignPatterns.Visitor/Models/SizeInBytes.cs create mode 100644 Behavioral/DesignPatterns.Visitor/Models/SizeInGigaBytes.cs create mode 100644 Behavioral/DesignPatterns.Visitor/Models/SizeInKiloBytes.cs create mode 100644 Behavioral/DesignPatterns.Visitor/Models/SizeInMegaBytes.cs create mode 100644 Behavioral/DesignPatterns.Visitor/Models/SizeInPetaBytes.cs create mode 100644 Behavioral/DesignPatterns.Visitor/Models/SizeInTeraBytes.cs create mode 100644 Behavioral/DesignPatterns.Visitor/Program.cs create mode 100644 Behavioral/DesignPatterns.Visitor/SizeVisitor.cs create mode 100644 Behavioral/DesignPatterns.Visitor/StringExtensions.cs create mode 100644 Behavioral/DesignPatterns.Visitor/StringVisitor.cs delete mode 100644 DesignPatterns.Visitor/Context.cs delete mode 100644 DesignPatterns.Visitor/Dockerfile delete mode 100644 DesignPatterns.Visitor/Handler.cs delete mode 100644 DesignPatterns.Visitor/IVisitor.cs delete mode 100644 DesignPatterns.Visitor/Program.cs delete mode 100644 DesignPatterns.Visitor/SizeInBytes.cs delete mode 100644 DesignPatterns.Visitor/SizeInGB.cs delete mode 100644 DesignPatterns.Visitor/SizeVisitor.cs diff --git a/Behavioral/DesignPatterns.Visitor/Context.cs b/Behavioral/DesignPatterns.Visitor/Context.cs new file mode 100644 index 0000000..c55dea1 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Context.cs @@ -0,0 +1,30 @@ +// using AutoFixture; +// +// namespace DesignPatterns.Visitor; +// +// public class Context +// { +// private readonly IFixture fixture = new Fixture(); +// private readonly Random random = new Random(); +// +// public (ICollection itemsInBytes, ICollection itemsInGB) GetData() +// { +// var inBytes = GetSizesInBytes().ToList(); +// var inGB = GetSizesInGB().ToList(); +// return (inBytes, inGB); +// } +// +// private static IEnumerable GetSizesInBytes() +// { +// yield return new AmountInBytes { Value = 1073741824 }; +// yield return new AmountInBytes { Value = 2147483648 }; +// yield return new AmountInBytes { Value = 3221225472 }; +// } +// +// private static IEnumerable GetSizesInGB() +// { +// yield return new AmountInGB { Value = 10 }; +// yield return new AmountInGB { Value = 20 }; +// yield return new AmountInGB { Value = 30 }; +// } +// } diff --git a/Behavioral/DesignPatterns.Visitor/Converters/GigaBytesToBytesConverter.cs b/Behavioral/DesignPatterns.Visitor/Converters/GigaBytesToBytesConverter.cs new file mode 100644 index 0000000..1128da3 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Converters/GigaBytesToBytesConverter.cs @@ -0,0 +1,24 @@ +using DesignPatterns.Visitor.Models; + +namespace DesignPatterns.Visitor.Converters; + +public class GigaBytesToBytesConverter : ISizeConverter +{ + public ISize Convert(ISize size) + { + if (size is SizeInGigaBytes gigaBytes) + { + return new SizeInBytes + { + Value = gigaBytes.Value * 1024 * 1024 * 1024 + }; + } + + throw new InvalidOperationException("Could not convert GigaBytes to Bytes."); + } + + public bool CanConvert(ISize size) + { + return size is SizeInGigaBytes; + } +} diff --git a/Behavioral/DesignPatterns.Visitor/Converters/KiloBytesToBytesConverter.cs b/Behavioral/DesignPatterns.Visitor/Converters/KiloBytesToBytesConverter.cs new file mode 100644 index 0000000..0822d96 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Converters/KiloBytesToBytesConverter.cs @@ -0,0 +1,24 @@ +using DesignPatterns.Visitor.Models; + +namespace DesignPatterns.Visitor.Converters; + +public class KiloBytesToBytesConverter : ISizeConverter +{ + public ISize Convert(ISize size) + { + if (size is SizeInKiloBytes kiloBytes) + { + return new SizeInBytes + { + Value = kiloBytes.Value * 1024 + }; + } + + throw new InvalidOperationException("Could not convert KiloBytes to Bytes."); + } + + public bool CanConvert(ISize size) + { + return size is SizeInKiloBytes; + } +} diff --git a/Behavioral/DesignPatterns.Visitor/Converters/MegaBytesToBytesConverter.cs b/Behavioral/DesignPatterns.Visitor/Converters/MegaBytesToBytesConverter.cs new file mode 100644 index 0000000..e217ee2 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Converters/MegaBytesToBytesConverter.cs @@ -0,0 +1,24 @@ +using DesignPatterns.Visitor.Models; + +namespace DesignPatterns.Visitor.Converters; + +public class MegaBytesToBytesConverter : ISizeConverter +{ + public ISize Convert(ISize size) + { + if (size is SizeInMegaBytes megaBytes) + { + return new SizeInBytes + { + Value = megaBytes.Value * 1024 * 1024 + }; + } + + throw new InvalidOperationException("Could not convert MegaBytes to Bytes."); + } + + public bool CanConvert(ISize size) + { + return size is SizeInMegaBytes; + } +} diff --git a/Behavioral/DesignPatterns.Visitor/Converters/PetaBytesToBytesConverter.cs b/Behavioral/DesignPatterns.Visitor/Converters/PetaBytesToBytesConverter.cs new file mode 100644 index 0000000..8a7d18a --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Converters/PetaBytesToBytesConverter.cs @@ -0,0 +1,24 @@ +using DesignPatterns.Visitor.Models; + +namespace DesignPatterns.Visitor.Converters; + +public class PetaBytesToBytesConverter : ISizeConverter +{ + public ISize Convert(ISize size) + { + if (size is SizeInPetaBytes petaBytes) + { + return new SizeInBytes + { + Value = petaBytes.Value * 1024 * 1024 * 1024 * 1024 * 1024 + }; + } + + throw new InvalidOperationException("Could not convert PetaBytes to Bytes."); + } + + public bool CanConvert(ISize size) + { + return size is SizeInPetaBytes; + } +} diff --git a/Behavioral/DesignPatterns.Visitor/Converters/TeraBytesToBytesConverter.cs b/Behavioral/DesignPatterns.Visitor/Converters/TeraBytesToBytesConverter.cs new file mode 100644 index 0000000..b1f18ae --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Converters/TeraBytesToBytesConverter.cs @@ -0,0 +1,24 @@ +using DesignPatterns.Visitor.Models; + +namespace DesignPatterns.Visitor.Converters; + +public class TeraBytesToBytesConverter : ISizeConverter +{ + public ISize Convert(ISize size) + { + if (size is SizeInTeraBytes gigaBytes) + { + return new SizeInBytes + { + Value = gigaBytes.Value * 1024 * 1024 * 1024 * 1024 + }; + } + + throw new InvalidOperationException("Could not convert TeraBytes to Bytes."); + } + + public bool CanConvert(ISize size) + { + return size is SizeInTeraBytes; + } +} diff --git a/DesignPatterns.Visitor/DesignPatterns.Visitor.csproj b/Behavioral/DesignPatterns.Visitor/DesignPatterns.Visitor.csproj similarity index 78% rename from DesignPatterns.Visitor/DesignPatterns.Visitor.csproj rename to Behavioral/DesignPatterns.Visitor/DesignPatterns.Visitor.csproj index e6d4746..c34b924 100644 --- a/DesignPatterns.Visitor/DesignPatterns.Visitor.csproj +++ b/Behavioral/DesignPatterns.Visitor/DesignPatterns.Visitor.csproj @@ -9,12 +9,6 @@ DesignPatterns.Visitor - - - .dockerignore - - - diff --git a/Behavioral/DesignPatterns.Visitor/Handler.cs b/Behavioral/DesignPatterns.Visitor/Handler.cs new file mode 100644 index 0000000..576e203 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Handler.cs @@ -0,0 +1,15 @@ +// namespace DesignPatterns.Visitor; +// +// public class Handler(IEnumerable visitors) +// { +// public IEnumerable Process(ICollection sizes) +// { +// var result = new List(); +// foreach (var visitor in visitors) +// { +// result.AddRange(sizes.Select(size => size.Accept(visitor))); +// } +// +// return result; +// } +// } diff --git a/DesignPatterns.Visitor/ISize.cs b/Behavioral/DesignPatterns.Visitor/ISize.cs similarity index 69% rename from DesignPatterns.Visitor/ISize.cs rename to Behavioral/DesignPatterns.Visitor/ISize.cs index 4354b3e..1f46572 100644 --- a/DesignPatterns.Visitor/ISize.cs +++ b/Behavioral/DesignPatterns.Visitor/ISize.cs @@ -4,5 +4,5 @@ public interface ISize { double Value { get; } string Unit { get; } - ISize Accept(IVisitor visitor); + ISize Accept(IVisitor visitor); } diff --git a/Behavioral/DesignPatterns.Visitor/ISizeConverter.cs b/Behavioral/DesignPatterns.Visitor/ISizeConverter.cs new file mode 100644 index 0000000..955d552 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/ISizeConverter.cs @@ -0,0 +1,7 @@ +namespace DesignPatterns.Visitor; + +public interface ISizeConverter where T : ISize +{ + ISize Convert(T size); + bool CanConvert(T size); +} diff --git a/Behavioral/DesignPatterns.Visitor/IVisitor.cs b/Behavioral/DesignPatterns.Visitor/IVisitor.cs new file mode 100644 index 0000000..38e4f4b --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/IVisitor.cs @@ -0,0 +1,6 @@ +namespace DesignPatterns.Visitor; + +public interface IVisitor +{ + TResult Visit(TInput size); +} diff --git a/Behavioral/DesignPatterns.Visitor/Models/Size.cs b/Behavioral/DesignPatterns.Visitor/Models/Size.cs new file mode 100644 index 0000000..541f65b --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Models/Size.cs @@ -0,0 +1,12 @@ +namespace DesignPatterns.Visitor.Models; + +public record class Size : ISize +{ + public double Value { get; init; } + public virtual string Unit { get; init; } + + public ISize Accept(IVisitor visitor) + { + return visitor.Visit(this); + } +} diff --git a/Behavioral/DesignPatterns.Visitor/Models/SizeInBytes.cs b/Behavioral/DesignPatterns.Visitor/Models/SizeInBytes.cs new file mode 100644 index 0000000..b5cdc70 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Models/SizeInBytes.cs @@ -0,0 +1,6 @@ +namespace DesignPatterns.Visitor.Models; + +public record class SizeInBytes : Size +{ + public override string Unit => "Bytes"; +} diff --git a/Behavioral/DesignPatterns.Visitor/Models/SizeInGigaBytes.cs b/Behavioral/DesignPatterns.Visitor/Models/SizeInGigaBytes.cs new file mode 100644 index 0000000..37fadd0 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Models/SizeInGigaBytes.cs @@ -0,0 +1,6 @@ +namespace DesignPatterns.Visitor.Models; + +public record class SizeInGigaBytes : Size +{ + public override string Unit => "GB"; +} diff --git a/Behavioral/DesignPatterns.Visitor/Models/SizeInKiloBytes.cs b/Behavioral/DesignPatterns.Visitor/Models/SizeInKiloBytes.cs new file mode 100644 index 0000000..a960c35 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Models/SizeInKiloBytes.cs @@ -0,0 +1,6 @@ +namespace DesignPatterns.Visitor.Models; + +public record class SizeInKiloBytes : Size +{ + public override string Unit => "KB"; +} diff --git a/Behavioral/DesignPatterns.Visitor/Models/SizeInMegaBytes.cs b/Behavioral/DesignPatterns.Visitor/Models/SizeInMegaBytes.cs new file mode 100644 index 0000000..889bbcf --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Models/SizeInMegaBytes.cs @@ -0,0 +1,6 @@ +namespace DesignPatterns.Visitor.Models; + +public record class SizeInMegaBytes : Size +{ + public override string Unit => "MB"; +} diff --git a/Behavioral/DesignPatterns.Visitor/Models/SizeInPetaBytes.cs b/Behavioral/DesignPatterns.Visitor/Models/SizeInPetaBytes.cs new file mode 100644 index 0000000..591e756 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Models/SizeInPetaBytes.cs @@ -0,0 +1,6 @@ +namespace DesignPatterns.Visitor.Models; + +public record class SizeInPetaBytes : Size +{ + public override string Unit => "PB"; +} diff --git a/Behavioral/DesignPatterns.Visitor/Models/SizeInTeraBytes.cs b/Behavioral/DesignPatterns.Visitor/Models/SizeInTeraBytes.cs new file mode 100644 index 0000000..ae84ede --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Models/SizeInTeraBytes.cs @@ -0,0 +1,6 @@ +namespace DesignPatterns.Visitor.Models; + +public record class SizeInTeraBytes : Size +{ + public override string Unit => "TB"; +} diff --git a/Behavioral/DesignPatterns.Visitor/Program.cs b/Behavioral/DesignPatterns.Visitor/Program.cs new file mode 100644 index 0000000..656251f --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/Program.cs @@ -0,0 +1,41 @@ +using DesignPatterns.Visitor; +using DesignPatterns.Visitor.Converters; + +// var context = new Context(); +// var discountVisitor = new SizeVisitor(); +// var handler = new Handler(new[] { discountVisitor }); +// var data = context.GetData(); +// var convertedInGB = handler.Process(data.itemsInBytes); +// var convertedInBytes = handler.Process(data.itemsInGB); +// +// Console.WriteLine(); + +var strings = new List +{ + "100KB", + "200 KB", + "10 MB", + "200MB", + "1GB", + "20 GB" +}; + +var visitor = new StringVisitor(); + +var result = new List(); +result.AddRange(strings.Select(x => x.Accept(visitor))); + +var amountVisitor = new SizeVisitor( + new List> + { + new KiloBytesToBytesConverter(), + new MegaBytesToBytesConverter(), + new GigaBytesToBytesConverter(), + new TeraBytesToBytesConverter(), + new PetaBytesToBytesConverter(), + }); + +var newResult = new List(); +newResult.AddRange(result.Select(x => x.Accept(amountVisitor))); + +Console.WriteLine(); \ No newline at end of file diff --git a/Behavioral/DesignPatterns.Visitor/SizeVisitor.cs b/Behavioral/DesignPatterns.Visitor/SizeVisitor.cs new file mode 100644 index 0000000..06177c1 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/SizeVisitor.cs @@ -0,0 +1,19 @@ +namespace DesignPatterns.Visitor; + +public class SizeVisitor : IVisitor +{ + private readonly IEnumerable> converters; + + public SizeVisitor(IEnumerable> converters) + { + this.converters = converters; + } + + public ISize Visit(ISize size) + { + var converter = this.converters.FirstOrDefault(x => x.CanConvert(size)); + return converter is null + ? throw new InvalidOperationException("Could not find a proper converter.") + : converter.Convert(size); + } +} diff --git a/Behavioral/DesignPatterns.Visitor/StringExtensions.cs b/Behavioral/DesignPatterns.Visitor/StringExtensions.cs new file mode 100644 index 0000000..b100863 --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/StringExtensions.cs @@ -0,0 +1,9 @@ +namespace DesignPatterns.Visitor; + +public static class StringExtensions +{ + public static ISize Accept(this string value, IVisitor visitor) + { + return visitor.Visit(value); + } +} diff --git a/Behavioral/DesignPatterns.Visitor/StringVisitor.cs b/Behavioral/DesignPatterns.Visitor/StringVisitor.cs new file mode 100644 index 0000000..e859bbc --- /dev/null +++ b/Behavioral/DesignPatterns.Visitor/StringVisitor.cs @@ -0,0 +1,36 @@ +using System.Text.RegularExpressions; +using DesignPatterns.Visitor.Models; + +namespace DesignPatterns.Visitor; + +public class StringVisitor : IVisitor +{ + public ISize Visit(string size) + { + if (string.IsNullOrWhiteSpace(size)) + { + throw new ArgumentException("Input size string cannot be null or empty.", nameof(size)); + } + + var regexPattern = @"^(?\d+(\.\d+)?)\s*(?[KMGTP]?B)$"; + var match = Regex.Match(size, regexPattern, RegexOptions.IgnoreCase); + + if (!match.Success) + { + throw new ArgumentException("Invalid size format.", nameof(size)); + } + + var value = long.Parse(match.Groups["value"].Value); + var unit = match.Groups["unit"].Value.ToUpper(); + + return unit switch + { + "KB" => new SizeInKiloBytes { Value = value }, + "MB" => new SizeInMegaBytes { Value = value }, + "GB" => new SizeInGigaBytes { Value = value }, + "TB" => new SizeInTeraBytes { Value = value }, + "PB" => new SizeInPetaBytes { Value = value }, + _ => throw new ArgumentOutOfRangeException(nameof(size), "Unknown size unit.") + }; + } +} diff --git a/DesignPatterns.Visitor/Context.cs b/DesignPatterns.Visitor/Context.cs deleted file mode 100644 index e775936..0000000 --- a/DesignPatterns.Visitor/Context.cs +++ /dev/null @@ -1,30 +0,0 @@ -using AutoFixture; - -namespace DesignPatterns.Visitor; - -public class Context -{ - private readonly IFixture fixture = new Fixture(); - private readonly Random random = new Random(); - - public (ICollection itemsInBytes, ICollection itemsInGB) GetData() - { - var inBytes = GetSizesInBytes().ToList(); - var inGB = GetSizesInGB().ToList(); - return (inBytes, inGB); - } - - private static IEnumerable GetSizesInBytes() - { - yield return new SizeInBytes { Value = 1073741824 }; - yield return new SizeInBytes { Value = 2147483648 }; - yield return new SizeInBytes { Value = 3221225472 }; - } - - private static IEnumerable GetSizesInGB() - { - yield return new SizeInGB { Value = 10 }; - yield return new SizeInGB { Value = 20 }; - yield return new SizeInGB { Value = 30 }; - } -} diff --git a/DesignPatterns.Visitor/Dockerfile b/DesignPatterns.Visitor/Dockerfile deleted file mode 100644 index 6e49dc3..0000000 --- a/DesignPatterns.Visitor/Dockerfile +++ /dev/null @@ -1,21 +0,0 @@ -FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base -USER $APP_UID -WORKDIR /app - -FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build -ARG BUILD_CONFIGURATION=Release -WORKDIR /src -COPY ["DesignPatterns.Visitor/DesignPatterns.Visitor.csproj", "DesignPatterns.Visitor/"] -RUN dotnet restore "DesignPatterns.Visitor/DesignPatterns.Visitor.csproj" -COPY . . -WORKDIR "/src/DesignPatterns.Visitor" -RUN dotnet build "DesignPatterns.Visitor.csproj" -c $BUILD_CONFIGURATION -o /app/build - -FROM build AS publish -ARG BUILD_CONFIGURATION=Release -RUN dotnet publish "DesignPatterns.Visitor.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false - -FROM base AS final -WORKDIR /app -COPY --from=publish /app/publish . -ENTRYPOINT ["dotnet", "DesignPatterns.Visitor.dll"] diff --git a/DesignPatterns.Visitor/Handler.cs b/DesignPatterns.Visitor/Handler.cs deleted file mode 100644 index 095744b..0000000 --- a/DesignPatterns.Visitor/Handler.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace DesignPatterns.Visitor; - -public class Handler(IEnumerable visitors) -{ - public IEnumerable Process(ICollection sizes) - { - var result = new List(); - foreach (var visitor in visitors) - { - result.AddRange(sizes.Select(size => size.Accept(visitor))); - } - - return result; - } -} diff --git a/DesignPatterns.Visitor/IVisitor.cs b/DesignPatterns.Visitor/IVisitor.cs deleted file mode 100644 index 85e38b3..0000000 --- a/DesignPatterns.Visitor/IVisitor.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace DesignPatterns.Visitor; - -public interface IVisitor -{ - ISize Visit(ISize size); -} diff --git a/DesignPatterns.Visitor/Program.cs b/DesignPatterns.Visitor/Program.cs deleted file mode 100644 index 7ae35d8..0000000 --- a/DesignPatterns.Visitor/Program.cs +++ /dev/null @@ -1,10 +0,0 @@ -using DesignPatterns.Visitor; - -var context = new Context(); -var discountVisitor = new SizeVisitor(); -var handler = new Handler(new[] { discountVisitor }); -var data = context.GetData(); -var convertedInGB = handler.Process(data.itemsInBytes); -var convertedInBytes = handler.Process(data.itemsInGB); - -Console.WriteLine(); \ No newline at end of file diff --git a/DesignPatterns.Visitor/SizeInBytes.cs b/DesignPatterns.Visitor/SizeInBytes.cs deleted file mode 100644 index 4581fb6..0000000 --- a/DesignPatterns.Visitor/SizeInBytes.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace DesignPatterns.Visitor; - -public record class SizeInBytes : ISize -{ - public double Value { get; init; } - public string Unit => "Bytes"; - - public ISize Accept(IVisitor visitor) - { - return visitor.Visit(this); - } -} diff --git a/DesignPatterns.Visitor/SizeInGB.cs b/DesignPatterns.Visitor/SizeInGB.cs deleted file mode 100644 index c633587..0000000 --- a/DesignPatterns.Visitor/SizeInGB.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace DesignPatterns.Visitor; - -public record class SizeInGB : ISize -{ - public double Value { get; init; } - public string Unit => "GB"; - - public ISize Accept(IVisitor visitor) - { - return visitor.Visit(this); - } -} diff --git a/DesignPatterns.Visitor/SizeVisitor.cs b/DesignPatterns.Visitor/SizeVisitor.cs deleted file mode 100644 index aae3655..0000000 --- a/DesignPatterns.Visitor/SizeVisitor.cs +++ /dev/null @@ -1,34 +0,0 @@ -namespace DesignPatterns.Visitor; - -public class SizeVisitor : IVisitor -{ - public ISize Visit(ISize size) - { - return size switch - { - SizeInBytes bytes => ConvertToGb(bytes), - SizeInGB gigaBytes => ConvertToBytes(gigaBytes), - _ => throw new ArgumentOutOfRangeException(nameof(size), "Could not recognize a unit.") - }; - } - - private static ISize ConvertToGb(SizeInBytes size) - { - var result = new SizeInGB - { - Value = size.Value / (1024 * 1024 * 1024.0) - }; - - return result; - } - - private static ISize ConvertToBytes(SizeInGB size) - { - var result = new SizeInBytes - { - Value = size.Value * (1024 * 1024 * 1024.0) - }; - - return result; - } -} diff --git a/DesignPatterns.sln b/DesignPatterns.sln index 20328dc..2f0933d 100644 --- a/DesignPatterns.sln +++ b/DesignPatterns.sln @@ -1,6 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Visitor", "DesignPatterns.Visitor\DesignPatterns.Visitor.csproj", "{DCE06EB6-BAB5-4573-AF67-128DBCCDB90C}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Visitor", "Behavioral\DesignPatterns.Visitor\DesignPatterns.Visitor.csproj", "{DCE06EB6-BAB5-4573-AF67-128DBCCDB90C}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Behavioral", "Behavioral", "{7FF0E83F-EBEB-4103-BCD1-2F100E82FCD1}" EndProject