在不违反 SOLID 原则的情况下添加条件逻辑 C#
Adding a conditional logic without violating SOLID principles C#
我有如下接口和实现。
如果一个数字可以被除数整除,它将显示名为 "Divisible" 的内容。
现在新的增强功能出现了,我需要根据时间更改文本。
如果数字可整除且时间为 12:00PM,则显示 "Divisible ***"。如果时间不是“12:PM”,则显示旧值 i:e "Divisible"。
我知道可以做到,但条件是不能违反SOLID principle.Whether 我做的设计错了吗?请提出建议。
public interface IRule
{
string GetResult(int number);
}
public class DivisibleRule : IRule
{
private readonly int divisor;
private readonly string contentToDisplay;
private readonly string replacementContent;
public DivisibleRule(int divisor, string contentToDisplay)
{
this.divisor = divisor;
this.contentToDisplay = contentToDisplay;
}
/// <summary>
/// Gets the result.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>Returns the content if divisible.</returns>
public string GetResult(int input)
{
return input % this.divisor == 0
? this.contentToDisplay
: string.Empty;
}
}
如果您想在不修改现有代码的情况下添加此功能(这基本上是 Open/closed principle is about) then you can add a decorator 将新的条件逻辑应用于从现有 DivisibleRule
返回的结果。然后在适当的地方你可以使用 DivisibleRule
用你的装饰器装饰。
这个装饰器看起来像这样:
public class RuleTimeDecorator : IRule
{
private readonly IRule _decoratedRule;
public RuleTimeDecorator(IRule decoratedRule)
{
_decoratedRule = decoratedRule;
}
public string GetResult(int input)
{
var result = _decoratedRule.GetResult(input);
return IsMidnight()? $"{result} ***" : result;
}
private bool IsMidnight() => //here goes the code to check if current time meets criteria
}
不错的是,这个装饰器可以用来装饰 IRule
的任何其他植入(只要它在您的域中有意义)。
顺便说一句,我正在使用一些 C#6 功能,例如字符串插值和表达式主体成员,但这不是必需的。
我有如下接口和实现。 如果一个数字可以被除数整除,它将显示名为 "Divisible" 的内容。 现在新的增强功能出现了,我需要根据时间更改文本。 如果数字可整除且时间为 12:00PM,则显示 "Divisible ***"。如果时间不是“12:PM”,则显示旧值 i:e "Divisible"。 我知道可以做到,但条件是不能违反SOLID principle.Whether 我做的设计错了吗?请提出建议。
public interface IRule
{
string GetResult(int number);
}
public class DivisibleRule : IRule
{
private readonly int divisor;
private readonly string contentToDisplay;
private readonly string replacementContent;
public DivisibleRule(int divisor, string contentToDisplay)
{
this.divisor = divisor;
this.contentToDisplay = contentToDisplay;
}
/// <summary>
/// Gets the result.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>Returns the content if divisible.</returns>
public string GetResult(int input)
{
return input % this.divisor == 0
? this.contentToDisplay
: string.Empty;
}
}
如果您想在不修改现有代码的情况下添加此功能(这基本上是 Open/closed principle is about) then you can add a decorator 将新的条件逻辑应用于从现有 DivisibleRule
返回的结果。然后在适当的地方你可以使用 DivisibleRule
用你的装饰器装饰。
这个装饰器看起来像这样:
public class RuleTimeDecorator : IRule
{
private readonly IRule _decoratedRule;
public RuleTimeDecorator(IRule decoratedRule)
{
_decoratedRule = decoratedRule;
}
public string GetResult(int input)
{
var result = _decoratedRule.GetResult(input);
return IsMidnight()? $"{result} ***" : result;
}
private bool IsMidnight() => //here goes the code to check if current time meets criteria
}
不错的是,这个装饰器可以用来装饰 IRule
的任何其他植入(只要它在您的域中有意义)。
顺便说一句,我正在使用一些 C#6 功能,例如字符串插值和表达式主体成员,但这不是必需的。