'Breaking the Liskov Subsitute Principle and other academic principles
I have never been good at recognizing the exact academic issues when conducting code reviews. The following code has a lot of issues and I know the solution to fix it, but I want to get better at describing the exact academic principles that are being broken.
Would the Widget class break the Liskov Substitute Principle if it were replaced with another instance of IWidget such as Widget2? Are there any other SOLID principles or academic principles that are being broken?
public class ProcessingService
{
private readonly IWidget _widget;
ProcessingService(IWidget widget)
{
_widget = widget;
}
public decimal Process()
{
// Fake input for now, but could be any dictionary of objects
var input = new Dictionary<string, object>();
return (decimal)_widget.Get(input);
}
}
public interface IWidget
{
object Get(Dictionary<string, object> input);
}
public class Widget : IWidget
{
public object Get(Dictionary<string, object> input)
{
var parameter = (int)input["parameter"];
// Fake some calculation with input and prepare return value....
var result = 0m;
return result;
}
}
public class Widget2 : IWidget
{
public object Get(Dictionary<string, object> input)
{
var parameter = (int)input["parameter"];
// Fake some calculation with input and prepare return value....
var result = "Widget String Value";
return result;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
