-
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
dac2335
commit 942b3f3
Showing
25 changed files
with
314 additions
and
129 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 |
---|---|---|
@@ -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
24
DesignPatterns.Visitor/Converters/GigaBytesToBytesConverter.cs
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,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
24
DesignPatterns.Visitor/Converters/KiloBytesToBytesConverter.cs
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,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
24
DesignPatterns.Visitor/Converters/MegaBytesToBytesConverter.cs
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,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
24
DesignPatterns.Visitor/Converters/PetaBytesToBytesConverter.cs
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,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
24
DesignPatterns.Visitor/Converters/TeraBytesToBytesConverter.cs
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,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; | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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; | ||
// } | ||
// } |
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
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,7 @@ | ||
namespace DesignPatterns.Visitor; | ||
|
||
public interface ISizeConverter<in T> where T : ISize | ||
{ | ||
ISize Convert(T size); | ||
bool CanConvert(T 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 |
---|---|---|
@@ -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); | ||
} |
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.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 GitHub Actions / build
|
||
|
||
public ISize Accept(IVisitor<ISize, ISize> 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,6 @@ | ||
namespace DesignPatterns.Visitor.Models; | ||
|
||
public record class SizeInBytes : Size | ||
{ | ||
public override string Unit => "Bytes"; | ||
} |
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.Models; | ||
|
||
public record class SizeInGigaBytes : Size | ||
{ | ||
public override string Unit => "GB"; | ||
} |
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.Models; | ||
|
||
public record class SizeInKiloBytes : Size | ||
{ | ||
public override string Unit => "KB"; | ||
} |
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.Models; | ||
|
||
public record class SizeInMegaBytes : Size | ||
{ | ||
public override string Unit => "MB"; | ||
} |
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.Models; | ||
|
||
public record class SizeInPetaBytes : Size | ||
{ | ||
public override string Unit => "PB"; | ||
} |
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.Models; | ||
|
||
public record class SizeInTeraBytes : Size | ||
{ | ||
public override string Unit => "TB"; | ||
} |
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 |
---|---|---|
@@ -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(); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.