Precautions when writing a program: Part 3

Precautions when writing a program: Part 3

1. Conclusion </ b> 2. What is Liskov's substitution principle? </ B> 3. Why is Liskov's substitution principle necessary? </ B> 4. Liskov Substitution Principle How to use </ b> 5. What I learned from here </ b>

  1. Conclusion

Follow Liskov's substitution principle!

  1. What is Liskov's substitution principle?

● What is Liskov's substitution principle?

Simply put, parent classes are child classes I have to make the same movement </ b>

To put it the other way around, the child class is a method of the parent class. It means that you should not change the behavior.

  1. How to use Liskov's substitution principle

This will also be explained using an example!

Liskov Substitution Principle


class A
 def initialize(aaa)
 end

 def a
 end
end

class B < A
 def initialize(aaa)
  @bbb = aaa
 end
 
 def b
 end
end

================

class C < A 
 def initialize(aaa)
 @bbb = aaa
 @ccc = aaa
 end
 
 def a
 end
 
 def c
 end

Not Liskov's substitution principle


class A
 def initialize(aaa)
 end

 def a
 end
end

class B < A
 def initialize(aaa)
  @bbb = aaa
 end
 
 def b
 end
end

class B < A 
 def initialize(aaa)
 @ccc = aaa
 end

================

class B < A
 def initialize(aaa)
  @bbb = aaa
  @ccc = aaa
 end

def a
 if ccc
 else
    ccc_a
 end

 def c
 end
end

For those who are "not a Liskov Substitution Principle" Under ================ Although it is class B Towards the "Liskov Substitution Principle" Under ================ Converting to class C.

Rather than being together in class B It is divided into class iC and follows the principle.


  1. Do we need the Liskov Substitution Principle?

In conclusion, the key points when writing programming are: I think it will be summarized in three.

I) Maintainability: Correction (easy to find errors) and ease of management Ⅱ) Extensibility: Ease of implementing additional functions Ⅲ) Readability: Easy to understand description I think that is necessary.

The above three things are when I explain the other four principles I say the same thing, so if you read one of the five principles All the conclusions fit into these three.

In other words, to fulfill the above three Follow Liskov's substitution principle "also (because there are four other principles)"! !! </ b>

  1. What I learned from here

i) I didn't know the Liskov principle itself at all The research itself was very valuable!

ii) Although the parent class and the child class are linked, The point was not to be influenced by the parent class from the child class!