-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bec99f7
commit 25036cd
Showing
12 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -396,3 +396,6 @@ FodyWeavers.xsd | |
|
||
# JetBrains Rider | ||
*.sln.iml | ||
.DS_Store | ||
.idea/ | ||
.idea/.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace DesignPatterns.Visitor; | ||
|
||
public interface IVisitor | ||
{ | ||
ISize Visit(ISize size); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |