Skip to content

Commit

Permalink
Add Visitor.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Jan 30, 2024
1 parent bec99f7 commit 25036cd
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,6 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml
.DS_Store
.idea/
.idea/.DS_Store
30 changes: 30 additions & 0 deletions DesignPatterns.Visitor/Context.cs
Original file line number Diff line number Diff line change
@@ -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<ISize> itemsInBytes, ICollection<ISize> itemsInGB) GetData()
{
var inBytes = GetSizesInBytes().ToList();
var inGB = GetSizesInGB().ToList();
return (inBytes, inGB);
}

private static IEnumerable<ISize> GetSizesInBytes()
{
yield return new SizeInBytes { Value = 1073741824 };
yield return new SizeInBytes { Value = 2147483648 };
yield return new SizeInBytes { Value = 3221225472 };
}

private static IEnumerable<ISize> GetSizesInGB()
{
yield return new SizeInGB { Value = 10 };
yield return new SizeInGB { Value = 20 };
yield return new SizeInGB { Value = 30 };
}
}
22 changes: 22 additions & 0 deletions DesignPatterns.Visitor/DesignPatterns.Visitor.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<RootNamespace>DesignPatterns.Visitor</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.18.1" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions DesignPatterns.Visitor/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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"]
15 changes: 15 additions & 0 deletions DesignPatterns.Visitor/Handler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace DesignPatterns.Visitor;

public class Handler(IEnumerable<IVisitor> visitors)
{
public IEnumerable<ISize> Process(ICollection<ISize> sizes)
{
var result = new List<ISize>();
foreach (var visitor in visitors)
{
result.AddRange(sizes.Select(size => size.Accept(visitor)));
}

return result;
}
}
8 changes: 8 additions & 0 deletions DesignPatterns.Visitor/ISize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DesignPatterns.Visitor;

public interface ISize
{
double Value { get; }
string Unit { get; }
ISize Accept(IVisitor visitor);
}
6 changes: 6 additions & 0 deletions DesignPatterns.Visitor/IVisitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DesignPatterns.Visitor;

public interface IVisitor
{
ISize Visit(ISize size);
}
10 changes: 10 additions & 0 deletions DesignPatterns.Visitor/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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();
12 changes: 12 additions & 0 deletions DesignPatterns.Visitor/SizeInBytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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);
}
}
12 changes: 12 additions & 0 deletions DesignPatterns.Visitor/SizeInGB.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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);
}
}
34 changes: 34 additions & 0 deletions DesignPatterns.Visitor/SizeVisitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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;
}
}
16 changes: 16 additions & 0 deletions DesignPatterns.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DCE06EB6-BAB5-4573-AF67-128DBCCDB90C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DCE06EB6-BAB5-4573-AF67-128DBCCDB90C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DCE06EB6-BAB5-4573-AF67-128DBCCDB90C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DCE06EB6-BAB5-4573-AF67-128DBCCDB90C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 25036cd

Please sign in to comment.