Skip to content

Instantly share code, notes, and snippets.

@kolosovpetro
Created April 9, 2021 18:52
Show Gist options
  • Save kolosovpetro/fe40a589ee6b4c0c3ba5a50c5f165b30 to your computer and use it in GitHub Desktop.
Save kolosovpetro/fe40a589ee6b4c0c3ba5a50c5f165b30 to your computer and use it in GitHub Desktop.
SRP
using System;
namespace SolidRules.SRP
{
// SRP is not violated here
public class SrpFriendly
{
private readonly ConsoleNotifier _notifier = new ConsoleNotifier();
private readonly ConsoleReader _reader = new ConsoleReader();
private readonly StringValidator _validator = new StringValidator();
public void PrintNumber(string text)
{
// notification sent by notifier instance
_notifier.Notify("Enter number to the console");
// input is read by reader class
var result = _reader.Read();
// input validated by the validator class
var isNumber = _validator.Validate(result);
// notification sent by notifier instance
_notifier.Notify(isNumber
? $"The number entered: {result}"
: "Input failed can not be converted to integer. Try again.");
}
}
public class ConsoleNotifier
{
public void Notify(string text) => Console.WriteLine(text);
}
public class ConsoleReader
{
public string Read() => Console.ReadLine();
}
public class StringValidator
{
public bool Validate(string input) => int.TryParse(input, out _);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment