SOLID Principles – Liskov substitution principle

Subtype Requirement: Let \phi (x) be a property provable about objects x of type T. Then {\displaystyle \phi (y)} should be true for objects y of type S where S is a subtype of T.

From Barbara Liskov and Jeannette Wing described the principle succinctly in a 1994 paper.

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:

  • Only strengthen invariants in subclasses; never weaken them
  • Only weaken preconditions when overriding methods
  • Only strengthen postconditions when overriding methods
  • Use Delegation instead of Inheritance
  • Figure out better abstractions

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

Leave a Reply