SOLID principles in .NET revisited part 5: the Liskov Substitution Principle 2

Introduction

In the previous post we looked at letter ‘L’ in SOLID, i.e. the Liskov Substitution Principle. We saw how adhering to the LSP helps remove implementation-specific details from a client class that consumes those implementations. The client class will be able to consume the services of an abstraction without worrying about the actual implementation of the abstraction. The goal of LSP is that any implementation of an abstraction will work seamlessly within the consuming class.

We looked at the following cases that can indicate a violation of LSP:

  • switch or if-else blocks checking for the value of an enumeration
  • Code blocks that check the actual type of an abstraction to branch logic. This can be coupled with downcasting the abstraction to a concrete implementation type
  • Blocks with magic strings that check for a value in some string parameter and branch logic accordingly

There’s at least one more indicator which we haven’t seen so far. I decided to devote a separate article to that case as it brings us closer to the next constituent of SOLID, i.e. the Interface Segregation Principle.

Read more of this post

SOLID principles in .NET revisited part 4: the Liskov Substitution Principle

Introduction

In the previous post we looked at the Open-Closed Principle, i.e. ‘O’ in the SOLID acronym. We saw how the OCP facilitated the flexibility and extensibility of a class. It also places a constraint on a class by making it “append-only”. In other words you can extend a class but you cannot change the implementation of existing parts of a class.

In this post we’ll take a look at letter ‘L’ which stands for the Liskov Substitution Principle LSP.

Definition of LSP

LSP stands for the interchangeability of different implementations of an abstraction. An abstraction in C#, and probably most other popular object-oriented programming languages, can either be an abstract base class or an interface. According to LSP it shouldn’t make any difference what implementation of an abstraction you call from a client. Any concrete implementation of the abstraction should behave in a way that doesn’t break the client and doesn’t produce any unexpected and/or incorrect results. The client shouldn’t ever be concerned with the implementation details of an abstraction. It should be able to consume any concrete implementation “without batting an eye”.

Read more of this post

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: