Liskov substitution principle in 3 minutes

The principle of large iskov replacement is the object-oriented design of large size in SO large ID. Despite having one of the more scary names in these five principles, it is actually very easy to wrap your head.

In fact, it is so simple that you will learn about it in about 3 minutes.

What Is It?

In simple terms, LSP points out that objects of the same superclass should be able to exchange with each other without destroying any objects.

If we have a cat and a dog derived from an animal-like class, using animal classes should be able to use cats or dogs and behave normally.

Let's do an example!

Even this definition does not explain it well, so let us put it into practice. The code will make everything clear.

In a real OOP way, let's super animal category, there is also a dog and cat subclass and capture their favorite kinds of food.

public static class Animal {
  public String favoriteFood;
  public Animal(String favoriteFood) {
    this.favoriteFood = favoriteFood;
  }
}

public static class Dog extends Animal {
  public Dog(String favoriteFood) {
    super(favoriteFood);
  }
}

public static class Cat extends Animal {
  public Cat(String favoriteFood) {
    super(favoriteFood);
  }
}

Now, we want to make a way for us to give these hairdressers some snacks. I do n’t want to put this method in the animal class, because it ’s my feeding the animals, not a behavior intrinsic to the animals. So I like something like dog feed may understand this, but I think feeding dogs is better, is n’t it?

Let's create this method and call it GiveTreatTo:

public static void GiveTreatTo(Animal animal) {
  String msg = "You fed the " + animal.getClass().getSimpleName() + " some "  + animal.favoriteFood;
  System.out.println(msg);
}

See here that GiveTreatTo requires any animal as a parameter. Since our animal constructors have distributed their favorite foods, we can almost always rely on the data there.

This means that we do not have to establish a method for each animal, namely GiveTreatToDog and GiveTreatToCat. Because we have implemented LSP, there is only one way. Let's take a look at what it does:

public static void main(String[] args) {
  Dog rover = new Dog("bacon");
  Cat bingo = new Cat("fish");

  GiveTreatTo(rover);
  GiveTreatTo(bingo);
}

Now, if we implement the LSP correctly, the program should run normally. Let's check the output:

You gave the Dog some bacon
You gave the Cat some fish

amazing. Another benefit of this subject is that we can add more and more animals and the GiveTreatTo method does not require any modification.

Wrap Up

Very scary? Like many other things in programming and computer science, the name obscures its simplicity. Now, go there and make your subclasses interchangeable, and thank Dr. Barbara Liskov for his useful principles.

from: https://dev.to//erikwhiting88/liskov-substitution-principle-in-3-minutes-2dc6

Published 0 original articles · liked 0 · visits 642

Guess you like

Origin blog.csdn.net/cunxiedian8614/article/details/105691162