-
-
Save suzdalnitski/fa3cd5baec7e041d87d6d252eac88e79 to your computer and use it in GitHub Desktop.
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
// before refactoring: | |
public class CalculatorForm { | |
private string aText, bText; | |
private bool IsValidInput(string text) => true; | |
private void btnAddClick(object sender, EventArgs e) { | |
if ( !IsValidInput(bText) || !IsValidInput(aText) ) { | |
return; | |
} | |
} | |
} | |
// after refactoring: | |
public class CalculatorForm { | |
private string aText, bText; | |
private readonly IInputValidator _inputValidator; | |
public CalculatorForm(IInputValidator inputValidator) { | |
_inputValidator = inputValidator; | |
} | |
private void btnAddClick(object sender, EventArgs e) { | |
if ( !_inputValidator.IsValidInput(bText) | |
|| !_inputValidator.IsValidInput(aText) ) { | |
return; | |
} | |
} | |
} | |
public interface IInputValidator { | |
bool IsValidInput(string text); | |
} | |
public class InputValidator : IInputValidator { | |
public bool IsValidInput(string text) => true; | |
} | |
public class InputValidatorFactory { | |
public IInputValidator CreateInputValidator() => new InputValidator(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment