Subtype Requirement: Let
From Barbara Liskov and Jeannette Wing described the principle succinctly in a 1994 paper.be a property provable about objects of type T. Then should be true for objects of type S where S is a subtype of T.
The Liskov substitution principle (LSP) idea starts from “Data abstraction and hierarchy” that was initially introduced by Barbara Liskov in a 1988 conference keynote address. It is a particular definition of a subtyping relation, which is called “strong behavioral subtyping”; then is based on the concept of substitutability, which principles in object-oriented programming (OOP) stating that an object and a sub-object must be interchangeable without breaking the program.
What does it mean in the actual programs?
LSP has directly explain the programs what the developers should do, which means the child class objects should be able to replace parent class objects without the compromising application integrity. It means essentially that the developer should put an effort to create such derived class objects, which can replace the base class object without modifying its behavior. The application may end up being broken if the developers don’t.
What is a kind of situation which can apply the Liskov substitution principle?
The Liskov substitution principle (LSP) doesn’t often see in the real world, which doesn’t mean that it doesn’t appear in the real world in comparison with other principles of SOLID. It is a low frequency of use in the real world. If the developers or the situations want to use the LSP, the developers should consider the below strategies:
from the Liskov Substitution Principle (LSP) of the wiki principles.
How to implement the Liskov substitution principle (LSP)?
public void Solution()
{
Console.WriteLine("Liskov Substitution Principle");
var numbers = new int[] { 5, 7, 9, 8, 1, 6, 4 };
SumCalculator sum = new SumCalculator(numbers);
Console.WriteLine($"The sum of all the numbers: {sum.Calculate()}");
Console.WriteLine();
EvenNumbersSumCalculator evenSum = new EvenNumbersSumCalculator(numbers);
Console.WriteLine($"The sum of all the even numbers: {evenSum.Calculate()}");
}
public abstract class Calculator
{
protected readonly int[] _numbers;
public Calculator(int[] numbers)
{
_numbers = numbers;
}
public abstract int Calculate();
}
public class SumCalculator : Calculator
{
public SumCalculator(int[] numbers) : base(numbers)
{ }
public override int Calculate() => _numbers.Sum();
}
public class EvenNumbersSumCalculator : SumCalculator
{
public EvenNumbersSumCalculator(int[] numbers) : base(numbers)
{ }
public override int Calculate() => _numbers.Where(x => x % 2 == 0).Sum();
} Reference
What is the API gateway? An API gateway is an API management tool that sits…
The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…
In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…
In the field of software engineering, the interface segregation principle (ISP) states that no code…
In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be…
The single-responsibility principle (SRP) is a computer-programming principle that states that every module, class or…