Skip to content

Commit 93df0f2

Browse files
Merge pull request #57 from vivopensource/dev
Merges Dev to Main
2 parents 6c47217 + e6ce78c commit 93df0f2

File tree

23 files changed

+636
-52
lines changed

23 files changed

+636
-52
lines changed
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Text;
2-
using Core.Console.Interfaces;
1+
using Core.Console.Interfaces;
32
using Microsoft.Extensions.Logging;
43

54
namespace Core.Extensions;
@@ -8,17 +7,6 @@ internal static class ConsoleExtensions
87
{
98
public static ILoggerFactory GetLoggerFactory() => LoggerFactory.Create(builder => { builder.AddConsole(); });
109

11-
public static void Log<T>(this IConsoleLogger logger, string identifier, IEnumerable<T> options)
12-
{
13-
var message = new StringBuilder($"Please enter from following {identifier}: ");
14-
15-
var i = 0;
16-
foreach (var option in options)
17-
message.Append($"\n {++i}. {option} ");
18-
19-
logger.Log(message.ToString());
20-
}
21-
2210
public static TEnum AcceptInputEnum<TEnum>(this IInputReader inputReader, TEnum defaultValue)
2311
{
2412
var input = inputReader.AcceptInput();
@@ -31,4 +19,9 @@ public static decimal AcceptInputDecimal(this IInputReader inputReader)
3119
var input = inputReader.AcceptInput();
3220
return decimal.Parse(input);
3321
}
22+
23+
public static void LogInfoQuit(this IConsoleLogger logger)
24+
{
25+
logger.Log("Please provide 'quit' input in order to quit from the example.");
26+
}
3427
}

