Skip to content

Commit

Permalink
Re-work the Visitor pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Jan 31, 2024
1 parent dac2335 commit 942b3f3
Show file tree
Hide file tree
Showing 25 changed files with 314 additions and 129 deletions.
60 changes: 30 additions & 30 deletions DesignPatterns.Visitor/Context.cs
Original file line number Diff line number Diff line change
@@ -1,30 +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 };
}
}
// using AutoFixture;
//
// namespace DesignPatterns.Visitor;
//
// public class Context
// {
// private readonly IFixture fixture = new Fixture();
// private readonly Random random = new Random();
//
// public (ICollection<IAmount> itemsInBytes, ICollection<IAmount> itemsInGB) GetData()
// {
// var inBytes = GetSizesInBytes().ToList();
// var inGB = GetSizesInGB().ToList();
// return (inBytes, inGB);
// }
//
// private static IEnumerable<IAmount> GetSizesInBytes()
// {
// yield return new AmountInBytes { Value = 1073741824 };
// yield return new AmountInBytes { Value = 2147483648 };
// yield return new AmountInBytes { Value = 3221225472 };
// }
//
// private static IEnumerable<IAmount> GetSizesInGB()
// {
// yield return new AmountInGB { Value = 10 };
// yield return new AmountInGB { Value = 20 };
// yield return new AmountInGB { Value = 30 };
// }
// }
24 changes: 24 additions & 0 deletions DesignPatterns.Visitor/Converters/GigaBytesToBytesConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using DesignPatterns.Visitor.Models;

namespace DesignPatterns.Visitor.Converters;

public class GigaBytesToBytesConverter : ISizeConverter<ISize>
{
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;
}
}
24 changes: 24 additions & 0 deletions DesignPatterns.Visitor/Converters/KiloBytesToBytesConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using DesignPatterns.Visitor.Models;

namespace DesignPatterns.Visitor.Converters;

public class KiloBytesToBytesConverter : ISizeConverter<ISize>
{
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;
}
}
24 changes: 24 additions & 0 deletions DesignPatterns.Visitor/Converters/MegaBytesToBytesConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using DesignPatterns.Visitor.Models;

namespace DesignPatterns.Visitor.Converters;

public class MegaBytesToBytesConverter : ISizeConverter<ISize>
{
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;
}
}
24 changes: 24 additions & 0 deletions DesignPatterns.Visitor/Converters/PetaBytesToBytesConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using DesignPatterns.Visitor.Models;

namespace DesignPatterns.Visitor.Converters;

public class PetaBytesToBytesConverter : ISizeConverter<ISize>
{
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;
}
}
24 changes: 24 additions & 0 deletions DesignPatterns.Visitor/Converters/TeraBytesToBytesConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using DesignPatterns.Visitor.Models;

namespace DesignPatterns.Visitor.Converters;

public class TeraBytesToBytesConverter : ISizeConverter<ISize>
{
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;
}
}
6 changes: 0 additions & 6 deletions DesignPatterns.Visitor/DesignPatterns.Visitor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
<RootNamespace>DesignPatterns.Visitor</RootNamespace>
</PropertyGroup>

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

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.18.1" />
</ItemGroup>
Expand Down
21 changes: 0 additions & 21 deletions DesignPatterns.Visitor/Dockerfile

This file was deleted.

30 changes: 15 additions & 15 deletions DesignPatterns.Visitor/Handler.cs
Original file line number Diff line number Diff line change
@@ -1,15 +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;
}
}
// namespace DesignPatterns.Visitor;
//
// public class Handler(IEnumerable<IVisitor> visitors)
// {
// public IEnumerable<IAmount> Process(ICollection<IAmount> sizes)
// {
// var result = new List<IAmount>();
// foreach (var visitor in visitors)
// {
// result.AddRange(sizes.Select(size => size.Accept(visitor)));
// }
//
// return result;
// }
// }
2 changes: 1 addition & 1 deletion DesignPatterns.Visitor/ISize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public interface ISize
{
double Value { get; }
string Unit { get; }
ISize Accept(IVisitor visitor);
ISize Accept(IVisitor<ISize, ISize> visitor);
}
7 changes: 7 additions & 0 deletions DesignPatterns.Visitor/ISizeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DesignPatterns.Visitor;

public interface ISizeConverter<in T> where T : ISize
{
ISize Convert(T size);
bool CanConvert(T size);
}
4 changes: 2 additions & 2 deletions DesignPatterns.Visitor/IVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DesignPatterns.Visitor;

public interface IVisitor
public interface IVisitor<in TInput, out TResult>
{
ISize Visit(ISize size);
TResult Visit(TInput size);
}
12 changes: 12 additions & 0 deletions DesignPatterns.Visitor/Models/Size.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace DesignPatterns.Visitor.Models;

public record class Size : ISize
{
public double Value { get; init; }
public virtual string Unit { get; init; }

Check warning on line 6 in DesignPatterns.Visitor/Models/Size.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Unit' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 6 in DesignPatterns.Visitor/Models/Size.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Unit' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public ISize Accept(IVisitor<ISize, ISize> visitor)
{
return visitor.Visit(this);
}
}
6 changes: 6 additions & 0 deletions DesignPatterns.Visitor/Models/SizeInBytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DesignPatterns.Visitor.Models;

public record class SizeInBytes : Size
{
public override string Unit => "Bytes";
}
6 changes: 6 additions & 0 deletions DesignPatterns.Visitor/Models/SizeInGigaBytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DesignPatterns.Visitor.Models;

public record class SizeInGigaBytes : Size
{
public override string Unit => "GB";
}
6 changes: 6 additions & 0 deletions DesignPatterns.Visitor/Models/SizeInKiloBytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DesignPatterns.Visitor.Models;

public record class SizeInKiloBytes : Size
{
public override string Unit => "KB";
}
6 changes: 6 additions & 0 deletions DesignPatterns.Visitor/Models/SizeInMegaBytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DesignPatterns.Visitor.Models;

public record class SizeInMegaBytes : Size
{
public override string Unit => "MB";
}
6 changes: 6 additions & 0 deletions DesignPatterns.Visitor/Models/SizeInPetaBytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DesignPatterns.Visitor.Models;

public record class SizeInPetaBytes : Size
{
public override string Unit => "PB";
}
6 changes: 6 additions & 0 deletions DesignPatterns.Visitor/Models/SizeInTeraBytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DesignPatterns.Visitor.Models;

public record class SizeInTeraBytes : Size
{
public override string Unit => "TB";
}
43 changes: 37 additions & 6 deletions DesignPatterns.Visitor/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +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);
// 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<string>
{
"100KB",
"200 KB",
"10 MB",
"200MB",
"1GB",
"20 GB"
};

var visitor = new StringVisitor();

var result = new List<ISize>();
result.AddRange(strings.Select(x => x.Accept(visitor)));

var amountVisitor = new SizeVisitor(
new List<ISizeConverter<ISize>>
{
new KiloBytesToBytesConverter(),
new MegaBytesToBytesConverter(),
new GigaBytesToBytesConverter(),
new TeraBytesToBytesConverter(),
new PetaBytesToBytesConverter(),
});

var newResult = new List<ISize>();
newResult.AddRange(result.Select(x => x.Accept(amountVisitor)));

Console.WriteLine();
12 changes: 0 additions & 12 deletions DesignPatterns.Visitor/SizeInBytes.cs

This file was deleted.

Loading

0 comments on commit 942b3f3

Please sign in to comment.