Directory.Packages.props

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project>
3-
<PropertyGroup>
4-
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
5-
<NoWarn>NU1507</NoWarn>
6-
</PropertyGroup>
7-
<ItemGroup Label="Microsoft Nugets">
8-
<PackageVersion Include="coverlet.collector" Version="3.1.2" />
9-
<PackageVersion Include="Microsoft.Extensions.Logging" Version="6.0.0" />
10-
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
11-
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
12-
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
13-
<PackageVersion Include="Moq" Version="4.18.4" />
14-
<PackageVersion Include="NUnit" Version="3.13.3" />
15-
<PackageVersion Include="NUnit3TestAdapter" Version="4.2.1" />
16-
<PackageVersion Include="NUnit.Analyzers" Version="3.3.0" />
17-
<PackageVersion Include="YamlDotNet" Version="13.2.0" />
18-
</ItemGroup>
3+
<PropertyGroup>
4+
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
5+
<NoWarn>NU1507</NoWarn>
6+
</PropertyGroup>
7+
<ItemGroup Label="Microsoft Nugets">
8+
<PackageVersion Include="coverlet.collector" Version="3.1.2" />
9+
<PackageVersion Include="Microsoft.AspNet.WebApi.Client" Version="4.0.20505" />
10+
<PackageVersion Include="Microsoft.Extensions.Logging" Version="6.0.0" />
11+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
12+
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
13+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
14+
<PackageVersion Include="Moq" Version="4.18.4" />
15+
<PackageVersion Include="NUnit" Version="3.13.3" />
16+
<PackageVersion Include="NUnit3TestAdapter" Version="4.2.1" />
17+
<PackageVersion Include="NUnit.Analyzers" Version="3.3.0" />
18+
<PackageVersion Include="YamlDotNet" Version="13.2.0" />
19+
</ItemGroup>
1920
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace GofConsoleApp.Examples.Behavioral.ObserverPattern.Components;
2+
3+
internal enum EnumTopic
4+
{
5+
Sports,
6+
Politics,
7+
Weather,
8+
Holidays,
9+
Invalid,
10+
Quit
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Core.Console.Interfaces;
2+
using GofPatterns.Behavioral.ObserverPattern.Interfaces;
3+
4+
namespace GofConsoleApp.Examples.Behavioral.ObserverPattern.Components;
5+
6+
internal class PersonAndrewSmith : ISubscriber<string>
7+
{
8+
private readonly IConsoleLogger logger;
9+
10+
public PersonAndrewSmith(IConsoleLogger logger)
11+
{
12+
this.logger = logger;
13+
}
14+
15+
public void Update(string input)
16+
{
17+
logger.Log($"Andrew Smith received: {input}");
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Core.Console.Interfaces;
2+
using GofPatterns.Behavioral.ObserverPattern.Interfaces;
3+
4+
namespace GofConsoleApp.Examples.Behavioral.ObserverPattern.Components;
5+
6+
internal class PersonJohnDoe : ISubscriber<string>
7+
{
8+
private readonly IConsoleLogger logger;
9+
10+
public PersonJohnDoe(IConsoleLogger logger)
11+
{
12+
this.logger = logger;
13+
}
14+
15+
public void Update(string input)
16+
{
17+
logger.Log($"John Doe received: {input}");
18+
}
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Core.Extensions;
2+
using GofConsoleApp.Examples.Behavioral.ObserverPattern.Components;
3+
using GofPatterns.Behavioral.ObserverPattern;
4+
using GofPatterns.Behavioral.ObserverPattern.Interfaces;
5+
6+
namespace GofConsoleApp.Examples.Behavioral.ObserverPattern;
7+
8+
internal class ObserverPatternExample : BaseExample
9+
{
10+
private readonly IPublisher<string> newsPublisher = new Publisher<string>();
11+
12+
protected override bool Execute()
13+
{
14+
var johnDoe = new PersonJohnDoe(Logger);
15+
var andrewSmith = new PersonAndrewSmith(Logger);
16+
17+
newsPublisher.AddSubscriber(johnDoe);
18+
newsPublisher.AddSubscriber(andrewSmith);
19+
20+
while (true)
21+
{
22+
Logger.LogInfoQuit();
23+
24+
var newsUpdate = AcceptInputString("news update");
25+
26+
if (newsUpdate.Trim().ToLower().Equals("quit"))
27+
break;
28+
29+
newsPublisher.NotifySubscribers(newsUpdate);
30+
}
31+
32+
return true;
33+
}
34+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Core.Extensions;
2+
using GofConsoleApp.Examples.Behavioral.ObserverPattern.Components;
3+
using GofPatterns.Behavioral.ObserverPattern;
4+
using GofPatterns.Behavioral.ObserverPattern.Interfaces;
5+
6+
namespace GofConsoleApp.Examples.Behavioral.ObserverPattern;
7+
8+
internal class ObserverPatternExampleWithCategory : BaseExample
9+
{
10+
private readonly IPublisher<string, EnumTopic> newsPublisher = new Publisher<string, EnumTopic>();
11+
12+
protected override bool Execute()
13+
{
14+
var johnDoe = new PersonJohnDoe(Logger);
15+
var andrewSmith = new PersonAndrewSmith(Logger);
16+
17+
newsPublisher.AddSubscriber(johnDoe, EnumTopic.Sports);
18+
newsPublisher.AddSubscriber(andrewSmith, EnumTopic.Sports);
19+
newsPublisher.AddSubscriber(johnDoe, EnumTopic.Politics);
20+
newsPublisher.AddSubscriber(andrewSmith, EnumTopic.Weather);
21+
newsPublisher.AddSubscriber(johnDoe, EnumTopic.Holidays);
22+
23+
do
24+
{
25+
Logger.LogInfoQuit();
26+
27+
var topic = AcceptInputEnum(EnumTopic.Invalid, nameof(EnumTopic), EnumTopic.Invalid);
28+
29+
if (IsInvalidOrQuit(topic, EnumTopic.Invalid, EnumTopic.Quit, out _))
30+
return true;
31+
32+
var newsUpdate = AcceptInputString($"{topic} news update");
33+
34+
newsPublisher.NotifySubscribers(newsUpdate, topic);
35+
36+
} while (true);
37+
}
38+
}

GofConsoleApp/Examples/Behavioral/StatePattern/StatePatternBulbExample.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ protected override bool Execute()
1818

1919
do
2020
{
21-
var inputOption = AcceptInputEnum(Invalid, "state", Invalid);
21+
var state = AcceptInputEnum(Invalid, "state", Invalid);
2222

23-
if (IsInvalidOrQuit(inputOption, Invalid, Quit, out var output))
23+
if (IsInvalidOrQuit(state, Invalid, Quit, out var output))
2424
return output;
2525

26-
if (bulb.State.Name.Equals(inputOption.ToString()))
26+
if (bulb.State.Name.Equals(state.ToString()))
2727
{
2828
Logger.Log($"Bulb already in {bulb.State.Name} state.");
2929
continue;
3030
}
3131

32-
switch (inputOption)
32+
switch (state)
3333
{
3434
case On:
3535
bulb.SetState(on);

GofConsoleApp/Examples/ExecutionHelpers/PatternOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using GofConsoleApp.Examples.Behavioral.CommandPattern;
22
using GofConsoleApp.Examples.Behavioral.CorPattern;
33
using GofConsoleApp.Examples.Behavioral.MediatorPattern;
4+
using GofConsoleApp.Examples.Behavioral.ObserverPattern;
45
using GofConsoleApp.Examples.Behavioral.StatePattern;
56
using GofConsoleApp.Examples.Behavioral.StrategyPattern;
67
using GofConsoleApp.Examples.Creational.AbstractFactoryPattern;
@@ -24,6 +25,9 @@ internal static class PatternOptions
2425
internal const string AdapterPatternOption = "13";
2526
internal const string FlyweightPatternOption = "14";
2627
internal const string MediatorPatternOption = "15";
28+
internal const string ObserverPatternOption = "16.1";
29+
internal const string ObserverPatternOptionWithType = "16.2";
30+
2731
internal const string ChainOfResponsibilityPatternOption = "21";
2832
internal const string ChainOfResponsibilityPatternOption2 = "21.2";
2933
internal const string ChainOfResponsibilityPatternOption3 = "21.3";
@@ -33,6 +37,7 @@ internal static class PatternOptions
3337
internal const string StatePatternOptionDriveExample = "23.2";
3438
internal const string StrategyPatternOptionSender = "24";
3539
internal const string StrategyPatternOptionPayment = "24.2";
40+
3641
internal const string FactoryOption = "31";
3742
internal const string AbstractFactoryOption = "32";
3843
internal const string BuilderPatternOption = "33";
@@ -73,6 +78,14 @@ internal static class PatternOptions
7378
MediatorPatternOption,
7479
new PatternExampleMap("Flyweight Pattern >> Drawing shapes", new MediatorPatternExample())
7580
},
81+
{
82+
ObserverPatternOption,
83+
new PatternExampleMap("Observer Pattern >> News Publisher", new ObserverPatternExample())
84+
},
85+
{
86+
ObserverPatternOptionWithType,
87+
new PatternExampleMap("Observer Pattern >> News Publisher with type", new ObserverPatternExampleWithCategory())
88+
},
7689

7790
// Behavioral Patterns
7891
{
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace GofPatterns.Behavioral.ObserverPattern.Exceptions;
2+
3+
public class NoSubscriptionFoundException : Exception
4+
{
5+
public NoSubscriptionFoundException(string message) : base(message) { }
6+
}

0 commit comments

Comments
 (0)